Adding dot commands from nodbot

This commit is contained in:
Skylar Grant 2023-05-16 13:24:42 -04:00
parent 56d9cacfbe
commit de6b9ec4e4
6 changed files with 133 additions and 1 deletions

10
dot-commands/_template Normal file
View File

@ -0,0 +1,10 @@
const fn = require('../modules/functions.js');
module.exports = {
name: "",
description: "",
usage: "",
execute(message, commandData) {
// Code here...
}
}

11
dot-commands/ping.js Normal file
View File

@ -0,0 +1,11 @@
const fn = require('../modules/functions.js');
module.exports = {
name: "ping",
description: "pong",
usage: "ping pong",
async execute(message, commandData) {
// Code here...
await message.reply("Pong!");
}
}

17
dot-commands/servers.js Normal file
View File

@ -0,0 +1,17 @@
const fn = require('../modules/functions.js');
module.exports = {
name: "servers",
description: "Get a list of servers the bot is in",
usage: ".servers",
async execute(message, commandData) {
let servers = [];
const count = JSON.stringify(message.client.guilds.cache.size);
servers.push("I'm currently in " + count + " servers.");
const guilds = await message.client.guilds.cache;
await guilds.each(g => {
servers.push(g.name + "," + g.ownerId);
});
await message.reply(servers.join("\n"));
}
}

21
main.js
View File

@ -29,6 +29,8 @@ const isDev = process.env.DEBUG;
client.once('ready', async () => { client.once('ready', async () => {
fn.collectionBuilders.slashCommands(client); fn.collectionBuilders.slashCommands(client);
fn.collectionBuilders.dotCommands(client);
fn.collectionBuilders.setvalidCommands(client);
await fn.collectionBuilders.guildInfos(client); await fn.collectionBuilders.guildInfos(client);
await fn.collectionBuilders.messageCollectors(client); await fn.collectionBuilders.messageCollectors(client);
// checkRateLimits(); // checkRateLimits();
@ -92,6 +94,25 @@ client.on('messageUpdate', async (oldMessage, message) => {
client.on('messageCreate', async message => { client.on('messageCreate', async message => {
await fn.messages.updateHandler(message).catch(e => console.error(e)); await fn.messages.updateHandler(message).catch(e => console.error(e));
// Dot Command Handling
// Some basic checking to prevent running unnecessary code
if (message.author.bot) return;
// Break the message down into its components and analyze it
const commandData = fn.dotCommands.getCommandData(message);
if (isDev) console.log(console.log(commandData));
if (commandData.isValid && commandData.isCommand && (message.author.id == process.env.ownerId)) {
try {
client.dotCommands.get(commandData.command).execute(message, commandData);
}
catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command.');
}
}
return;
}); });
async function checkRateLimits(hi) { async function checkRateLimits(hi) {

View File

@ -17,6 +17,7 @@ const { GuildInfo } = require('./CustomClasses');
const config = require('../data/config.json'); const config = require('../data/config.json');
const strings = require('../data/strings.json'); const strings = require('../data/strings.json');
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js')); const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js'));
const dbfn = require('./dbfn.js'); const dbfn = require('./dbfn.js');
const { finished } = require('stream'); const { finished } = require('stream');
@ -35,6 +36,44 @@ const functions = {
} }
if (isDev) console.log('Slash Commands Collection Built'); if (isDev) console.log('Slash Commands Collection Built');
}, },
dotCommands(client) {
// If the dotCommands collection doesn't exist already, create it
if (!client.dotCommands) client.dotCommands = new Discord.Collection();
// Empty the dotcommands collection
client.dotCommands.clear();
// Iterate over each file within ./dot-commands that ends with .js
for (const file of dotCommandFiles) {
// Pull the file into a temporary variable
const dotCommand = require(`../dot-commands/${file}`);
// Create a new entry in the collection with the key of the command name, value of the command itself
client.dotCommands.set(dotCommand.name, dotCommand);
// Old code from NodBot to implement aliases for dot commands. Unused as of 5/16/23
// if (Array.isArray(dotCommand.alias)) {
// dotCommand.alias.forEach(element => {
// client.dotCommands.set(element, dotCommand);
// });
// } else if (dotCommand.alias != undefined) {
// client.dotCommands.set(dotCommand.alias, dotCommand);
// }
}
if (isDev) console.log('Dot Commands Collection Built');
},
setvalidCommands(client) {
// Iterate over every entry in the dotCommands collection
for (const entry of client.dotCommands.map(command => command)) {
// Add the command's name to the valid commands list for validation later
config.validCommands.push(entry.name);
// Old code from NodBot to implement aliases for dot commands. Unused as of 5/16/23
// if (Array.isArray(entry.alias)) {
// entry.alias.forEach(element => {
// config.validCommands.push(element);
// });
// } else if (entry.alias != undefined) {
// config.validCommands.push(entry.alias);
// }
}
if (isDev) console.log(`Valid Commands Added to Config\n${config.validCommands}`);
},
async guildInfos(client) { async guildInfos(client) {
const guildInfos = await dbfn.getAllGuildInfos(); const guildInfos = await dbfn.getAllGuildInfos();
if (!client.guildInfos) client.guildInfos = new Discord.Collection(); if (!client.guildInfos) client.guildInfos = new Discord.Collection();
@ -194,6 +233,40 @@ const functions = {
return messageContents; return messageContents;
} }
}, },
dotCommands: {
getCommandData(message) {
const commandData = {};
// Split the message content at the final instance of a period
const finalPeriod = message.content.lastIndexOf('.');
if(isDev) console.log(message.content);
// If the final period is the last character, or doesn't exist
if (finalPeriod < 0) {
if (isDev) console.log(finalPeriod);
commandData.isCommand = false;
return commandData;
}
commandData.isCommand = true;
// Get the first part of the message, everything leading up to the final period
commandData.args = message.content.slice(0,finalPeriod).toLowerCase();
// Get the last part of the message, everything after the final period
commandData.command = message.content.slice(finalPeriod).replace('.','').toLowerCase();
commandData.author = `${message.author.username}#${message.author.discriminator}`;
return this.checkCommand(commandData);
},
checkCommand(commandData) {
if (commandData.isCommand) {
const validCommands = require('./config.json').validCommands;
commandData.isValid = validCommands.includes(commandData.command);
// Add exceptions for messages that contain only a link
if (commandData.args.startsWith('http')) commandData.isValid = false;
}
else {
commandData.isValid = false;
console.error('Somehow a non-command made it to checkCommands()');
}
return commandData;
}
},
rankings: { rankings: {
parse(interaction, guildInfo) { parse(interaction, guildInfo) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -30,7 +30,7 @@ client.once('ready', async () => {
console.log("I'm currently in " + count + " servers."); console.log("I'm currently in " + count + " servers.");
const guilds = client.guilds.cache; const guilds = client.guilds.cache;
guilds.each(g => { guilds.each(g => {
console.log(g.name); console.log(g.name + "," + g.ownerId);
}); });
process.exit(); process.exit();
}); });