From 44571d735a456c4e912dabcfdf653dd17d643959 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 2 Dec 2021 15:38:38 +0000 Subject: [PATCH] Migrate events to typescript --- events/guildMemberAdd.js | 32 ------------ events/guildMemberRemove.js | 32 ------------ events/guildMemberUpdate.js | 41 --------------- events/messageDelete.js | 32 ------------ package.json | 2 +- src/events/MemberEvents.ts | 36 +++++++++++++ src/events/MemberEvents/GuildMemberUpdate.ts | 25 +++++++++ src/events/MessageEvents.ts | 35 +++++++++++++ src/helpers/EventEmbed.ts | 54 ++++++++++++++++++++ yarn.lock | 8 +-- 10 files changed, 155 insertions(+), 142 deletions(-) delete mode 100644 events/guildMemberAdd.js delete mode 100644 events/guildMemberRemove.js delete mode 100644 events/guildMemberUpdate.js delete mode 100644 events/messageDelete.js create mode 100644 src/events/MemberEvents.ts create mode 100644 src/events/MemberEvents/GuildMemberUpdate.ts create mode 100644 src/events/MessageEvents.ts create mode 100644 src/helpers/EventEmbed.ts diff --git a/events/guildMemberAdd.js b/events/guildMemberAdd.js deleted file mode 100644 index 7b57581..0000000 --- a/events/guildMemberAdd.js +++ /dev/null @@ -1,32 +0,0 @@ -// Required components -const { event } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -// Event variables -const embedColor = "0x3050ba"; -const logchannel = "member-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 - const embed = new MessageEmbed() - .setTitle("Member Joined") - .setColor(embedColor) - .addField("User", `${member} \`${member.user.tag}\``) - .addField("Created", `${member.user.createdAt}`) - .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); - } -} - -module.exports = guildmemberadd; diff --git a/events/guildMemberRemove.js b/events/guildMemberRemove.js deleted file mode 100644 index ed85ef2..0000000 --- a/events/guildMemberRemove.js +++ /dev/null @@ -1,32 +0,0 @@ -// Required components -const { event } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -// Event variables -const embedColor = "0x3050ba"; -const logchannel = "member-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 - const embed = new MessageEmbed() - .setTitle("Member Left") - .setColor(embedColor) - .addField("User", `${member} \`${member.user.tag}\``) - .addField("Joined", `${member.joinedAt}`) - .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); - } -} - -module.exports = guildmemberremove; diff --git a/events/guildMemberUpdate.js b/events/guildMemberUpdate.js deleted file mode 100644 index 806903c..0000000 --- a/events/guildMemberUpdate.js +++ /dev/null @@ -1,41 +0,0 @@ -// Required components -const { event } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -// Event variables -const embedColor = "0x3050ba"; -const logchannel = "member-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 - const oldNickname = oldMember.nickname || "*none*"; - const newNickname = newMember.nickname || "*none*"; - - // Create the embed with the user's information - const embed = new MessageEmbed() - .setTitle("Nickname Changed") - .setColor(embedColor) - .addField("User", `${newMember} \`${newMember.user.tag}\``) - .addField("Before", oldNickname, true) - .addField("After", newNickname, true) - .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 == logchannel).send(embed); - } - } -} - -module.exports = guildmemberupdate; diff --git a/events/messageDelete.js b/events/messageDelete.js deleted file mode 100644 index 19fea4a..0000000 --- a/events/messageDelete.js +++ /dev/null @@ -1,32 +0,0 @@ -// Required components -const { event } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -// Event variables -const embedColor = "0x3050ba"; -const logchannel = "message-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 - const embed = new MessageEmbed() - .setTitle("Message Deleted") - .setColor(embedColor) - .addField("User", `${message.author} \`${message.author.tag}\``) - .addField("Channel", message.channel) - .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; diff --git a/package.json b/package.json index a032aeb..6874877 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "dotenv": "^10.0.0", "emoji-regex": "^9.2.0", "random-bunny": "^2.0.0", - "vylbot-core": "^2.0.2" + "vylbot-core": "^2.0.3" }, "devDependencies": { "@types/node": "^16.11.10", diff --git a/src/events/MemberEvents.ts b/src/events/MemberEvents.ts new file mode 100644 index 0000000..696fa11 --- /dev/null +++ b/src/events/MemberEvents.ts @@ -0,0 +1,36 @@ +import { Event } from "vylbot-core"; +import { GuildMember } from "discord.js"; +import EventEmbed from "../helpers/EventEmbed"; +import GuildMemberUpdate from "./MemberEvents/GuildMemberUpdate"; + +export default class MemberEvents extends Event { + constructor() { + super(); + } + + public override guildMemberAdd(member: GuildMember) { + const embed = new EventEmbed(member.guild, "Member Joined"); + embed.AddUser("User", member.user, true); + embed.addField("Created", member.user.createdAt); + embed.setFooter(`Id: ${member.user.id}`); + + embed.SendToMemberLogsChannel(); + } + + public override guildMemberRemove(member: GuildMember) { + const embed = new EventEmbed(member.guild, "Member Left"); + embed.AddUser("User", member.user, true); + embed.addField("Joined", member.joinedAt); + embed.setFooter(`Id: ${member.user.id}`); + + embed.SendToMemberLogsChannel(); + } + + public override guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember) { + const handler = new GuildMemberUpdate(oldMember, newMember); + + if (oldMember.nickname != newMember.nickname) { // Nickname change + handler.NicknameChanged(); + } + } +} \ No newline at end of file diff --git a/src/events/MemberEvents/GuildMemberUpdate.ts b/src/events/MemberEvents/GuildMemberUpdate.ts new file mode 100644 index 0000000..ef2c530 --- /dev/null +++ b/src/events/MemberEvents/GuildMemberUpdate.ts @@ -0,0 +1,25 @@ +import { GuildMember } from "discord.js"; +import EventEmbed from "../../helpers/EventEmbed"; + +export default class GuildMemberUpdate { + private _oldMember: GuildMember; + private _newMember: GuildMember; + + constructor(oldMember: GuildMember, newMember: GuildMember) { + this._oldMember = oldMember; + this._newMember = newMember; + } + + public NicknameChanged() { + const oldNickname = this._oldMember.nickname || "*none*"; + const newNickname = this._newMember.nickname || "*none*"; + + const embed = new EventEmbed(this._newMember.guild, "Nickname Changed"); + embed.AddUser("User", this._newMember.user, true); + embed.addField("Before", oldNickname, true); + embed.addField("After", newNickname, true); + embed.setFooter(`Id: ${this._newMember.user.id}`); + + embed.SendToMemberLogsChannel(); + } +} \ No newline at end of file diff --git a/src/events/MessageEvents.ts b/src/events/MessageEvents.ts new file mode 100644 index 0000000..0d449ec --- /dev/null +++ b/src/events/MessageEvents.ts @@ -0,0 +1,35 @@ +import { Event } from "vylbot-core"; +import { Message } from "discord.js"; +import EventEmbed from "../helpers/EventEmbed"; + +export default class MessageEvents extends Event { + constructor() { + super(); + } + + public override messageDelete(message: Message) { + if (!message.guild) return; + + const embed = new EventEmbed(message.guild, "Message Deleted"); + embed.AddUser("User", message.author, true); + embed.addField("Channel", message.channel, true); + embed.addField("Content", `\`\`\`${message.content || "*none*"}\`\`\``); + embed.addField("Attachments", `\`\`\`${message.attachments.map(x => x.url).join("\n")}`); + + embed.SendToMessageLogsChannel(); + } + + public override messageUpdate(oldMessage: Message, newMessage: Message) { + if (!newMessage.guild) return; + if (newMessage.author.bot) return; + if (oldMessage.content == newMessage.content) return; + + const embed = new EventEmbed(newMessage.guild, "Message Edited"); + embed.AddUser("User", newMessage.author, true); + embed.addField("Channel", newMessage.channel, true); + embed.addField("Before", `\`\`\`${oldMessage.content || "*none*"}\`\`\``); + embed.addField("After", `\`\`\`${newMessage.content || "*none*"}\`\`\``); + + embed.SendToMessageLogsChannel(); + } +} \ No newline at end of file diff --git a/src/helpers/EventEmbed.ts b/src/helpers/EventEmbed.ts new file mode 100644 index 0000000..25141d8 --- /dev/null +++ b/src/helpers/EventEmbed.ts @@ -0,0 +1,54 @@ +import { MessageEmbed, TextChannel, User, Guild } from "discord.js"; +import ErrorMessages from "../constants/ErrorMessages"; +import ErrorEmbed from "./ErrorEmbed"; + +export default class EventEmbed extends MessageEmbed { + private _guild: Guild; + + constructor(guild: Guild, title: string) { + super(); + + super.setColor(process.env.EMBED_COLOUR!); + super.setTitle(title); + + this._guild = guild; + } + + // Detail methods + public AddUser(title: string, user: User, setThumbnail: boolean = false) { + super.addField(title, `${user} \`${user.tag}\``, true); + + if (setThumbnail) { + super.setThumbnail(user.displayAvatarURL()); + } + } + + public AddReason(message: String) { + super.addField("Reason", message || "*none*"); + } + + // Send methods + public SendToChannel(name: string) { + const channel = this._guild.channels.cache + .find(channel => channel.name == name) as TextChannel; + + if (!channel) { + console.error(`Unable to find channel ${name}`); + return; + } + + channel.send(this); + } + + public SendToMessageLogsChannel() { + this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!) + } + + public SendToMemberLogsChannel() { + this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!) + } + + public SendToModLogsChannel() { + this.SendToChannel(process.env.CHANNELS_LOGS_MOD!) + } +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 011876d..1b8040a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1131,10 +1131,10 @@ v8-compile-cache@^2.0.3: resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee" integrity sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA== -vylbot-core@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/vylbot-core/-/vylbot-core-2.0.2.tgz#8f83b836b423e9f0812abb86368672716dc1fa75" - integrity sha512-Og+cdPG+ExjzjhqVDb1hac7pm6KfLvT85NMBoL7hfbSPsg11wFwLKI6toqVnbvbqMKzKnOpIvijo4jx+fdUDsA== +vylbot-core@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/vylbot-core/-/vylbot-core-2.0.3.tgz#e7d844bc54e72b14f06f14b63866854e3b846d55" + integrity sha512-RxABRcwhVIVZcIssVP8yosLlUXzcdrNLiYJzcvkAIz4KeHfoARcxjsz2vG2x9GIzeFpfn/hjrPvjFC5lN6zclw== dependencies: discord.js "^12.3.1" dotenv "^10.0.0"