From a01a43788ea7974516130bed420460080588a11e Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sat, 16 Jul 2022 16:12:55 +0100 Subject: [PATCH] Feature/49 ignore channel lists when logging (#168) * Add entity * Create ignore command * Update message events to ignore channels set --- src/commands/ignore.ts | 37 +++++++++++++++++++ src/entity/IgnoredChannel.ts | 21 +++++++++++ src/events/MessageEvents.ts | 7 ++++ .../1657647026816-CreateIgnoredChannel.ts | 13 +++++++ src/registry.ts | 8 ++-- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/commands/ignore.ts create mode 100644 src/entity/IgnoredChannel.ts create mode 100644 src/migration/1657647026816-CreateIgnoredChannel.ts diff --git a/src/commands/ignore.ts b/src/commands/ignore.ts new file mode 100644 index 0000000..2bd3297 --- /dev/null +++ b/src/commands/ignore.ts @@ -0,0 +1,37 @@ +import { ICommandContext } from "../contracts/ICommandContext"; +import IgnoredChannel from "../entity/IgnoredChannel"; +import PublicEmbed from "../helpers/embeds/PublicEmbed"; +import { Command } from "../type/command"; + +export default class Ignore extends Command { + constructor() { + super(); + + super.Category = "Moderation"; + super.Roles = [ + "moderator" + ]; + } + + public override async execute(context: ICommandContext) { + if (!context.message.guild) return; + + const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(context.message.channel.id); + + if (isChannelIgnored) { + const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, context.message.channel.id); + + await IgnoredChannel.Remove(IgnoredChannel, entity); + + const embed = new PublicEmbed(context, "Success", "This channel will start being logged again."); + await embed.SendToCurrentChannel(); + } else { + const entity = new IgnoredChannel(context.message.channel.id); + + await entity.Save(IgnoredChannel, entity); + + const embed = new PublicEmbed(context, "Success", "This channel will now be ignored from logging."); + await embed.SendToCurrentChannel(); + } + } +} \ No newline at end of file diff --git a/src/entity/IgnoredChannel.ts b/src/entity/IgnoredChannel.ts new file mode 100644 index 0000000..cd38dd5 --- /dev/null +++ b/src/entity/IgnoredChannel.ts @@ -0,0 +1,21 @@ +import { Entity, getConnection } from "typeorm"; +import BaseEntity from "../contracts/BaseEntity"; + +@Entity() +export default class IgnoredChannel extends BaseEntity { + constructor(channelId: string) { + super(); + + this.Id = channelId; + } + + public static async IsChannelIgnored(channelId: string): Promise { + const connection = getConnection(); + + const repository = connection.getRepository(IgnoredChannel); + + const single = await repository.findOne(channelId); + + return single != undefined; + } +} \ No newline at end of file diff --git a/src/events/MessageEvents.ts b/src/events/MessageEvents.ts index cfeb13e..9f37a9e 100644 --- a/src/events/MessageEvents.ts +++ b/src/events/MessageEvents.ts @@ -3,6 +3,7 @@ import { Message } from "discord.js"; import EventEmbed from "../helpers/embeds/EventEmbed"; import SettingsHelper from "../helpers/SettingsHelper"; import OnMessage from "./MessageEvents/OnMessage"; +import IgnoredChannel from "../entity/IgnoredChannel"; export default class MessageEvents extends Event { constructor() { @@ -16,6 +17,9 @@ export default class MessageEvents extends Event { 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 EventEmbed(message.client, message.guild, "Message Deleted"); embed.AddUser("User", message.author, true); embed.addField("Channel", message.channel.toString(), true); @@ -39,6 +43,9 @@ export default class MessageEvents extends Event { 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 EventEmbed(newMessage.client, newMessage.guild, "Message Edited"); embed.AddUser("User", newMessage.author, true); embed.addField("Channel", newMessage.channel.toString(), true); diff --git a/src/migration/1657647026816-CreateIgnoredChannel.ts b/src/migration/1657647026816-CreateIgnoredChannel.ts new file mode 100644 index 0000000..aac003a --- /dev/null +++ b/src/migration/1657647026816-CreateIgnoredChannel.ts @@ -0,0 +1,13 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class migration1657647026816 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(`CREATE TABLE ignored_channel (Id varchar(255), PRIMARY KEY (Id))`); + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query(`DROP TABLE ignored_channel`) + } + +} diff --git a/src/registry.ts b/src/registry.ts index 9d073b1..183bac2 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -8,6 +8,7 @@ import Code from "./commands/code"; import Config from "./commands/config"; import Disable from "./commands/disable"; import Help from "./commands/help"; +import Ignore from "./commands/ignore"; import Kick from "./commands/kick"; import Mute from "./commands/mute"; import Poll from "./commands/poll"; @@ -32,7 +33,11 @@ export default class Registry { CoreClient.RegisterCommand("ban", new Ban()); CoreClient.RegisterCommand("bunny", new Bunny()); CoreClient.RegisterCommand("clear", new Clear()); + CoreClient.RegisterCommand("code", new Code()); + CoreClient.RegisterCommand("config", new Config()); + CoreClient.RegisterCommand("disable", new Disable()); CoreClient.RegisterCommand("help", new Help()); + CoreClient.RegisterCommand("ignore", new Ignore()); CoreClient.RegisterCommand("kick", new Kick()); CoreClient.RegisterCommand("mute", new Mute()); CoreClient.RegisterCommand("poll", new Poll()); @@ -41,9 +46,6 @@ export default class Registry { CoreClient.RegisterCommand("unmute", new Unmute()); CoreClient.RegisterCommand("warn", new Warn()); CoreClient.RegisterCommand("setup", new Setup()); - CoreClient.RegisterCommand("config", new Config()); - CoreClient.RegisterCommand("code", new Code()); - CoreClient.RegisterCommand("disable", new Disable()); // Exclusive Commands: MankBot CoreClient.RegisterCommand("lobby", new Lobby(), "501231711271780357");