Customizing SMF > Bridges and Integrations

Post to Discord

(1/1)

cedgil:
Not quite sure this is the right place, anyway, ad I ould lihe SMF to interact with Discord, I guess it might be.

A long time ago, I has SMF to post in a Simpleportal shoutbox with this code in post.php :

--- Code: --- /////////////////////////////////////
// Post new subjects to shoutbox 1 //
/////////////////////////////////////
global $user_info, $board, $scripturl, $boardName, $topic;

$smcFunc['db_insert']('',
'{db_prefix}sp_shouts',
array(
'id_shoutbox' => 'int',
'id_member' => 'int',
'member_name' => 'string',
'log_time' => 'int',
'body' => 'string',
),
array(
1,
$user_info['id'],
$user_info['name'],
time(),
'[color=red][i]a posté nouveau sujet: [url='.$scripturl . '?topic=' . $topic . '.new#new'.']'.html_entity_decode($_POST['subject']).'[/url][/i][/color]',
),
array('id_shout')
);
////////////////////////////////////////////
// End of post new subjects to shoutbox 1 //
////////////////////////////////////////////

--- End code ---

As I want to my forum to interact closely with Discord, I'd like to do the same thing : whenever there's a new post on the forum, I want it to be announced on Discord. Yet, I didn't manage to have it work, whatever I try.

Here's is the best help I have found is (magazine.gaiscioch.com/guides/posting_to_discord_using_php_and_webhooks.html) :

The first part of this script is a function that will handle posting the data to your discord.

--- Code: ---<?PHP
function discordmsg($msg, $webhook) {
  if($webhook != "") {
    $ch = curl_init($webhook);
    $msg = "payload_json=" . urlencode(json_encode($msg))."";
   
    if(isset($ch)) {
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
    }
  }
}
?>
--- End code ---

The next part is the actual action and message you wish to send.

--- Code: ---<?PHP
// URL FROM DISCORD WEBHOOK SETUP
$webhook = "https://discordapp.com/api/webhooks/xxxxxxxxx/xxxxxxxx";
$msg = json_decode('
{
    "username":"BOTNAME",
    "content":"The message the BOTNAME posts.",
    "embeds": [{
        "title":"The Link Title",
        "description":"The Link Description",
        "url":"https://www.thelinkurl.com/",
        "color":DECIMALCOLORCODE,
        "author":{
            "name":"Site Name",
            "url":"https://www.sitelink.com/",
            "icon_url":"URLTOIMG"
        },
        "fields":[
            {
                "name":"LISTITEM1",
                "value":"LISTVALUE1",
                "inline":true
            },
            {
                "name":"LISTITEM2",
                "value":"LISTVALUE2",
                "inline":true
            },
            {
                "name":"LISTITEM3",
                "value":"LISTVALUE3",
                "inline":true
            }]
    }]
}
', true);
 ?>
--- End code ---

Lastly there's the call to fire the script.

--- Code: ---<?PHP
discordmsg($msg, $webhook); // SENDS MESSAGE TO DISCORD
?>
--- End code ---

Could anyone help me with getting it to work? Thanks in advance for any help.

Aleksi "Lex" Kilpinen:
Perhaps try to see how similar stuff has been made for other platforms like Twitter?
For example there's this SimpleTwitter

butch2k:
I use the RSS bot to do this.

Aleksi "Lex" Kilpinen:
That's an option too. :)

Navigation

[0] Message Index

Go to full version