Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Require libraries
- var tmi = require('tmi.js');
- var mysql = require('mysql');
- //Configure chat connection
- var options = {
- options: {
- debug: true
- },
- connection: {
- cluster: "aws",
- reconnect: true
- },
- identity: {
- username: "ShadowoBot27",
- password: "oauth:3qanta6l9vu80wl0x502ytgcxljg9x"
- },
- channels: ["ShadowMario27"]
- };
- //Configure MySql connection
- var con = mysql.createConnection({
- host: "localhost",
- user: "shadowobot27",
- password: "454pildl380z",
- database: "shadowobot"
- });
- //Connect to chat
- var client = new tmi.client(options);
- client.connect();
- //Connect to MySql
- con.connect(function(err) {
- if (err) throw err;
- console.log("Connected to MySql");
- });
- //Handle chat event
- client.on('chat', function(channel, userstate, message, self) {
- //Don't give a shit about own messages
- if (self) {
- return;
- }
- //Set user info variables
- var user = userstate["username"];
- var isMod = userstate["mod"];
- var userDisplayName = userstate["display-name"];
- //The most important command
- if (message.startsWith("gl")) {
- client.say("ShadowMario27", "thankS");
- return;
- }
- //Lock Samuel from editing !sam
- if (message.startsWith("!editcom sam") && user == "samgodro43") {
- client.say("ShadowMario27", "Sorry, Samuel is unable to edit his own command. TPFufun");
- return;
- }
- //Handle command adding
- if (message.startsWith("!addcom") && (isMod || user == "shadowmario27" || user == "challenger_egirl" )) {
- var errorResponse = "@" + userDisplayName + ", that is an invalid format. Please add command in the format: \"!addcom name text\".";
- //Get string index of the command name
- var a_commandIndex = message.indexOf(" ") + 1;
- //Respond with error if there is no command name
- if (a_commandIndex == 0) {
- client.say("ShadowMario27", errorResponse);
- return;
- }
- //Get string index of the command text
- var a_textIndex = message.substring(a_commandIndex).indexOf(" ") + a_commandIndex + 1;
- //Respond with error if there is no command text
- if (a_textIndex == a_commandIndex) {
- client.say("ShadowMario27", errorResponse);
- return;
- }
- //Get command name and command text
- var a_commandName = message.substring(a_commandIndex, a_textIndex - 1);
- var a_commandText = message.substring(a_textIndex);
- //Escape single quote character so MySql query works
- a_commandText = a_commandText.replace("'", "''");
- //Attempt to insert new command using stored procedure
- con.query("call AddCommand('" + a_commandName + "', '" + a_commandText + "')", function (err, result, fields) {
- if (err) throw err;
- if (result[0][0].response == "a_success") {
- //Procedure was able to add command successfully
- client.say("ShadowMario27", "@" + userDisplayName + ", command \"!" + a_commandName + "\" has been added.");
- }
- else {
- //Command already existed
- client.say("ShadowMario27", "@" + userDisplayName + ", command \"!" + a_commandName + "\" already exists.");
- }
- });
- }
- //Handle general text commands
- if (message.charAt(0) == "!") {
- var g_commandName = "";
- //If there is space after the command, only grab the command part of the message
- if (message.indexOf(" ") > -1) {
- g_commandName = message.substring(1, message.indexOf(" "));
- }
- else {
- g_commandName = message.substring(1);
- }
- //Query database for command, respond if it is found
- con.query("select command_text from command where command_name = '" + g_commandName + "'", function (err, result, fields) {
- if (err) throw err;
- if (!(result[0] == null)) {
- client.say("ShadowMario27", result[0].command_text);
- }
- else {
- return;
- }
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement