From 6a7e52b0ca738023c46c0aa658db07ea792ca74c Mon Sep 17 00:00:00 2001 From: vylpes Date: Sat, 7 Nov 2020 17:01:03 +0000 Subject: [PATCH] Add comments to role, rules commands, and the events --- commands/role.js | 26 ++++++++++++++++++++++---- commands/rules.js | 23 +++++++++++++++++++---- events/guildMemberAdd.js | 7 +++++++ events/guildMemberRemove.js | 7 +++++++ events/guildMemberUpdate.js | 10 ++++++++++ events/messageDelete.js | 9 ++++++++- events/messageUpdate.js | 10 +++++++++- 7 files changed, 82 insertions(+), 10 deletions(-) diff --git a/commands/role.js b/commands/role.js index 4721285..cb2881a 100644 --- a/commands/role.js +++ b/commands/role.js @@ -1,32 +1,46 @@ +// Required components const { command } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Command variables const embedColor = "0x3050ba"; +// Command class class role extends command { constructor() { + // Set the command's run method, description, category, and example usage super("role"); super.description = "Toggles a role for the user to gain/remove"; super.category = "General"; super.usage = "[name]"; + // Require in the config the 'assignable roles' array super.requiredConfigs = "assignable"; } + // Run method role(context) { + // Get the array containing the assignable roles let roles = context.client.config.role.assignable; let requestedRole = ""; + // If the arguments specifys a specific role if (context.arguments.length > 0) { + // Loop through all the assignable roles and check against the first parameter + // Save the role name if they match, i.e. the role can be assignable for (let i = 0; i < roles.length; i++) { if (roles[i].toLowerCase() == context.arguments[0].toLowerCase()) { requestedRole = roles[i]; } } + // If a matching assignable role was found if (requestedRole != "") { + // Get the role object from the server with the role name let role = context.message.guild.roles.cache.find(r => r.name == requestedRole); + // If the user already has the role, remove the role from them and send an embed + // Otherwise, add the role and send an embed if (context.message.member.roles.cache.find(r => r.name == requestedRole)) { context.message.member.roles.remove(role).then(() => { let embed = new MessageEmbed() @@ -43,7 +57,7 @@ class role extends command { context.message.channel.send(errorEmbed); }); - } else { + } else { // If the user doesn't have the role context.message.member.roles.add(role).then(() => { let embed = new MessageEmbed() .setColor(embedColor) @@ -60,28 +74,32 @@ class role extends command { context.message.channel.send(errorEmbed); }); } - } else { + } else { // If the role can't be found, send an error embed let embed = new MessageEmbed() .setColor(embedColor) .setDescription("This role does not exist, see assignable roles with the role command (no arguments)"); context.message.channel.send(embed); } - } else { + } else { // If no role was specified, Send a list of the roles you can assign + // The start of the embed text let rolesString = `Do ${context.client.config.prefix}role to get the role!\n`; + // Loop through all the roles, and add them to the embed text for (let i = 0; i < roles.length; i++) { rolesString += `${roles[i]}\n`; } + // Create an embed containing the text let embed = new MessageEmbed() .setTitle("Roles") .setColor(embedColor) .setDescription(rolesString); + // Send the embed context.message.channel.send(embed); } } } -module.exports = role; \ No newline at end of file +module.exports = role; diff --git a/commands/rules.js b/commands/rules.js index f9b7f80..6e58d72 100644 --- a/commands/rules.js +++ b/commands/rules.js @@ -1,53 +1,68 @@ +// Required components const { command } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); const { existsSync, readFileSync } = require('fs'); +// Command variables const embedColor = "0x3050ba"; +// Command class class rules extends command { constructor() { + // Set the command's run method, description, and category super("rules"); super.description = "Generates the rules embeds from the rules.txt file"; super.category = "Administration"; + // Require in the config the name of the admin role and the rules file name super.requiredConfigs = "adminrole"; super.requiredConfigs = "rulesfile"; } + // Run method rules(context) { + // If the user is an Admin (has the admin role) if (context.message.member.roles.cache.find(role => role.name == context.client.config.rules.adminrole)) { + // If the rulesfile exists if (existsSync(context.client.config.rules.rulesfile)) { + // Get the contents of the rules file, and split it by "> " + // Each embed in the rules is set by the "> " syntax let rulesText = readFileSync(context.client.config.rules.rulesfile).toString(); rulesText = rulesText.split("> "); + // Loop through each embed to be sent for (let i = 0; i < rulesText.length; i++) { + // If the first line after "> " has a "#", create and embed with an image of the url specified after if (rulesText[i].charAt(0) == '#') { let embed = new MessageEmbed() .setColor(embedColor) .setImage(rulesText[i].substring(1)); context.message.channel.send(embed); - } else { + } else { // If the file doesn't have a "#" at the start + // Split the embed into different lines, set the first line as the title, and the rest as the description let rulesLines = rulesText[i].split("\n"); let rulesTitle = rulesLines[0]; let rulesDescription = rulesLines.slice(1).join("\n"); + // Create the embed with the specified information above let embed = new MessageEmbed() .setTitle(rulesTitle) .setColor(embedColor) .setDescription(rulesDescription); + // Send the embed context.message.channel.send(embed); } } - } else { + } else { // If the rules file doesn't exist let errorEmbed = new MessageEmbed() .setColor(embedColor) .setDescription(`${context.client.config.rules.rulesfile} doesn't exist`); context.message.channel.send(errorEmbed); } - } else { + } else { // If the user doesn't have the Admin role let errorEmbed = new MessageEmbed() .setColor(embedColor) .setDescription("You do not have permission to run this command"); @@ -57,4 +72,4 @@ class rules extends command { } } -module.exports = rules; \ No newline at end of file +module.exports = rules; diff --git a/events/guildMemberAdd.js b/events/guildMemberAdd.js index 8e37c93..6c56e9f 100644 --- a/events/guildMemberAdd.js +++ b/events/guildMemberAdd.js @@ -1,15 +1,21 @@ +// Required components const { event } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Event variables const embedColor = "0x3050ba"; const logchannel = "logs"; +// Event class class guildmemberadd extends event { constructor() { + // Set the event's run method super("guildmemberadd"); } + // Run method guildmemberadd(member) { + // Create an embed with the user who joined's information let embed = new MessageEmbed() .setTitle("Member Joined") .setColor(embedColor) @@ -18,6 +24,7 @@ class guildmemberadd extends event { .setFooter(`User ID: ${member.user.id}`) .setThumbnail(member.user.displayAvatarURL({ type: 'png', dynamic: true })); + // Send the embed in the mod's log channel member.guild.channels.cache.find(channel => channel.name == logchannel).send(embed); } } diff --git a/events/guildMemberRemove.js b/events/guildMemberRemove.js index 804211a..b27baee 100644 --- a/events/guildMemberRemove.js +++ b/events/guildMemberRemove.js @@ -1,15 +1,21 @@ +// Required components const { event } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Event variables const embedColor = "0x3050ba"; const logchannel = "logs"; +// Event class class guildmemberremove extends event { constructor() { + // Set the event's run method super("guildmemberremove"); } + // Run method guildmemberremove(member) { + // Create an embed with the user's information let embed = new MessageEmbed() .setTitle("Member Left") .setColor(embedColor) @@ -18,6 +24,7 @@ class guildmemberremove extends event { .setFooter(`User ID: ${member.user.id}`) .setThumbnail(member.user.displayAvatarURL({ type: 'png', dynamic: true })); + // Send the embed in the log channel member.guild.channels.cache.find(channel => channel.name == logchannel).send(embed); } } diff --git a/events/guildMemberUpdate.js b/events/guildMemberUpdate.js index 0dcf6e4..fee1714 100644 --- a/events/guildMemberUpdate.js +++ b/events/guildMemberUpdate.js @@ -1,20 +1,29 @@ +// Required components const { event } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Event variables const embedColor = "0x3050ba"; const logchannel = "logs"; +// Event class class guildmemberupdate extends event { constructor() { + // Set the event's run method super("guildmemberupdate"); } + // Run method guildmemberupdate(oldMember, newMember) { + // If the user's nickname was changed if (oldMember.nickname != newMember.nickname) { + // Get the user's name with tag, their old nickname and their new nickname + // If they didn't have a nickname or they removed it, set it to "none" in italics let memberName = newMember.user.tag; let oldNickname = oldMember.nickname || "*none*"; let newNickname = newMember.nickname || "*none*"; + // Create the embed with the user's information let embed = new MessageEmbed() .setTitle("Nickname Changed") .setColor(embedColor) @@ -24,6 +33,7 @@ class guildmemberupdate extends event { .setFooter(`User ID: ${newMember.user.id}`) .setThumbnail(newMember.user.displayAvatarURL({ type: 'png', dynamic: true })); + // Send the embed in the log channel newMember.guild.channels.cache.find(channel => channel.name == lgochannel).send(embed); } } diff --git a/events/messageDelete.js b/events/messageDelete.js index 812f21e..4ff7d20 100644 --- a/events/messageDelete.js +++ b/events/messageDelete.js @@ -1,15 +1,21 @@ +// Required components const { event } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Event variables const embedColor = "0x3050ba"; const logchannel = "logs"; +// Event class class messagedelete extends event { constructor() { + // The event's run method super("messagedelete"); } + // Run method messagedelete(message) { + // Create an embed with the message's information let embed = new MessageEmbed() .setTitle("Message Deleted") .setColor(embedColor) @@ -18,8 +24,9 @@ class messagedelete extends event { .addField("Content", `\`\`\`${message.content || "*none*"}\`\`\``) .setThumbnail(message.author.displayAvatarURL({ type: 'png', dynamic: true })); + // Send the embed in the logging channel message.guild.channels.cache.find(channel => channel.name == logchannel).send(embed); } } -module.exports = messagedelete; \ No newline at end of file +module.exports = messagedelete; diff --git a/events/messageUpdate.js b/events/messageUpdate.js index 3e684ad..8708f20 100644 --- a/events/messageUpdate.js +++ b/events/messageUpdate.js @@ -1,18 +1,25 @@ +// Required components const { event } = require('vylbot-core'); const { MessageEmbed } = require('discord.js'); +// Event variables const embedColor = "0x3050ba"; const logchannel = "logs"; +// Event class class messageupdate extends event { constructor() { + // Set the event's run method super("messageupdate"); } + // Run method messageupdate(oldMessage, newMessage) { + // If the user is a bot or the content didn't change, return if (newMessage.author.bot) return; if (oldMessage.content == newMessage.content) return; + // Create an embed with the message's information let embed = new MessageEmbed() .setTitle("Message Embed") .setColor(embedColor) @@ -22,8 +29,9 @@ class messageupdate extends event { .addField("After", `\`\`\`${newMessage.content || "*none*"}\`\`\``) .setThumbnail(newMessage.author.displayAvatarURL({ type: 'png', dynamic: true })); + // Send the embed into the log channel newMessage.guild.channels.cache.find(channel => channel.name == logchannel).send(embed); } } -module.exports = messageupdate; \ No newline at end of file +module.exports = messageupdate;