/** * Constructs the bot instance. * * @param string $configfile * @param Logger $log * @return void */ public function __construct($configfile, $log) { $this->configfile = $configfile; $this->log = $log; $config = Config::getConfig($this->configfile); $this->log->addInfo('Loaded config.', $config); $this->discord = Discord::createWithBotToken($config['token']); $this->websocket = new WebSocket($this->discord); }
/** * Handles the message. * * @param Message $message * @param array $params * @param Discord $discord * @param Config $config * @return void */ public static function handleMessage($message, $params, $discord, $config) { $id = isset($params[0]) ? $params[0] : $message->author->id; if (preg_match('/<@(.+)>/', $id, $matches)) { $id = $matches[1]; } $user = new User((array) Guzzle::get("users/{$id}"), true); $str = "**Information for {$user->username}#{$user->discriminator}:**\r\n"; $str .= "**ID:** {$user->id}\r\n"; $str .= "**Avatar URL:** {$user->avatar}\r\n"; $str .= "**Discriminator:** {$user->discriminator}\r\n"; $str .= "**Mention:** `{$user}`\r\n"; $guildcount = 0; $servers = ''; foreach ($discord->guilds as $guild) { foreach ($guild->members as $member) { if ($member->id == $user->id) { $guildcount++; $servers .= $guild->name . ", "; } } } $servers = rtrim($servers, ', '); $str .= "**Shared Servers:** {$guildcount} _({$servers})_\r\n"; $level = isset($config['perms']['perms'][$user->id]) ? $config['perms']['perms'][$user->id] : $config['perms']['default']; $level = $config['perms']['levels'][$level]; $str .= "**User Level:** {$level}\r\n\r\n"; $roles = ''; try { foreach ($message->full_channel->guild->members->get('id', $id)->roles as $role) { $roles .= str_replace('@everyone', '@ everyone', $role->name) . ', '; } } catch (\Exception $e) { $roles = 'Could not get roles.'; } $roles = rtrim($roles, ', '); $str .= "**User Roles:** _{$roles}_\r\n"; $joinedDiscord = Discord::getTimestamp($message->author->id); $str .= "**Joined Discord:** {$joinedDiscord}\r\n"; $joinedGuild = $message->full_channel->guild->members->get('id', $id)->joined_at; $str .= "**Joined Guild:** {$joinedGuild}\r\n"; $message->channel->sendMessage($str); }
/** * @param $serverHash * @throws \Discord\Exception\InvalidArgumentException */ public function verify($serverHash) { // Get logged in user information $characterInformation = $this->app->characters->getAllByID($_SESSION["characterID"]); $characterInformation["corporation"] = $this->app->corporations->getAllByID($this->app->characters->getAllByID($_SESSION["characterID"])["corporationID"]); $characterInformation["alliance"] = $this->app->alliances->getAllByID($this->app->characters->getAllByID($_SESSION["characterID"])["allianceID"]); $characterInformation["groups"] = $this->app->UsersGroups->getGroup($this->app->Users->getUserByName($characterInformation["characterName"])["id"]); // Get the discord server information $discordServerInfo = $this->app->discordServers->getAllByServerHash($serverHash); // Allowed Entities $allowedEntities = json_decode($discordServerInfo[0]["allowedEntities"], true); // Is the user allowed on this Discord server? $allowed = false; foreach ($characterInformation["groups"] as $group) { foreach ($allowedEntities as $type => $ent) { if ($type == "corporationID") { if (in_array($group["groupID"], $ent)) { $allowed = true; } } if ($type == "allianceID") { if ($group["groupID"] == $ent) { $allowed = true; } } } } // User is allowed if ($allowed) { $authString = md5($serverHash . md5($characterInformation["characterName"])); $this->app->discordUsers->insertIntoDiscordUsers($this->app->discordServers->getServerIDByServerHash($serverHash), $this->app->Users->getUserByName($characterInformation["characterName"])["id"], $characterInformation["characterID"], $characterInformation["corporationID"], $characterInformation["allianceID"], $authString, date("Y-m-d H:i:s")); $serverName = $this->app->discordServers->getServerNameByServerHash($serverHash); $channelID = $this->discord->api("guild")->channels()->show($this->app->discordServers->getServerIDByServerHash($serverHash))[0]["id"]; $inviteData = $this->discord->api("channel")->invites()->create($channelID, 60 * (mt_rand(1, 500) + 30), 1); // gotta randomize the time, as to create multiple invite links at the same time.. $inviteLink = "https://discord.gg/" . $inviteData["code"]; $authString = "!auth " . $this->db->queryField("SELECT authString FROM discordUsers WHERE serverID = :serverID AND userID = :userID", "authString", array(":serverID" => $this->app->discordServers->getServerIDByServerHash($serverHash), ":userID" => $this->app->Users->getUserByName($characterInformation["characterName"])["id"])); render("discord/authenticated.twig", array("serverName" => $serverName, "inviteLink" => $inviteLink, "authString" => $authString)); } else { $this->app->redirect("/"); } }
<?php /* * This file is part of cleanse/discord-hypertext package. * * (c) 2015-2015 Paul Lovato <*****@*****.**> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ error_reporting(-1); require __DIR__ . '/../vendor/autoload.php'; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); use Discord\Discord; // Email/Password for Discord $email_address = getenv('DISCORD_EMAIL'); $password = getenv('DISCORD_PASSWORD'); // Try log into Discord! $discord = new Discord($email_address, $password, $token = null); //Cache this for your next queries, pass it as the third argument $cachedToken = $discord->token();
public function __construct(Discord $client) { $this->client = $client; $this->token = $client->token(); }
* This file is part of cleanse/discord-hypertext package. * * (c) 2015-2015 Paul Lovato <*****@*****.**> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ error_reporting(-1); require __DIR__ . '/../vendor/autoload.php'; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); use Discord\Discord; use Discord\Helpers\Mentions; // Email/Password for Discord $email_address = getenv('DISCORD_EMAIL'); $password = getenv('DISCORD_PASSWORD'); // Try log into Discord! $discord = new Discord($email_address, $password); $channelId = '105775765877989376'; echo '<pre>'; $messages = $discord->api('message')->get($channelId); // Script start $time1 = microtime(true); $mentions = new Mentions($discord); foreach ($messages as $message) { echo '<b>' . $message['author']['username'] . '</b> ' . $mentions->addMentions($message) . '<br>'; } // Script end $time2 = microtime(true); echo "script execution time: " . ($time2 - $time1); //value in seconds
/** * Tests logging out of Discord. * * @return void */ public function testLoggingOut() { $discord = new Discord(getenv('DISCORD_EMAIL'), getenv('DISCORD_PASSWORD')); $discord->logout(); }
/** * */ public function listen() { $this->discord->on(Event::GUILD_CREATE, function (Guild $guild) { $this->managerFactory->create($guild); }); $this->discord->on(Event::MESSAGE_CREATE, [$this, 'onMessageCreate']); $this->discord->on(Event::CHANNEL_CREATE, function (Channel $channel) { $this->emitServerEvent($channel->guild, 'channelCreate', $channel); }); $this->discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, Channel $oldChannel) { $this->emitServerEvent($channel->guild, 'channelUpdate', $channel, $oldChannel); }); $this->discord->on(Event::CHANNEL_DELETE, function (Channel $channel) { $this->emitServerEvent($channel->guild, 'channelDelete', $channel); }); $this->discord->on(Event::GUILD_BAN_ADD, function (Ban $ban) { $this->emitServerEvent($ban->guild, 'ban', $ban); }); $this->discord->on(Event::GUILD_BAN_REMOVE, function (Ban $ban) { $this->emitServerEvent($ban->guild, 'unban', $ban); }); $this->discord->on(Event::GUILD_DELETE, function (Guild $guild) { $this->emitServerEvent($guild, 'serverDelete', $guild); }); $this->discord->on(Event::GUILD_MEMBER_ADD, function (Member $member) { $guild = $this->discord->guilds->get('id', $member->guild_id); $this->emitServerEvent($guild, 'memberCreate', $member); }); $this->discord->on(Event::GUILD_MEMBER_REMOVE, function (Member $member) { $guild = $this->discord->guilds->get('id', $member->guild_id); $this->emitServerEvent($guild, 'memberDelete', $member); }); $this->discord->on(Event::GUILD_MEMBER_UPDATE, function (Member $member, Discord $discord, Member $oldMember) { $guild = $this->discord->guilds->get('id', $member->guild_id); $this->emitServerEvent($guild, 'memberUpdate', $member, $oldMember); }); $this->discord->on(Event::GUILD_ROLE_CREATE, function (Role $role) { $guild = $this->discord->guilds->get('id', $role->guild_id); $this->emitServerEvent($guild, 'roleCreate', $role); }); $this->discord->on(Event::GUILD_ROLE_DELETE, function (Role $role) { $guild = $this->discord->guilds->get('id', $role->guild_id); $this->emitServerEvent($guild, 'roleDelete', $role); }); $this->discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, Role $oldRole) { $guild = $this->discord->guilds->get('id', $role->guild_id); $this->emitServerEvent($guild, 'roleUpdate', $role, $oldRole); }); $this->discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, Guild $oldGuild) { $this->emitServerEvent($guild, 'serverUpdate', $guild, $oldGuild); }); $this->discord->on(Event::MESSAGE_DELETE, function ($message) { if ($message instanceof Message) { $this->emitServerEvent($message->channel->guild, 'messageDelete', $message); } }); $this->discord->on(Event::MESSAGE_UPDATE, function (Message $message, Discord $discord, Message $oldMessage) { $this->emitServerEvent($message->channel->guild, 'messageUpdate', $message, $oldMessage); }); $this->discord->on(Event::PRESENCE_UPDATE, function (PresenceUpdate $presenceUpdate, Discord $discord, PresenceUpdate $oldPresenceUpdate) { $this->emitServerEvent($presenceUpdate->guild, 'presenceUpdate', $presenceUpdate, $oldPresenceUpdate); }); $this->discord->on(Event::TYPING_START, function (TypingStart $typingStart) { $this->emitServerEvent($typingStart->channel->guild, 'typingStart', $typingStart); }); $this->discord->on(Event::VOICE_STATE_UPDATE, function (VoiceStateUpdate $voiceStateUpdate) { $this->emitServerEvent($voiceStateUpdate->guild, 'voiceStateUpdate', $voiceStateUpdate); }); }
/** * */ public function run() { // Reap the threads! $this->websocket->loop->addPeriodicTimer(600, function () { $this->log->addInfo('Restarting the threading pool, to clear out old threads..'); // Shutdown the pool $this->pool->shutdown(); $this->timers->shutdown(); // Startup the pool again $this->pool = new \Pool(count($this->onMessage), \Worker::class); $this->timers = new \Pool(count($this->onTimer), \Worker::class); }); // Handle the onReady event, and setup some timers and so forth $this->websocket->on('ready', function (Discord $discord) { $this->log->addInfo('Websocket connected..'); // Update our presence status $game = new Game(array('name' => $this->globalConfig->get('presence', 'bot', "table flippin'"), 'url' => null, 'type' => null), true); $this->websocket->updatePresence($game, false); // Count the amount of people we are available to.. /** @var Guild $guild */ foreach ($this->discord->getClient()->getGuildsAttribute()->all() as $guild) { $this->extras['memberCount'] += $guild->member_count; $this->extras['guildCount']++; $this->extras['guild']['memberCount']["id{$guild->id}"] = $guild->member_count; $this->extras['onMessagePlugins'] = $this->onMessage; $this->extras['onVoicePlugins'] = $this->onVoice; } $this->log->addInfo("Member count, currently available to: {$this->extras['memberCount']} people"); // Setup the timers for the timer plugins foreach ($this->onTimer as $command => $data) { $this->websocket->loop->addPeriodicTimer($data['timer'], function () use($data, $discord) { try { $plugin = new $data['class']($discord, $this->log, $this->globalConfig, $this->db, $this->curl, $this->settings, $this->permissions, $this->container->get('serverConfig'), $this->users, $this->extras); $this->timers->submit($plugin); } catch (\Exception $e) { $this->log->addError("Error running the periodic timer: {$e->getMessage()}"); } }); } // Issue periodically recounting and other things (Needed because of pthreads not putting the entire context into children - leading to some weirdness in some plugins) $this->websocket->loop->addPeriodicTimer(600, function () { $this->extras['memberCount'] = 0; $this->extras['guildCount'] = 0; /** @var Guild $guild */ foreach ($this->discord->getClient()->getGuildsAttribute()->all() as $guild) { $this->extras['memberCount'] += $guild->member_count; $this->extras['guildCount']++; $this->extras['guild']['memberCount']["id{$guild->id}"] = $guild->member_count; $this->extras['onMessagePlugins'] = $this->onMessage; $this->extras['onVoicePlugins'] = $this->onVoice; } // Output periodic information while doing the recounting stuff $this->log->addInfo('Currently running audio streams: ' . count($this->audioStreams)); $this->log->addInfo("Member recount, currently available to: {$this->extras['memberCount']} people"); }); // @todo run a timer to check if there are any active voice sessions - and if there are, if there are any people in those voice sessions // If not, stop the session and leave the channel (To save some bandwidth) }); $this->websocket->on('error', function ($error, $websocket) { $this->log->addError('An error occurred on the websocket', [$error->getMessage()]); die(1); }); $this->websocket->on('close', function ($opCode, $reason) { $this->log->addWarning('Websocket got closed', ['code' => $opCode, 'reason' => $reason]); die(1); }); $this->websocket->on('reconnecting', function () { $this->log->addInfo('Websocket is reconnecting..'); }); $this->websocket->on('reconnected', function () { $this->log->addInfo('Websocket was reconnected..'); }); // Handle incoming message logging $this->websocket->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { $this->log->addInfo("Message from {$message->author->username}", [$message->content]); // Don't update data for ourselves.. if ($message->author->id !== $discord->getClient()->id) { $this->users->set($message->author->id, $message->author->username, 'online', null, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $message->content); } // @todo Create text logs }); // Handle plugin running $this->websocket->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { $guildID = $message->getChannelAttribute()->guild_id; // Get server config $config = $this->settings->get($guildID); // Is the person admin? $userDiscordID = $message->author->id; foreach ($this->globalConfig->get('admins', 'permissions') as $admins) { $message->isAdmin = $admins === $userDiscordID; } // Define the prefix if it isn't already set.. @($config->prefix = $config->prefix ?? $this->globalConfig->get('prefix', 'bot')); // Check if the user requested an onMessage plugin if (substr($message->content, 0, strlen($config->prefix)) === $config->prefix) { $content = explode(' ', $message->content); foreach ($this->onMessage as $command => $data) { $parts = []; foreach ($content as $index => $c) { foreach (explode("\n", $c) as $p) { $parts[] = $p; } } if ($parts[0] === $config->prefix . $command) { // If they are listed under the admins array in the bot config, they're the super admins if (in_array($message->author->id, $this->globalConfig->get('admins', 'permissions'))) { $userPerms = 3; } elseif (null !== $message->getChannelAttribute()->getGuildAttribute()->owner_id && $message->author->id === $message->getChannelAttribute()->getGuildAttribute()->owner_id) { $userPerms = 2; } else { $userPerms = 1; } if ($userPerms >= $data['permissions']) { try { $message->getChannelAttribute()->broadcastTyping(); if ($data['class'] === "\\Sovereign\\Plugins\\onMessage\\auth") { /** @var \Threaded $plugin */ $plugin = new $data['class']($message, $discord, $config, $this->log, $this->globalConfig, $this->db, $this->curl, $this->settings, $this->permissions, $this->container->get('serverConfig'), $this->users, $this->extras); $plugin->run(); } else { /** @var \Threaded $plugin */ $plugin = new $data['class']($message, $discord, $config, $this->log, $this->globalConfig, $this->db, $this->curl, $this->settings, $this->permissions, $this->container->get('serverConfig'), $this->users, $this->extras); $this->pool->submit($plugin); } $this->log->addInfo("{$message->author->username}#{$message->author->discriminator} ({$message->author}) ran command {$config->prefix}{$command}", $content); } catch (\Exception $e) { $this->log->addError("Error running command {$config->prefix}{$command}. Command run by {$message->author->username} in {$message->getChannelAttribute()->name}. Error: {$e->getMessage()}"); $message->reply("**Error:** There was a problem running the command: {$e->getMessage()}"); } } } } } }); // Handle joining a voice channel, and playing.. stuff.... $this->websocket->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { // Get the guildID $guildID = $message->getChannelAttribute()->guild_id; // Get this guilds settings $config = $this->settings->get($guildID); // Get the prefix for this guild @($config->prefix = $config->prefix ?? $this->globalConfig->get('prefix', 'bot')); if (substr($message->content, 0, strlen($config->prefix)) === $config->prefix) { $content = explode(' ', $message->content); foreach ($this->onVoice as $command => $data) { $parts = []; foreach ($content as $index => $c) { foreach (explode("\n", $c) as $p) { $parts[] = $p; } } if ($parts[0] === $config->prefix . $command) { try { $voiceChannels = $message->getFullChannelAttribute()->getGuildAttribute()->channels->getAll('type', 'voice'); foreach ($voiceChannels as $channel) { if (!empty($channel->members[$message->author->id])) { $voice = new $data['class'](); $voice->run($message, $discord, $this->websocket, $this->log, $this->audioStreams, $channel, $this->curl); } } } catch (\Exception $e) { $this->log->addError("Error running voice command {$config->prefix}{$command}. Command run by {$message->author->username} in {$message->getChannelAttribute()->name}. Error: {$e->getMessage()}"); $message->reply("**Error:** There was a problem running the command: {$e->getMessage()}"); } } } } }); // Handle if it's a message for the bot (CleverBot invocation) $this->websocket->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { // If we got highlighted we should probably answer back if (stristr($message->content, $discord->getClient()->id)) { try { $this->pool->submit(new cleverBotMessage($message, $discord, $this->log, $this->globalConfig, $this->db, $this->curl, $this->settings, $this->permissions, $this->container->get('serverConfig'), $this->users)); } catch (\Exception $e) { $message->reply("**Error:** There was an error with CleverBot: {$e->getMessage()}"); } } }); // Handle presence updates $this->websocket->on(Event::PRESENCE_UPDATE, function (PresenceUpdate $presenceUpdate) { if ($presenceUpdate->user->id && $presenceUpdate->user->username) { try { $this->log->addInfo("Updating presence info for {$presenceUpdate->user->username}"); $game = $presenceUpdate->getGameAttribute()->name ?? null; $this->users->set($presenceUpdate->user->id, $presenceUpdate->user->username, $presenceUpdate->status, $game, date('Y-m-d H:i:s'), null, null); } catch (\Exception $e) { $this->log->addError("Error: {$e->getMessage()}"); } } }); // Create a new cleverbot \nick\ for this new guild $this->websocket->on(Event::GUILD_CREATE, function (Guild $guild) { $cleverBotExists = $this->db->queryField("SELECT serverID FROM cleverbot WHERE serverID = :serverID", "serverID", array(":serverID" => $guild->id)); $guildExists = $this->db->queryField("SELECT guildID FROM guilds WHERE guildID = :serverID", "guildID", array(":serverID" => $guild->id)); // Only create a new server nick if the cleverbot instance doesn't exist.. (Hopefully cleverbot.io is done deleting them at random) if (!isset($cleverBotExists)) { $this->log->addInfo("Setting up Cleverbot for {$guild->name}"); $serverID = $guild->id; $result = $this->curl->post('https://cleverbot.io/1.0/create', ['user' => $this->globalConfig->get('user', 'cleverbot'), 'key' => $this->globalConfig->get('key', 'cleverbot')]); if ($result) { $result = @json_decode($result); $nick = $result->nick ?? false; if ($nick) { $this->db->execute('INSERT INTO cleverbot (serverID, nick) VALUES (:serverID, :nick) ON DUPLICATE KEY UPDATE nick = :nick', [':serverID' => $serverID, ':nick' => $nick]); } } } if (!isset($guildExists)) { $this->db->execute("INSERT IGNORE INTO guilds (guildID) VALUES (:guildID)", array(":guildID" => $guild->id)); // Send a hello message to the channel (Only if it's new!) //$message = "Hello, i was invited here by someone with admin permissions, i have quite a few features that you can discover by doing %help\n"; //$message .= "I am sorry if i am triggering other bots aswell, you can change my trigger with %config setTrigger newTrigger (Example: %config setTrigger *)\n"; //$message .= "If you for some reason don't want me here after all, just kick me ;)"; // Get the first channel in the list (usually the default channel) //$channel = $guild->channels->first(); //$channel->sendMessage($message); } }); // Run the websocket, and in turn, the bot! $this->websocket->run(); }
/** * Will add the services for the WebSocket. * * @param Service[] $services * * @return void */ private function setServices($services) { foreach ($services as $service) { $this->discord->on($service->getEvent(), $service->getListener()); } }
<?php error_reporting(-1); require __DIR__ . '/../vendor/autoload.php'; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); use Discord\Discord; use Discord\Helpers\Permissions; use Discord\Helpers\RoleColors; // Email/Password for Discord $email_address = getenv('DISCORD_EMAIL'); $password = getenv('DISCORD_PASSWORD'); // Try log into Discord! $discord = new Discord($email_address, $password); $permissions = new Permissions(); //pass in null if you want the role to have zero permissions $color = new RoleColors(); /* * By default a role is created with the following permissions: * Read Messages, Send Messages, Send TTS Messages, Embed Links, * Attach Files, Read Message History, Mention Everyone * Voice Connect, Voice Speak, Use Voice Activity * Create Instant Invite * * Let's revoke the user's ability to create instant invites and send TTS messages... */ $permissions->setCreateInstantInvite(false); $permissions->setSendTTSMessages(false); echo '<pre>'; echo 'Look good? <br>'; print_r($permissions->listPermissions());
/** * */ public function run() { $guildID = $this->message->getFullChannelAttribute()->guild_id; $cleverBotNick = $this->db->queryField("SELECT nick FROM cleverbot WHERE serverID = :serverID", "nick", array(":serverID" => $guildID)); // Simply remove the <id> part of the string, since it seems to make the responses from Cleverbot be less idiotic and terrible.. $msg = str_replace("<@{$this->discord->getClient()->id}>", "", $this->message->content); $response = $this->curl->post("https://cleverbot.io/1.0/ask", array("user" => $this->config->get("user", "cleverbot"), "key" => $this->config->get("key", "cleverbot"), "nick" => $cleverBotNick, "text" => $msg)); if ($response) { $resp = @json_decode($response); $reply = isset($resp->response) ? $resp->response : false; if ($reply) { $this->message->getChannelAttribute()->broadcastTyping(); $this->message->reply($reply); } } }
/** * When a message arrives that contains a trigger, this is started * * @param $msgData */ public function onMessage(stdClass $msgData) { $message = $msgData->message->message; $data = $this->trigger->trigger($message, $this->information()["trigger"]); if (isset($data["trigger"])) { if (preg_match('/https:\\/\\/discord.gg\\/(.+)/', $message, $matches)) { $invite = $this->discord->acceptInvite($message); $msgData->user->reply("Joined server {$invite->guild->name}"); } } }