Advertisement
DudeThatsErin

index.js

Jan 7th, 2021 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const config = require('./config.json');
  3. const client = new Discord.Client();
  4. client.commands = new Discord.Collection();
  5. const fs = require('fs');
  6.  
  7. // Read the Commands folder
  8. const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
  9.  
  10. // Include the other files we have
  11. for (const file of commandFiles) {
  12.     console.log(file.slice(0,-3));
  13.     const command = require(`./commands/${file.slice(0,-3)}`)
  14.     client.commands.set(command.name, command);
  15. }
  16.  
  17. let connection;
  18. const guildCommandPrefixes = new Map();
  19.  
  20. client.on('ready', () => {
  21.     console.log(`${client.user.tag} is logged in and ready!`);
  22.  
  23.     client.guilds.cache.forEach(guild => {
  24.         connection.query(
  25.             `SELECT cmdPrefix FROM GuildConfigurable WHERE guildId = '${guild.id}'`
  26.         ).then(result => {
  27.             const guildId = guild.id;
  28.             const prefix = result[0][0].cmdPrefix;
  29.             guildCommandPrefixes.set(guildId, prefix);
  30.             client.user.setPresence({
  31.                 status: "dnd",
  32.                 activity: {
  33.                     name: `${prefix}help`,  
  34.                     type: "LISTENING"
  35.                 }
  36.             });
  37.         }).catch(err => console.log(err));
  38.     });
  39.  
  40.  
  41.  
  42. });
  43.  
  44. client.on('guildCreate', async (guild) => {
  45.     try {
  46.         await connection.query(
  47.             `INSERT INTO Guilds VALUES('${guild.id}', '${guild.ownerID}')`
  48.         );
  49.         await connection.query(
  50.             `INSERT INTO GuildConfigurable (guildId) VALUES ('${guild.id}')`
  51.         );
  52.     } catch(err) {
  53.         console.log(err);
  54.     }
  55. });
  56.  
  57. client.on('message', async (message) => { // Looking for a message
  58. const prefix = guildCommandPrefixes.get(message.guild.id);
  59. if (!message.content.startsWith(prefix) || message.author.bot) return;
  60.  
  61. const args = message.content.slice(prefix.length).trim().split(/ +/);
  62. const commandName = args.shift().toLowerCase();
  63.  
  64. const command = client.commands.get(commandName)
  65.     || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
  66.  
  67. if (!command) return;
  68.  
  69. if (command.guildOnly && message.channel.type !== 'text') {
  70.     return message.reply('I can\'t execute that command inside DMs!');
  71. }
  72.  
  73. if (command.args && !args.length) {
  74.     let reply = `You didn't provide any arguments, ${message.author}!`;
  75.  
  76.     if (command.usage) {
  77.         reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
  78.     }
  79.  
  80.     return message.channel.send(reply);
  81. }
  82.  
  83. try {
  84.     command.execute(message, args);
  85. } catch (error) {
  86.     console.error(error);
  87.     message.reply('there was an error trying to execute that command!');
  88. }
  89.    
  90. }); // end of looking
  91.  
  92. // if there is an error
  93. client.on('error', function(err) {
  94.     console.log('Global error handler called:\n');
  95.     if(err) {console.log(err);}
  96. });
  97.  
  98. (async () => {
  99.     connection = await require('./db.js');
  100.     await client.login(config.client.token);
  101. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement