I would like to display messages from my Discord channel on my Kirby homepage, but I cannot seem to connect the two.
Is it possible to integrate Discord with Kirby through node.js or php? Pasting the code from a failed attempt below for reference.
<?php
require_once("./Autoload.php");
$configs = include("./Configs.php");
$discord = New \Discord\Client(New \Discord\Configs($configs));
$channel = New \Discord\Client\Channels($discord, "XXX5976");
$messages = $channel->messages();
?>
Yes, you can connect to Discord via their API, either directly or through a library like the one you posted above.
Where are you using this code? Where did you put the library?
Thanks, @pixelijn. I added the folder from the library above to Kirby’s plugin folder. I then created a snippet with the following code.
<?php
require_once("../plugins/discord-php/Autoload.php");
$configs = include("../plugins/discord-php/Configs.php");
$discord = New \Discord\Client(New \Discord\Configs($configs));
$channel = New \Discord\Client\Channels($discord, "842382870664445976");
$messages = $channel->messages();
echo $messages;
?>
Is there a better way to set this up?
You should do the loading of the needed classes in the plugins index.php
. So folder structure:
--site
--plugins
--discord/
-- index.php
-- discord-php/
I.e. the library folder is inside your plugin folder, next to the index.php.
You will then have access to the loaded classes in your template/snippets/controllers…
Thanks, @pixelijn. All is working as planned now. I am pasting the updated code below for reference.
<?php
require_once("discord-php/Autoload.php");
$configs = include("discord-php/Configs.php");
$discord = New \Discord\Client(New \Discord\Configs($configs));
$channel = New \Discord\Client\Channels($discord, "842382870664445976");
$messages = $channel->messages();
foreach($messages as $message) {
echo $message->content;
}
?>