Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- const config = require('./config.json');
- const client = new Discord.Client();
- client.commands = new Discord.Collection();
- const fs = require('fs');
- // Read the Commands folder
- const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
- // Include the other files we have
- for (const file of commandFiles) {
- console.log(file.slice(0,-3));
- const command = require(`./commands/${file.slice(0,-3)}`)
- client.commands.set(command.name, command);
- }
- let connection;
- const guildCommandPrefixes = new Map();
- client.on('ready', () => {
- console.log(`${client.user.tag} is logged in and ready!`);
- client.guilds.cache.forEach(guild => {
- connection.query(
- `SELECT cmdPrefix FROM GuildConfigurable WHERE guildId = '${guild.id}'`
- ).then(result => {
- const guildId = guild.id;
- const prefix = result[0][0].cmdPrefix;
- guildCommandPrefixes.set(guildId, prefix);
- client.user.setPresence({
- status: "dnd",
- activity: {
- name: `${prefix}help`,
- type: "LISTENING"
- }
- });
- }).catch(err => console.log(err));
- });
- });
- client.on('guildCreate', async (guild) => {
- try {
- await connection.query(
- `INSERT INTO Guilds VALUES('${guild.id}', '${guild.ownerID}')`
- );
- await connection.query(
- `INSERT INTO GuildConfigurable (guildId) VALUES ('${guild.id}')`
- );
- } catch(err) {
- console.log(err);
- }
- });
- client.on('message', async (message) => { // Looking for a message
- const prefix = guildCommandPrefixes.get(message.guild.id);
- if (!message.content.startsWith(prefix) || message.author.bot) return;
- const args = message.content.slice(prefix.length).trim().split(/ +/);
- const commandName = args.shift().toLowerCase();
- const command = client.commands.get(commandName)
- || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
- if (!command) return;
- if (command.guildOnly && message.channel.type !== 'text') {
- return message.reply('I can\'t execute that command inside DMs!');
- }
- if (command.args && !args.length) {
- let reply = `You didn't provide any arguments, ${message.author}!`;
- if (command.usage) {
- reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
- }
- return message.channel.send(reply);
- }
- try {
- command.execute(message, args);
- } catch (error) {
- console.error(error);
- message.reply('there was an error trying to execute that command!');
- }
- }); // end of looking
- // if there is an error
- client.on('error', function(err) {
- console.log('Global error handler called:\n');
- if(err) {console.log(err);}
- });
- (async () => {
- connection = await require('./db.js');
- await client.login(config.client.token);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement