first commit

This commit is contained in:
Raven Scott
2022-01-24 19:05:30 +00:00
commit 390fff9c42
31 changed files with 10171 additions and 0 deletions

4
events/error.js Normal file
View File

@ -0,0 +1,4 @@
const logger = require("../modules/Logger.js");
module.exports = async (client, error) => {
logger.log(`An error event was sent by Discord.js: \n${JSON.stringify(error)}`, "error");
};

6
events/guildCreate.js Normal file
View File

@ -0,0 +1,6 @@
const logger = require("../modules/Logger.js");
// This event executes when a new guild (server) is joined.
module.exports = (client, guild) => {
logger.log(`[GUILD JOIN] ${guild.id} added the bot. Owner: ${guild.ownerId}`);
};

16
events/guildDelete.js Normal file
View File

@ -0,0 +1,16 @@
const logger = require("../modules/Logger.js");
const { settings } = require("../modules/settings.js");
// This event executes when a new guild (server) is left.
module.exports = (client, guild) => {
if (!guild.available) return; // If there is an outage, return.
logger.log(`[GUILD LEAVE] ${guild.id} removed the bot.`);
// If the settings Enmap contains any guild overrides, remove them.
// No use keeping stale data!
if (settings.has(guild.id)) {
settings.delete(guild.id);
}
};

17
events/guildMemberAdd.js Normal file
View File

@ -0,0 +1,17 @@
const { getSettings } = require("../modules/functions.js");
// This event executes when a new member joins a server. Let's welcome them!
module.exports = (client, member) => {
// Load the guild's settings
const settings = getSettings(member.guild);
// If welcome is off, don't proceed (don't welcome the user)
if (settings.welcomeEnabled !== "true") return;
// Replace the placeholders in the welcome message with actual data
const welcomeMessage = settings.welcomeMessage.replace("{{user}}", member.user.tag);
// Send the welcome message to the welcome channel
// There's a place for more configs here.
member.guild.channels.cache.find(c => c.name === settings.welcomeChannel).send(welcomeMessage).catch(console.error);
};

View File

@ -0,0 +1,54 @@
const logger = require("../modules/Logger.js");
const { getSettings, permlevel } = require("../modules/functions.js");
const config = require("../config.js");
module.exports = async (client, interaction) => {
// If it's not a command, stop.
if (!interaction.isCommand()) return;
// Grab the settings for this server from Enmap.
// If there is no guild, get default conf (DMs)
const settings = interaction.settings = getSettings(interaction.guild);
// Get the user or member's permission level from the elevation
const level = permlevel(interaction);
// Grab the command data from the client.container.slashcmds Collection
const cmd = client.container.slashcmds.get(interaction.commandName);
// If that command doesn't exist, silently exit and do nothing
if (!cmd) return;
// Since the permission system from Discord is rather limited in regarding to
// Slash Commands, we'll just utilise our permission checker.
if (level < client.container.levelCache[cmd.conf.permLevel]) {
// Due to the nature of interactions we **must** respond to them otherwise
// they will error out because we didn't respond to them.
return await interaction.reply({
content: `This command can only be used by ${cmd.conf.permLevel}'s only`,
// This will basically set the ephemeral response to either announce
// to everyone, or just the command executioner. But we **HAVE** to
// respond.
ephemeral: settings.systemNotice !== "true"
});
}
// If everything checks out, run the command
try {
await cmd.run(client, interaction);
logger.log(`${config.permLevels.find(l => l.level === level).name} ${interaction.user.id} ran slash command ${interaction.commandName}`, "cmd");
} catch (e) {
console.error(e);
if (interaction.replied)
interaction.followUp({ content: `There was a problem with your request.\n\`\`\`${e.message}\`\`\``, ephemeral: true })
.catch(e => console.error("An error occurred following up on an error", e));
else
if (interaction.deferred)
interaction.editReply({ content: `There was a problem with your request.\n\`\`\`${e.message}\`\`\``, ephemeral: true })
.catch(e => console.error("An error occurred following up on an error", e));
else
interaction.reply({ content: `There was a problem with your request.\n\`\`\`${e.message}\`\`\``, ephemeral: true })
.catch(e => console.error("An error occurred replying on an error", e));
}
};

