Feature/103 improve events #201

Merged
Vylpes merged 2 commits from feature/103-improve-events into develop 2022-10-09 15:23:25 +01:00
14 changed files with 297 additions and 370 deletions
Showing only changes of commit 0ea50546bc - Show all commits

View file

@ -5,7 +5,6 @@ import { EventType } from "../constants/EventType";
import ICommandItem from "../contracts/ICommandItem"; import ICommandItem from "../contracts/ICommandItem";
import IEventItem from "../contracts/IEventItem"; import IEventItem from "../contracts/IEventItem";
import { Command } from "../type/command"; import { Command } from "../type/command";
import { Event } from "../type/event";
import { Events } from "./events"; import { Events } from "./events";
import { Util } from "./util"; import { Util } from "./util";

View file

@ -1,81 +0,0 @@
import { Event } from "../type/event";
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js";
import GuildMemberUpdate from "./MemberEvents/GuildMemberUpdate";
import SettingsHelper from "../helpers/SettingsHelper";
import EmbedColours from "../constants/EmbedColours";
export default class MemberEvents extends Event {
constructor() {
super();
}
public override async guildMemberAdd(member: GuildMember) {
if (!member.guild) return;
const enabled = await SettingsHelper.GetSetting("event.member.add.enabled", member.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Member Joined')
.setDescription(`${member.user} \`${member.user.tag}\``)
.setFooter({ text: `Id: ${member.user.id}` })
.addFields([
{
name: 'Created',
value: member.user.createdAt.toISOString(),
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.add.channel", member.guild.id);
if (!channelSetting) return;
const channel = member.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
}
public override async guildMemberRemove(member: GuildMember) {
if (!member.guild) return;
const enabled = await SettingsHelper.GetSetting("event.member.remove.enabled", member.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Member Left')
.setDescription(`${member.user} \`${member.user.tag}\``)
.setFooter({ text: `Id: ${member.user.id}` })
.addFields([
{
name: 'Joined',
value: member.joinedAt ? member.joinedAt.toISOString() : "*none*",
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.remove.channel", member.guild.id);
if (!channelSetting) return;
const channel = member.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
}
public override async guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember) {
const handler = new GuildMemberUpdate(oldMember, newMember);
if (oldMember.nickname != newMember.nickname) { // Nickname change
await handler.NicknameChanged();
}
}
}

View file

@ -0,0 +1,34 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js";
import EmbedColours from "../../constants/EmbedColours";
import SettingsHelper from "../../helpers/SettingsHelper";
export default async function GuildMemberAdd(member: GuildMember) {
if (!member.guild) return;
const enabled = await SettingsHelper.GetSetting("event.member.add.enabled", member.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Member Joined')
.setDescription(`${member.user} \`${member.user.tag}\``)
.setFooter({ text: `Id: ${member.user.id}` })
.addFields([
{
name: 'Created',
value: member.user.createdAt.toISOString(),
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.add.channel", member.guild.id);
if (!channelSetting) return;
const channel = member.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
}

View file

@ -0,0 +1,34 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js";
import EmbedColours from "../../constants/EmbedColours";
import SettingsHelper from "../../helpers/SettingsHelper";
export default async function GuildMemberRemove(member: GuildMember) {
if (!member.guild) return;
const enabled = await SettingsHelper.GetSetting("event.member.remove.enabled", member.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Member Left')
.setDescription(`${member.user} \`${member.user.tag}\``)
.setFooter({ text: `Id: ${member.user.id}` })
.addFields([
{
name: 'Joined',
value: member.joinedAt ? member.joinedAt.toISOString() : "*none*",
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.remove.channel", member.guild.id);
if (!channelSetting) return;
const channel = member.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
}

View file

@ -1,49 +1,8 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js"; import { GuildMember } from "discord.js";
import EmbedColours from "../../constants/EmbedColours"; import NicknameChanged from "./GuildMemberUpdate/NicknameChanged";
import SettingsHelper from "../../helpers/SettingsHelper";
export default class GuildMemberUpdate { export default async function GuildMemberUpdate(oldMember: GuildMember, newMember: GuildMember) {
public oldMember: GuildMember; if (oldMember.nickname != newMember.nickname) { // Nickname change
public newMember: GuildMember; await NicknameChanged(oldMember, newMember);
constructor(oldMember: GuildMember, newMember: GuildMember) {
this.oldMember = oldMember;
this.newMember = newMember;
}
public async NicknameChanged() {
const enabled = await SettingsHelper.GetSetting("event.member.update.enabled", this.newMember.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const oldNickname = this.oldMember.nickname || "*none*";
const newNickname = this.newMember.nickname || "*none*";
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Nickname Changed')
.setDescription(`${this.newMember.user} \`${this.newMember.user.tag}\``)
.setFooter({ text: `Id: ${this.newMember.user.id}` })
.addFields([
{
name: 'Before',
value: oldNickname,
},
{
name: 'After',
value: newNickname,
},
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.update.channel", this.newMember.guild.id);
if (!channelSetting) return;
const channel = this.newMember.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
} }
} }

View file

@ -0,0 +1,39 @@
import { EmbedBuilder, GuildMember, TextChannel } from "discord.js";
import EmbedColours from "../../../constants/EmbedColours";
import SettingsHelper from "../../../helpers/SettingsHelper";
export default async function NicknameChanged(oldMember: GuildMember, newMember: GuildMember) {
const enabled = await SettingsHelper.GetSetting("event.member.update.enabled", newMember.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const oldNickname = oldMember.nickname || "*none*";
const newNickname = newMember.nickname || "*none*";
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle('Nickname Changed')
.setDescription(`${newMember.user} \`${newMember.user.tag}\``)
.setFooter({ text: `Id: ${newMember.user.id}` })
.addFields([
{
name: 'Before',
value: oldNickname,
},
{
name: 'After',
value: newNickname,
},
]);
const channelSetting = await SettingsHelper.GetSetting("event.member.update.channel", newMember.guild.id);
if (!channelSetting) return;
const channel = newMember.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [embed ]});
}

View file

@ -1,115 +0,0 @@
import { Event } from "../type/event";
import { EmbedBuilder, Message, TextChannel } from "discord.js";
import SettingsHelper from "../helpers/SettingsHelper";
import OnMessage from "./MessageEvents/OnMessage";
import IgnoredChannel from "../entity/IgnoredChannel";
import EmbedColours from "../constants/EmbedColours";
export default class MessageEvents extends Event {
constructor() {
super();
}
public override async messageDelete(message: Message) {
if (!message.guild) return;
if (message.author.bot) return;
const enabled = await SettingsHelper.GetSetting("event.message.delete.enabled", message.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const ignored = await IgnoredChannel.IsChannelIgnored(message.channel.id);
if (ignored) return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Message Deleted")
.setDescription(`${message.author} \`${message.author.tag}\``)
.addFields([
{
name: "Channel",
value: message.channel.toString(),
inline: true,
},
{
name: "Content",
value: `\`\`\`${message.content || "*none*"}\`\`\``,
}
]);
if (message.attachments.size > 0) {
embed.addFields([
{
name: "Attachments",
value: `\`\`\`${message.attachments.map(x => x.url).join("\n")}\`\`\``
}
]);
}
const channelSetting = await SettingsHelper.GetSetting("event.message.delete.channel", message.guild.id);
if (!channelSetting) return;
const channel = message.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [ embed ]});
}
public override async messageUpdate(oldMessage: Message, newMessage: Message) {
if (!newMessage.guild) return;
if (newMessage.author.bot) return;
if (oldMessage.content == newMessage.content) return;
const enabled = await SettingsHelper.GetSetting("event.message.update.enabled", newMessage.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const ignored = await IgnoredChannel.IsChannelIgnored(newMessage.channel.id);
if (ignored) return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Message Edited")
.setDescription(`${newMessage.author} \`${newMessage.author.tag}\``)
.addFields([
{
name: "Channel",
value: newMessage.channel.toString(),
inline: true,
},
{
name: "Before",
value: `\`\`\`${oldMessage.content || "*none*"}\`\`\``,
},
{
name: "After",
value: `\`\`\`${newMessage.content || "*none*"}\`\`\``,
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.message.delete.channel", newMessage.guild.id);
if (!channelSetting) return;
const channel = newMessage.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [ embed ]});
}
public override async messageCreate(message: Message) {
if (!message.guild) return;
if (message.author.bot) return;
const isVerificationEnabled = await SettingsHelper.GetSetting("verification.enabled", message.guild.id);
if (isVerificationEnabled && isVerificationEnabled.toLocaleLowerCase() == "true") {
await OnMessage.VerificationCheck(message);
}
}
}

View file

@ -0,0 +1,14 @@
import { Message } from "discord.js";
import SettingsHelper from "../../helpers/SettingsHelper";
import VerificationCheck from "./MessageCreate/VerificationCheck";
export default async function MessageCreate(message: Message) {
if (!message.guild) return;
if (message.author.bot) return;
const isVerificationEnabled = await SettingsHelper.GetSetting("verification.enabled", message.guild.id);
if (isVerificationEnabled && isVerificationEnabled.toLocaleLowerCase() == "true") {
await VerificationCheck(message);
}
}

View file

@ -0,0 +1,57 @@
import { Message } from "discord.js";
import SettingsHelper from "../../../helpers/SettingsHelper";
export default async function VerificationCheck(message: Message) {
if (!message.guild) return;
const verificationChannel = await SettingsHelper.GetSetting("verification.channel", message.guild.id);
if (!verificationChannel) {
return;
}
const channel = message.guild.channels.cache.find(x => x.name == verificationChannel);
if (!channel) {
return;
}
const currentChannel = message.guild.channels.cache.find(x => x == message.channel);
if (!currentChannel || currentChannel.name != verificationChannel) {
return;
}
const verificationCode = await SettingsHelper.GetSetting("verification.code", message.guild.id);
if (!verificationCode || verificationCode == "") {
await message.reply("`verification.code` is not set inside of the server's config. Please contact the server's mod team.");
await message.delete();
return;
}
const verificationRoleName = await SettingsHelper.GetSetting("verification.role", message.guild.id);
if (!verificationRoleName) {
await message.reply("`verification.role` is not set inside of the server's config. Please contact the server's mod team.");
await message.delete();
return;
}
const role = message.guild.roles.cache.find(x => x.name == verificationRoleName);
if (!role) {
await message.reply("The entry role configured for this server does not exist. Please contact the server's mod team.");
await message.delete();
return;
}
if (message.content.toLocaleLowerCase() != verificationCode.toLocaleLowerCase()) {
await message.delete();
return;
}
await message.member?.roles.add(role);
await message.delete();
}

View file

@ -0,0 +1,52 @@
import { EmbedBuilder, Message, TextChannel } from "discord.js";
import EmbedColours from "../../constants/EmbedColours";
import IgnoredChannel from "../../entity/IgnoredChannel";
import SettingsHelper from "../../helpers/SettingsHelper";
export default async function MessageDelete(message: Message) {
if (!message.guild) return;
if (message.author.bot) return;
const enabled = await SettingsHelper.GetSetting("event.message.delete.enabled", message.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const ignored = await IgnoredChannel.IsChannelIgnored(message.channel.id);
if (ignored) return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Message Deleted")
.setDescription(`${message.author} \`${message.author.tag}\``)
.addFields([
{
name: "Channel",
value: message.channel.toString(),
inline: true,
},
{
name: "Content",
value: `\`\`\`${message.content || "*none*"}\`\`\``,
}
]);
if (message.attachments.size > 0) {
embed.addFields([
{
name: "Attachments",
value: `\`\`\`${message.attachments.map(x => x.url).join("\n")}\`\`\``
}
]);
}
const channelSetting = await SettingsHelper.GetSetting("event.message.delete.channel", message.guild.id);
if (!channelSetting) return;
const channel = message.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [ embed ]});
}

View file

@ -0,0 +1,48 @@
import { EmbedBuilder, Message, TextChannel } from "discord.js";
import EmbedColours from "../../constants/EmbedColours";
import IgnoredChannel from "../../entity/IgnoredChannel";
import SettingsHelper from "../../helpers/SettingsHelper";
export default async function MessageUpdate(oldMessage: Message, newMessage: Message) {
if (!newMessage.guild) return;
if (newMessage.author.bot) return;
if (oldMessage.content == newMessage.content) return;
const enabled = await SettingsHelper.GetSetting("event.message.update.enabled", newMessage.guild.id);
if (!enabled || enabled.toLowerCase() != "true") return;
const ignored = await IgnoredChannel.IsChannelIgnored(newMessage.channel.id);
if (ignored) return;
const embed = new EmbedBuilder()
.setColor(EmbedColours.Ok)
.setTitle("Message Edited")
.setDescription(`${newMessage.author} \`${newMessage.author.tag}\``)
.addFields([
{
name: "Channel",
value: newMessage.channel.toString(),
inline: true,
},
{
name: "Before",
value: `\`\`\`${oldMessage.content || "*none*"}\`\`\``,
},
{
name: "After",
value: `\`\`\`${newMessage.content || "*none*"}\`\`\``,
}
]);
const channelSetting = await SettingsHelper.GetSetting("event.message.delete.channel", newMessage.guild.id);
if (!channelSetting) return;
const channel = newMessage.guild.channels.cache.find(x => x.name == channelSetting);
if (!channel) return;
const guildChannel = channel as TextChannel;
await guildChannel.send({ embeds: [ embed ]});
}

View file

@ -1,59 +0,0 @@
import { Message as Message } from "discord.js";
import SettingsHelper from "../../helpers/SettingsHelper";
export default class OnMessage {
public static async VerificationCheck(message: Message) {
if (!message.guild) return;
const verificationChannel = await SettingsHelper.GetSetting("verification.channel", message.guild.id);
if (!verificationChannel) {
return;
}
const channel = message.guild.channels.cache.find(x => x.name == verificationChannel);
if (!channel) {
return;
}
const currentChannel = message.guild.channels.cache.find(x => x == message.channel);
if (!currentChannel || currentChannel.name != verificationChannel) {
return;
}
const verificationCode = await SettingsHelper.GetSetting("verification.code", message.guild.id);
if (!verificationCode || verificationCode == "") {
await message.reply("`verification.code` is not set inside of the server's config. Please contact the server's mod team.");
await message.delete();
return;
}
const verificationRoleName = await SettingsHelper.GetSetting("verification.role", message.guild.id);
if (!verificationRoleName) {
await message.reply("`verification.role` is not set inside of the server's config. Please contact the server's mod team.");
await message.delete();
return;
}
const role = message.guild.roles.cache.find(x => x.name == verificationRoleName);
if (!role) {
await message.reply("The entry role configured for this server does not exist. Please contact the server's mod team.");
await message.delete();
return;
}
if (message.content.toLocaleLowerCase() != verificationCode.toLocaleLowerCase()) {
await message.delete();
return;
}
await message.member?.roles.add(role);
await message.delete();
}
}

View file

@ -1,9 +1,11 @@
import { CoreClient } from "./client/client"; import { CoreClient } from "./client/client";
import { EventType } from "./constants/EventType";
// Command Imports // Command Imports
import About from "./commands/about"; import About from "./commands/about";
import Audits from "./commands/audits"; import Audits from "./commands/audits";
import Ban from "./commands/ban"; import Ban from "./commands/ban";
import Bunny from "./commands/bunny";
import Clear from "./commands/clear"; import Clear from "./commands/clear";
import Code from "./commands/code"; import Code from "./commands/code";
import Config from "./commands/config"; import Config from "./commands/config";
@ -25,10 +27,12 @@ import AddLobby from "./commands/501231711271780357/Lobby/add";
import RemoveLobby from "./commands/501231711271780357/Lobby/remove"; import RemoveLobby from "./commands/501231711271780357/Lobby/remove";
// Event Imports // Event Imports
import MemberEvents from "./events/MemberEvents"; import GuildMemberAdd from "./events/MemberEvents/GuildMemberAdd";
import MessageEvents from "./events/MessageEvents"; import GuildMemberRemove from "./events/MemberEvents/GuildMemberRemove";
import Bunny from "./commands/bunny"; import GuildMemberUpdate from "./events/MemberEvents/GuildMemberUpdate";
import { EventType } from "./constants/EventType"; import MessageDelete from "./events/MessageEvents/MessageDelete";
import MessageUpdate from "./events/MessageEvents/MessageUpdate";
import MessageCreate from "./events/MessageEvents/MessageCreate";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
@ -65,15 +69,12 @@ export default class Registry {
} }
public static RegisterEvents() { public static RegisterEvents() {
const memberEvents = new MemberEvents(); CoreClient.RegisterEvent(EventType.GuildMemberAdd, GuildMemberAdd);
const messageEvents = new MessageEvents(); CoreClient.RegisterEvent(EventType.GuildMemberRemove, GuildMemberRemove);
CoreClient.RegisterEvent(EventType.GuildMemberUpdate, GuildMemberUpdate);
CoreClient.RegisterEvent(EventType.GuildMemberAdd, memberEvents.guildMemberAdd); CoreClient.RegisterEvent(EventType.MessageDelete, MessageDelete);
CoreClient.RegisterEvent(EventType.GuildMemberRemove, memberEvents.guildMemberRemove); CoreClient.RegisterEvent(EventType.MessageUpdate, MessageUpdate);
CoreClient.RegisterEvent(EventType.GuildMemberUpdate, memberEvents.guildMemberUpdate); CoreClient.RegisterEvent(EventType.MessageCreate, MessageCreate);
CoreClient.RegisterEvent(EventType.MessageDelete, messageEvents.messageDelete);
CoreClient.RegisterEvent(EventType.MessageUpdate, messageEvents.messageUpdate);
CoreClient.RegisterEvent(EventType.MessageCreate, messageEvents.messageCreate);
} }
} }

View file

@ -1,55 +0,0 @@
import { Channel, Guild, GuildMember, Message, PartialDMChannel, PartialGuildMember, PartialMessage, GuildBan } from "discord.js";
export class Event {
public channelCreate(channel: Channel) {
}
public channelDelete(channel: Channel | PartialDMChannel) {
}
public channelUpdate(oldChannel: Channel, newChannel: Channel) {
}
public guildBanAdd(ban: GuildBan) {
}
public guildBanRemove(ban: GuildBan) {
}
public guildCreate(guild: Guild) {
}
public guildMemberAdd(member: GuildMember) {
}
public guildMemberRemove(member: GuildMember | PartialGuildMember) {
}
public guildMemberUpdate(oldMember: GuildMember | PartialGuildMember, newMember: GuildMember) {
}
public messageCreate(message: Message) {
}
public messageDelete(message: Message | PartialMessage) {
}
public messageUpdate(oldMessage: Message | PartialMessage, newMessage: Message | PartialMessage) {
}
public ready() {
}
}