Skip to content

Commit

Permalink
Added aliases to commands, and updated to discord.js v12.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Snailedlt committed Aug 12, 2021
1 parent 8481edc commit e8443ed
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
142 changes: 75 additions & 67 deletions src/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});

};

Expand Down
1 change: 1 addition & 0 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/commands/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8443ed

Please sign in to comment.