90
events/messageCreate.js Normal file
View File

@ -0,0 +1,90 @@
const logger = require("../modules/Logger.js");
const { getSettings, permlevel } = require("../modules/functions.js");
const config = require("../config.js");
// The MESSAGE event runs anytime a message is received
// Note that due to the binding of client to every event, every event
// goes `client, other, args` when this function is run.
module.exports = async (client, message) => {
// Grab the container from the client to reduce line length.
const { container } = client;
// It's good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that "botception").
if (message.author.bot) return;
// Grab the settings for this server from Enmap.
// If there is no guild, get default conf (DMs)
const settings = message.settings = getSettings(message.guild);
// Checks if the bot was mentioned via regex, with no message after it,
// returns the prefix. The reason why we used regex here instead of
// message.mentions is because of the mention prefix later on in the
// code, would render it useless.
const prefixMention = new RegExp(`^<@!?${client.user.id}> ?$`);
if (message.content.match(prefixMention)) {
return message.reply(`My prefix on this guild is \`${settings.prefix}\``);
}
// It's also good practice to ignore any and all messages that do not start
// with our prefix, or a bot mention.
const prefix = new RegExp(`^<@!?${client.user.id}> |^\\${settings.prefix}`).exec(message.content);
// This will return and stop the code from continuing if it's missing
// our prefix (be it mention or from the settings).
if (!prefix) return;
// Here we separate our "command" name, and our "arguments" for the command.
// e.g. if we have the message "+say Is this the real life?" , we'll get the following:
// command = say
// args = ["Is", "this", "the", "real", "life?"]
const args = message.content.slice(prefix[0].length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// If the member on a guild is invisible or not cached, fetch them.
if (message.guild && !message.member) await message.guild.members.fetch(message.author);
// Get the user or member's permission level from the elevation
const level = permlevel(message);
// Check whether the command, or alias, exist in the collections defined
// in app.js.
const cmd = container.commands.get(command) || container.commands.get(container.aliases.get(command));
// using this const varName = thing OR otherThing; is a pretty efficient
// and clean way to grab one of 2 values!
if (!cmd) return;
// Some commands may not be useable in DMs. This check prevents those commands from running
// and return a friendly error message.
if (cmd && !message.guild && cmd.conf.guildOnly)
return message.channel.send("This command is unavailable via private message. Please run this command in a guild.");
if (!cmd.conf.enabled) return;
if (level < container.levelCache[cmd.conf.permLevel]) {
if (settings.systemNotice === "true") {
return message.channel.send(`You do not have permission to use this command.
Your permission level is ${level} (${config.permLevels.find(l => l.level === level).name})
This command requires level ${container.levelCache[cmd.conf.permLevel]} (${cmd.conf.permLevel})`);
} else {
return;
}
}
// To simplify message arguments, the author's level is now put on level (not member so it is supported in DMs)
// The "level" command module argument will be deprecated in the future.
message.author.permLevel = level;
message.flags = [];
while (args[0] && args[0][0] === "-") {
message.flags.push(args.shift().slice(1));
}
// If the command exists, **AND** the user has permission, run it.
try {
await cmd.run(client, message, args, level);
logger.log(`${config.permLevels.find(l => l.level === level).name} ${message.author.id} ran command ${cmd.help.name}`, "cmd");
} catch (e) {
console.error(e);
message.channel.send({ content: `There was a problem with your request.\n\`\`\`${e.message}\`\`\`` })
.catch(e => console.error("An error occurred replying on an error", e));
}
};

9
events/ready.js Normal file
View File

@ -0,0 +1,9 @@
const logger = require("../modules/Logger.js");
const { getSettings } = require("../modules/functions.js");
module.exports = async client => {
// Log that the bot is online.
logger.log(`${client.user.tag}, ready to serve ${client.guilds.cache.map(g => g.memberCount).reduce((a, b) => a + b)} users in ${client.guilds.cache.size} servers.`, "ready");
// Make the bot "play the game" which is the help command with default prefix.
client.user.setActivity(`${getSettings("default").prefix}help`, { type: "PLAYING" });
};