Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Discord = require('discord.io');
  2. var mysql = require('mysql');
  3. var auth = require('./auth.json');
  4.  
  5. var bot = new Discord.Client({
  6.     token: auth.token,
  7.     autorun: true
  8. });
  9.  
  10. var connection = mysql.createConnection({
  11.     host: 'localhost',
  12.     user: 'advil0',
  13.     password: '#####',
  14.     database: 'discord_bot'
  15. });
  16.  
  17. connection.connect();
  18.  
  19. bot.on('ready', function (evt) {
  20.     console.log('Connected');
  21.     console.log('Logged in as: ');
  22.     console.log(bot.username + ' - (' + bot.id + ')');
  23. });
  24.  
  25. bot.on('message', function (user, userID, channelID, message, evt) {
  26.     if(message.substring(0, 1) == '!') {
  27.         console.log('Incoming command: ' + message);
  28.         var args = message.substring(1).split(' ');
  29.         var cmd = args[0];
  30.  
  31.         if(cmd == 'help')
  32.         {
  33.             var helpMsg = 'List of commands: ';
  34.             var index = 0;
  35.             connection.query('SELECT command FROM commands', function(error, results, fields) {
  36.                 for(index = 0; index < results.length; ++index) {
  37.                     helpMsg += `!${results[index].command.toLowerCase()} `;
  38.                 }
  39.                 bot.sendMessage({
  40.                     to: channelID,
  41.                     message: helpMsg
  42.                 });
  43.             });
  44.         }
  45.         else {
  46.             connection.query('SELECT response FROM commands WHERE command = ?', [cmd.toLowerCase()], function(error, results, fields) {
  47.                 bot.sendMessage({
  48.                     to: channelID,
  49.                     message: results[0].response
  50.                 });
  51.             });
  52.         }
  53.     }
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement