Skip to content

Instantly share code, notes, and snippets.

@scottheckel
Created August 2, 2019 14:02
Show Gist options
  • Save scottheckel/aff3b392fff4272432e3da1fe0091768 to your computer and use it in GitHub Desktop.
Save scottheckel/aff3b392fff4272432e3da1fe0091768 to your computer and use it in GitHub Desktop.
Neptune's Pride Message Discord Bot
In order to use this grab https://github.com/wrenoud/phpTriton. I also have a fork @ https://github.com/scottheckel/phpTriton that I added some things to but isn't needed for this.
Make sure you show restraint on how many times an hour you use it, so Jay doesn't remove this ability for everyone. I have it run once an hour using IFTTT, but you could easily modify it for curl.
<?php
require 'client.php';
// Discord Information
$discordApiUrl = 'https://discordapp.com/api/webhooks/INSERTYOURURL';
$discordBotName = 'leaderbot';
// Neptune's pride information
$alias = $_GET['username'];
$password = $_GET['password'];
$gameId = $_GET['gameId'];
// This is taken from the following url, but it's just a simple curl
// https://www.gaisciochmagazine.com/articles/posting_to_discord_using_php_and_webhooks.html
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;
}
}
}
$client = new TritonClient($alias, $password);
if ($client->authenticate()) {
$game = $client->GetGame($gameId);
$unread = $game->GetUnreadCount();
$total = $unread['diplomacy'] + $unread['events'];
if ($total > 0) {
echo 'new messages';
$payload = json_decode('
{
"username": $discordBotName,
"content": "You have '.$unread['diplomacy'].' messages and '.$unread['events'].' events.",
"embeds": []
}
', true);
discordmsg($payload, $discordApiUrl);
} else {
echo 'no new messages';
}
} else {
echo 'couldn\'t connect';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment