diff --git a/package.json b/package.json index 706cbe1..ab555aa 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "license": "ISC", "dependencies": { "axios": "^0.19.2", - "discord.js": "^11.6.4", + "discord.js": "^12.5.3", "esm": "^3.2.25" }, "devDependencies": { diff --git a/src/boot.js b/src/boot.js index de033a6..c33dbba 100644 --- a/src/boot.js +++ b/src/boot.js @@ -6,75 +6,83 @@ const publicSSMApiPath = "https://hub.splitscreen.me/api/v1/"; const botPrefix = "-"; const DiscordInit = secretDiscordToken => { - const DiscordBot = new Discord.Client(); - - DiscordBot.commands = new Discord.Collection(); - - const commandFiles = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js')); - let availableCMDs = []; - for (const file of commandFiles) { - availableCMDs.push(file.replace('.js','')); - const command = require(`./commands/${file}`); - DiscordBot.commands.set(command.name, command); - } - - DiscordBot.on('ready', async () => { - console.log('[Debug] Connected as ' + DiscordBot.user.tag); - console.log('[Debug] Servers:'); - DiscordBot.guilds.forEach(guild => { - console.log('[Debug] - ' + guild.name); + const DiscordBot = new Discord.Client({ + commandPrefix: botPrefix }); - console.log( - `[Debug] Bot has started, with ${DiscordBot.users.size} user(s), in ${DiscordBot.channels.size} channel(s) of ${DiscordBot.guilds.size} server(s).`, - ); - try { - const allHandlers = await Axios.get(publicSSMApiPath + 'allhandlers'); - const totalHandlers = allHandlers.data.Handlers.length; - - await DiscordBot.user.setActivity('Hosting ' + totalHandlers + ' handlers!'); - - setInterval(async () => { - const allHandlers = await Axios.get(publicSSMApiPath + 'allhandlers'); - const totalHandlers = allHandlers.data.Handlers.length; - await DiscordBot.user.setActivity('Hosting ' + totalHandlers + ' handlers!'); - }, 60000); - } catch (e) { - console.log("error", e) - } - }); - - DiscordBot.on('guildCreate', guild => { - // This event triggers when the bot joins a guild. - console.log( - `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`, - ); - }); - - DiscordBot.on('guildDelete', guild => { - // this event triggers when the bot is removed from a guild. - console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`); - }); - - DiscordBot.login(secretDiscordToken); - - DiscordBot.on('message', async receivedMessage => { - if (receivedMessage.author.bot) return; //Will ignore bots and it-self - if (!receivedMessage.content.startsWith(botPrefix)) return; //Ignore messages from users which not start with {botPrefix} - const fullCommand = receivedMessage.content.substr(1); // Remove the leading {botPrefix} - const splitCommand = fullCommand.split(' '); // Split the message up in to pieces for each space - const commandName = splitCommand[0].toLowerCase(); // The first word directly after the {botPrefix} is the command - if (availableCMDs.includes(commandName)){ - const command = DiscordBot.commands.get(commandName); - try { - - command.execute(receivedMessage,DiscordBot); - } catch (error) { - console.error(error); - } - } else { - return; //Ignore CMDs which are not imported + + DiscordBot.commands = new Discord.Collection(); + + const commandFiles = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js')); + for (const file of commandFiles) { + const command = require(`./commands/${file}`); + const commandName = file.split('.')[0]; + + DiscordBot.commands.set(commandName, command); } - }); + + DiscordBot.on('ready', async () => { + console.log('[Debug] Connected as ' + DiscordBot.user.tag); + console.log('[Debug] Servers:'); + DiscordBot.guilds.cache.forEach((guild) => { + console.log('[Debug] - ' + guild.name); + }); + console.log( + `[Debug] Bot has started, with ${DiscordBot.users.size} user(s), in ${DiscordBot.channels.size} channel(s) of ${DiscordBot.guilds.size} server(s).`, + ); + try { + const allHandlers = await Axios.get(publicSSMApiPath + 'allhandlers'); + const totalHandlers = allHandlers.data.Handlers.length; + + await DiscordBot.user.setActivity('Hosting ' + totalHandlers + ' handlers!'); + + setInterval(async () => { + const allHandlers = await Axios.get(publicSSMApiPath + 'allhandlers'); + const totalHandlers = allHandlers.data.Handlers.length; + await DiscordBot.user.setActivity('Hosting ' + totalHandlers + ' handlers!'); + }, 60000); + } catch (e) { + console.log("error", e) + } + }); + + DiscordBot.on('guildCreate', guild => { + // This event triggers when the bot joins a guild. + console.log( + `New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`, + ); + }); + + DiscordBot.on('guildDelete', guild => { + // this event triggers when the bot is removed from a guild. + console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`); + }); + + DiscordBot.login(secretDiscordToken); + + DiscordBot.on('message', async receivedMessage => { + if (receivedMessage.author.bot) return; //Will ignore bots and it-self + if (!receivedMessage.content.startsWith(botPrefix)) return; //Ignore messages from users which not start with {botPrefix} + const fullCommand = receivedMessage.content.substr(1); // Remove the leading {botPrefix} + const splitCommand = fullCommand.split(' '); // Split the message up in to pieces for each space + const commandName = splitCommand[0].toLowerCase(); // The first word directly after the {botPrefix} is the command or alias + const command = //get the command based on a command or alias + DiscordBot.commands.get(commandName) || DiscordBot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName.toString())); + + console.log(`commandName => ${commandName}`); + console.log(`command => ${command}`); + + if (command) { + console.log("command recognized"); + try { + + command.execute(receivedMessage, DiscordBot); + } catch (error) { + console.error(error); + } + } else { + console.log("command NOT recognized"); //Ignore CMDs which are not imported + } + }); }; diff --git a/src/commands/create.js b/src/commands/create.js index d0b89de..de2dcfb 100644 --- a/src/commands/create.js +++ b/src/commands/create.js @@ -3,6 +3,7 @@ import Settings from "../../src/settings" module.exports = { name: 'create', + aliases: ['c'], description: 'Creates the handler from the provided information. Example: "-create GameName"', async execute(receivedMessage, DiscordBot) { console.log('settings: ',Settings.private.DEVELOPMENT_CHANNELS) diff --git a/src/commands/handler.js b/src/commands/handler.js index 3f1c21c..0040e61 100644 --- a/src/commands/handler.js +++ b/src/commands/handler.js @@ -4,6 +4,7 @@ const publicSSMApiPath = "https://hub.splitscreen.me/api/v1/"; module.exports = { name: 'handler', + aliases: ['h', 'script'], description: 'Return handler for the specified game. Example: "-handler Game name"', async execute(receivedMessage, DiscordBot) { let fullCommand = receivedMessage.content.substr(1); // Remove the leading exclamation mark