/** * 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"])) { $channelName = $msgData->channel->name; $guildName = $msgData->guild->name; $guildID = $msgData->guild->id; $switch = $data["messageArray"][0]; switch ($switch) { case "save": $fittingURL = $data["messageArray"][1]; unset($data["messageArray"][0]); unset($data["messageArray"][1]); $fittingName = implode(" ", $data["messageArray"]); $this->sluggardDB->execute("REPLACE INTO fittings (guildID, fittingName, fittingURL) VALUES (:guildID, :fittingName, :fittingURL)", array(":guildID" => $guildID, ":fittingName" => $fittingName, ":fittingURL" => $fittingURL)); $this->log->info("Saved fitting ({$fittingName}) on {$guildName}"); $msg = "Fitting saved, you can now call upon it by using !fit {$fittingName}"; $msgData->user->reply($msg); break; case "delete": unset($data["messageArray"][0]); $fittingName = implode(" ", $data["messageArray"]); if (strlen($fittingName) > 1) { $fittingData = $this->sluggardDB->queryField("SELECT fittingURL FROM fittings WHERE fittingName = :fittingName AND guildID = :guildID", "fittingURL", array(":fittingName" => $fittingName, ":guildID" => $guildID)); if ($fittingData) { $this->sluggardDB->execute("DELETE FROM fittings WHERE fittingName = :fittingName AND guildID = :guildID", array(":fittingName" => $fittingName, ":guildID" => $guildID)); $msg = "Fitting {$fittingName} was deleted"; } else { $msg = "Fitting {$fittingName} not found for this server"; } } else { $msg = "Error with your search"; } $msgData->user->reply($msg); break; default: $search = $data["messageString"]; if (strlen($search) > 1) { $fittingData = $this->sluggardDB->queryField("SELECT fittingURL FROM fittings WHERE fittingName = :fittingName AND guildID = :guildID", "fittingURL", array(":fittingName" => $search, ":guildID" => $guildID)); if ($fittingData) { $msg = "Fitting {$search} can be seen here: {$fittingData}"; } else { $msg = "Fitting {$search} not found for this server"; } } else { $msg = "Error with your search"; } $msgData->user->reply($msg); break; } } }
/** * */ public function createSluggardDB() { $tables = array("users", "usersSeen", "storage", "authentications"); $tableCreateCode = array("users" => "\r\n BEGIN;\r\n CREATE TABLE IF NOT EXISTS `users` (\r\n `id` INTEGER PRIMARY KEY AUTOINCREMENT,\r\n `serverID` BIGINT(20) NOT NULL,\r\n `userID` BIGINT(20) NOT NULL,\r\n `discordID` BIGINT(20) NOT NULL,\r\n `characterID` INT(16) NOT NULL,\r\n `corporationID` VARCHAR(255) NOT NULL,\r\n `allianceID` VARCHAR(255) NOT NULL,\r\n `authString` VARCHAR(255) NOT NULL,\r\n `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP\r\n );\r\n CREATE UNIQUE INDEX userID ON users (userID);\r\n CREATE INDEX serverID ON users (serverID);\r\n CREATE INDEX corporationID ON users (corporationID);\r\n CREATE INDEX allianceID ON users (allianceID);\r\n COMMIT;", "usersSeen" => "\r\n BEGIN;\r\n CREATE TABLE IF NOT EXISTS `usersSeen` (\r\n `id` INTEGER PRIMARY KEY,\r\n `name` VARCHAR(255) NOT NULL,\r\n `isAdmin` TINYINT(1) NOT NULL DEFAULT '0',\r\n `lastSeen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,\r\n `lastSpoke` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\r\n `lastStatus` VARCHAR(50) NULL DEFAULT NULL,\r\n `lastWritten` TEXT NULL\r\n );\r\n CREATE INDEX name ON usersSeen (name);\r\n COMMIT;", "storage" => "\r\n BEGIN;\r\n CREATE TABLE IF NOT EXISTS `storage` (\r\n `id` INTEGER PRIMARY KEY AUTOINCREMENT,\r\n `key` VARCHAR(255) NOT NULL,\r\n `value` VARCHAR(255) NOT NULL\r\n );\r\n CREATE UNIQUE INDEX key ON storage (key);\r\n COMMIT;", "authentications" => "\r\n BEGIN;\r\n CREATE TABLE IF NOT EXISTS `authentications` (\r\n `discordID` INTEGER PRIMARY KEY,\r\n `characterID` BIGINT(40) NOT NULL,\r\n `corporationID` BIGINT(40) NOT NULL,\r\n `allianceID` BIGINT(40) NOT NULL,\r\n `guildID` BIGINT(128) DEFAULT 0 NOT NULL\r\n );\r\n CREATE UNIQUE INDEX discordID ON authentications (discordID);\r\n COMMIT;\r\n ", "fittings" => "\r\n BEGIN;\r\n CREATE TABLE `fittings` (\r\n `guildID` INTEGER PRIMARY KEY,\r\n `fittingName` VARCHAR(255) NOT NULL,\r\n `fittingURL` VARCHAR(255) NOT NULL\r\n );\r\n CREATE UNIQUE INDEX fittingName ON fittings (guildID, fittingName);\r\n COMMIT;"); $dbName = $this->config->get("botName", "bot"); // Does the file exist? if (!file_exists(BASEDIR . "/config/database/{$dbName}.sqlite")) { touch(BASEDIR . "/config/database/{$dbName}.sqlite"); } // Check if the tables exist, if not, create them foreach ($tables as $table) { $exists = $this->sluggardDB->queryField("SELECT name FROM sqlite_master WHERE type = 'table' AND name = :name", "name", array(":name" => $table)); if (!$exists) { $this->log->warn("Creating {$table} in {$dbName}.sqlite, since it does not exist"); $this->sluggardDB->execute(trim($tableCreateCode[$table])); } } }
/** * When the bot's tick hits a specified time, this is started * * Runtime is defined in $this->information(), timerFrequency */ public function onTimer() { // Fetch all the characterIDs from the database $users = $this->sluggardDB->query("SELECT * FROM authentications"); foreach ($users as $user) { $discordID = $user["discordID"]; $guildID = $user["guildID"]; $characterID = $user["characterID"]; $corporationID = $user["corporationID"]; $allianceID = $user["allianceID"]; // Get the information for this user from CCP try { $ccpData = json_decode(json_encode(new SimpleXMLElement($this->curl->getData("https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids={$characterID}"))), true); $data = $ccpData["result"]["rowset"]["row"]["@attributes"]; $currentCharacterID = $data["characterID"]; $currentCorporationID = $data["corporationID"]; $currentAllianceID = $data["allianceID"]; // Lets just be sure we're doing this for the correct character.. CCP is weird sometimes if ($currentCharacterID == $characterID) { $remove = false; // Remove if the guy switched corp if ($currentCorporationID != $corporationID) { $remove = true; } // Remove if the guy switched alliance if ($currentAllianceID != $allianceID) { $remove = true; } // Lets remove the groups from this user (Every single role!) if ($remove == true) { $guild = $this->discord->guilds->first(); $guildName = $guild->name; $member = $guild->members->get("id", $discordID); $memberName = $member->user->username; $roles = $member->roles; // Remove all roles, we don't care what roles they are, remove them all.. // Can't remove server owner tho, so.. mehe.. foreach ($roles as $role) { $member->removeRole($role); } // Delete the auth info from the db $this->sluggardDB->execute("DELETE FROM authentications WHERE discordID = :discordID", array(":discordID" => $discordID)); $this->log->info("Deleted all roles for {$memberName} on {$guildName}"); } } } catch (\Exception $e) { $this->log->err("Error with character check: " . $e->getMessage()); } } }
/** * 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"])) { $channelName = $msgData->channel->name; $guildName = $msgData->guild->name; $authString = trim($data["messageString"]); $private = (bool) $msgData->channel->is_private; $authData = $this->authData->queryRow("SELECT * FROM registrations WHERE authString = :authString AND active = 1", array(":authString" => $authString)); if ($private) { return $msgData->user->reply("**Error:** you are trying to send your auth token in private. This won't work because i need Guild information. Post it in a channel on the server where you want to be authed"); } // Someone had a valid auth string, amazing if (!empty($authData)) { $groups = json_decode($authData["groups"], true); $roles = $msgData->guild->roles; $guild = $this->discord->guilds->first(); //$guild = $this->discord->guilds->get("id", $msgData->guild->id); $member = $guild->members->get("id", $msgData->user->author->id); $username = $msgData->user->author->username; $discordID = $msgData->user->author->id; $characterID = $authData["characterID"]; $corporationID = $authData["corporationID"]; $allianceID = $authData["allianceID"]; foreach ($roles as $role) { $roleName = $role->name; if (in_array($roleName, $groups)) { // Add user to group $member->addRole($role); $member->save(); } } // Add the user data to the local database so we can recheck it $this->sluggardDB->execute("REPLACE INTO authentications (discordID, characterID, corporationID, allianceID) VALUES (:discordID, :characterID, :corporationID, :allianceID)", array("discordID" => $discordID, ":characterID" => $characterID, ":corporationID" => $corporationID, ":allianceID" => $allianceID)); // Now set the auth to inactive, and we'll be golden $this->authData->execute("UPDATE registrations SET active = 0 WHERE authString = :authString", array(":authString" => $authString)); $this->log->info("Authenticating {$username} in {$channelName} on {$guildName}"); $msgData->user->reply("You have now been added to the following groups: " . implode(", ", $groups)); } else { $msgData->user->reply("**Error:** you are trying to use an already used auth code, or a non-existing auth code. Either way, prepare to get #rekt"); } } }
/** * Set a permanent storage object into the database * * @param $key * @param $value * @return null */ public function set($key, $value) { $this->db->execute("REPLACE INTO storage (`key`, value) VALUES (:key, :value)", array(":key" => $key, ":value" => $value)); }