DudeThatsErin

index.js

Nov 23rd, 2020 (edited)
896
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Defining Variables and Loggin into the Client/Bot */
  2. const Discord = require("discord.js");
  3. const fs = require("fs");
  4. const client = new Discord.Client( {
  5.   partials: ['USER', 'CHANNEL', 'GUILD_MEMBER', 'REACTION', 'MESSAGE'] // partials so I can include role reactions in this same bot.
  6. });
  7. const config = require("./config.json");
  8. // We also need to make sure we're attaching the config to the CLIENT so it's accessible everywhere!
  9. client.config = config;
  10.  
  11. /* Adding Events & Commands to bot */
  12.  
  13. /* Adding Events to bot */
  14. fs.readdir("./events/", (err, files) => {
  15.   if (err) return console.error(err);
  16.   files.forEach(file => {
  17.     const event = require(`./events/${file}`);
  18.     let eventName = file.split(".")[0];
  19.     client.on(eventName, event.bind(null, client));
  20.   });
  21. });
  22.  
  23. /* Reading Commands Files for usage */
  24. client.commands = new Discord.Collection();
  25. const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
  26. for (const file of commandFiles) {
  27.     console.log(file.slice(0,-3));
  28.     const command = require(`./commands/${file.slice(0,-3)}`)
  29.     client.commands.set(command.name, command);
  30. }
  31.  
  32. /* Database Stuffs */
  33. let connection;
  34. const guildCommandPrefixes = new Map(); // Creates a new map for per-server prefixes
  35.  
  36. /* Displays that the bot was logged in and loads the per-server prefixes */
  37. client.on('ready', () => {
  38.   const Guilds = client.guilds.cache.map(guild => guild.id);
  39.     console.log('Guilds: ' + Guilds);
  40.   console.log(client.user.tag + " has logged in.");
  41.   client.guilds.cache.forEach (guild => {
  42.     connection.query (
  43.       `SELECT cmdPrefix FROM GuildConfigurable WHERE guildId = '${guild.id}'`
  44.     ).then(result => {
  45.       guildCommandPrefixes.set(guild.id, result[0][0].cmdPrefix);
  46.     }).catch(err => console.log(err))
  47.   });
  48.  
  49.      // Sends embed when bot is reloaded
  50.      const onEmbed = new Discord.MessageEmbed() // WORKS!
  51.      .setColor('#000000')
  52.      .setFooter(`The bot is now online. :) If you have issues please ping cute.as.ducks#8061 (Erin).`);
  53.      let channel = client.channels.cache.find(c => c.id === '776192612671553578');
  54.    
  55.      channel.send(onEmbed);
  56.  
  57.     // Sets Bot's Status
  58.     client.user.setPresence({
  59.       status: "dnd",
  60.       activity: {
  61.           name: "++help",  
  62.           type: "PLAYING"
  63.       }
  64.     });
  65. });
  66.  
  67. /* Adds Guilds to SQL table if there isn't already a row for the guild */
  68. client.on('guildCreate', async (guild) => {
  69.   try {
  70.     await connection.query(
  71.       `INSERT INTO Guilds VALUES('${guild.id}', '${guild.ownerID}')`
  72.     );
  73.     await connection.query(
  74.       `INSERT INTO GuildConfigurable (guildId) VALUES ('${guild.id}')`
  75.     );
  76.   } catch(err) {
  77.     console.log(err);
  78.   }
  79. });
  80.  
  81. /* commands for per-server prefix */
  82. client.on('message', async (message) => {
  83.   if(message.author.bot) return;
  84.   const prefix = guildCommandPrefixes.get(message.guild.id);
  85.   if(message.content.toLowerCase().startsWith(prefix + 'ping')) {
  86.     message.channel.send(`Pong! You triggered this command with the prefix: ${prefix}`);
  87.   } else if(message.content.toLowerCase().startsWith(prefix + 'changeprefix')) {
  88.     if(message.member.id === message.guild.ownerID) {
  89.       const [ cmdName, newPrefix ] = message.content.split(" ");
  90.       if(newPrefix) {
  91.         try {
  92.           await connection.query(
  93.             `UPDATE GuildConfigurable SET cmdPrefix = '${newPrefix}' WHERE guildId = '${message.guild.id}'`
  94.           );
  95.           guildCommandPrefixes.set(message.guild.id, newPrefix);
  96.           message.channel.send(`Updated guild prefix to ${newPrefix}`);
  97.         } catch(err) {
  98.           console.log(err);
  99.           message.channel.send(`Failed to update guild prefix.`);
  100.         }
  101.       } else {
  102.         message.channel.send('❗ Please tell me what prefix you would like, I can\'t update the prefix unless you tell me. You should format this command like so: ' + `\`[originalprefix]changeprefix [newprefix]\``);
  103.       }
  104.     } else {
  105.       message.channel.send('❌ You do not have permission to use this command. Only guild owners can change this command.')
  106.     }
  107.   }
  108. });
  109.  
  110. /* Async function to login to database and the bot itself */
  111. (async () => {
  112.   connection = await require('../database/db.js');
  113.   await client.login(config.token);
  114. })();
RAW Paste Data