Add comments to role, rules commands, and the events

This commit is contained in:
vylpes 2020-11-07 17:01:03 +00:00
parent 2e13b4d861
commit 6a7e52b0ca
7 changed files with 82 additions and 10 deletions

View file

@ -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,25 +74,29 @@ 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 <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);
}
}

View file

@ -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");

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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,6 +24,7 @@ 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);
}
}

View file

@ -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,6 +29,7 @@ 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);
}
}