From aa070bb7a7f17acaddaa8981f92070387c9c5ed4 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Mon, 16 May 2022 18:41:15 +0100 Subject: [PATCH 1/2] v3.0.2 (#159) * Change lobby command to error upon making a duplicate lobby channel (#154) * Update lobby command to give proper errors if role or channel id cannot be found (#156) * Add bunny command back (#157) * 150 assignable roles should be its own table to prevent limitations on length (#158) * Add entity * Update role config command to use new entity * Update role command to use new entity * Remove legacy code from config command * Update .env template to current date --- .env.template | 6 +- data/usage/config.txt | 1 - package.json | 4 +- src/commands/501231711271780357/lobby.ts | 40 +++++++--- src/commands/bunny.ts | 29 ++++++++ src/commands/role.ts | 91 ++++++++++++++--------- src/entity/Role.ts | 44 +++++++++++ src/entity/Server.ts | 10 ++- src/migration/1652375907691-CreateRole.ts | 13 ++++ src/registry.ts | 2 + 10 files changed, 188 insertions(+), 52 deletions(-) create mode 100644 src/commands/bunny.ts create mode 100644 src/entity/Role.ts create mode 100644 src/migration/1652375907691-CreateRole.ts diff --git a/.env.template b/.env.template index 33148ee..91d1655 100644 --- a/.env.template +++ b/.env.template @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=3.0.1 +BOT_VER=3.0.2 BOT_AUTHOR=Vylpes -BOT_DATE=24 Apr 2022 -BOT_OWNERID=147392775707426816 \ No newline at end of file +BOT_DATE=16 May 2022 +BOT_OWNERID=147392775707426816 diff --git a/data/usage/config.txt b/data/usage/config.txt index ffbcf1b..f284c78 100644 --- a/data/usage/config.txt +++ b/data/usage/config.txt @@ -5,7 +5,6 @@ bot.prefix: The bot prefix for the server (Default: "v!") commands.disabled: Disabled commands, separated by commas (Default: "") -role.assignable: List of roles assignable to user, separated by commas (Default: "") role.moderator: The moderator role name (Default: "Moderator") role.administrator: The administrator role name (Default: "Administrator") role.muted: The muted role name (Default: "Muted") diff --git a/package.json b/package.json index ce8b454..e4416f2 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "scripts": { "build": "tsc", "start": "node ./dist/vylbot", - "test": "jest" + "test": "jest", + "db:up": "typeorm migration:run", + "db:down": "typeorm migration:revert" }, "repository": { "type": "git", diff --git a/src/commands/501231711271780357/lobby.ts b/src/commands/501231711271780357/lobby.ts index 6d3a58c..4d0bcfc 100644 --- a/src/commands/501231711271780357/lobby.ts +++ b/src/commands/501231711271780357/lobby.ts @@ -111,8 +111,26 @@ export default class Lobby extends Command { const cooldown = Number(context.args[4]) || 30; const gameName = context.args.splice(5).join(" "); - if (!channel || !role) { - this.SendConfigHelp(context); + if (!channel) { + const errorEmbed = new ErrorEmbed(context, "The channel id you provided is invalid or channel does not exist."); + errorEmbed.SendToCurrentChannel(); + + return; + } + + if (!role) { + const errorEmbed = new ErrorEmbed(context, "The role id you provided is invalid or role does not exist."); + errorEmbed.SendToCurrentChannel(); + + return; + } + + const lobby = await eLobby.FetchOneByChannelId(channel.id); + + if (lobby) { + const errorEmbed = new ErrorEmbed(context, "This channel has already been setup."); + errorEmbed.SendToCurrentChannel(); + return; } @@ -124,20 +142,18 @@ export default class Lobby extends Command { } private async RemoveLobbyConfig(context: ICommandContext) { - const channel = context.message.guild!.channels.cache.find(x => x.id == context.args[2]); + const entity = await eLobby.FetchOneByChannelId(context.args[2]); + + if (!entity) { + const errorEmbed = new ErrorEmbed(context, "The channel id you provided has not been setup as a lobby, unable to remove."); + errorEmbed.SendToCurrentChannel(); - if (!channel) { - this.SendConfigHelp(context); return; } + + await BaseEntity.Remove(eLobby, entity); - const entity = await eLobby.FetchOneByChannelId(channel.id); - - if (entity) { - await BaseEntity.Remove(eLobby, entity); - } - - const embed = new PublicEmbed(context, "", `Removed \`${channel.name}\` from the list of lobby channels`); + const embed = new PublicEmbed(context, "", `Removed <#${context.args[2]}> from the list of lobby channels`); embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/bunny.ts b/src/commands/bunny.ts new file mode 100644 index 0000000..758df44 --- /dev/null +++ b/src/commands/bunny.ts @@ -0,0 +1,29 @@ +import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; +import PublicEmbed from "../helpers/embeds/PublicEmbed"; +import { Command } from "../type/command"; +import { ICommandContext } from "../contracts/ICommandContext"; +import randomBunny from "random-bunny"; + +export default class Bunny extends Command { + constructor() { + super(); + + super.Category = "Fun"; + } + + public override async execute(context: ICommandContext) { + const result = await randomBunny('rabbits', 'hot'); + + if (result.IsSuccess) { + const embed = new PublicEmbed(context, result.Result!.Title, "") + .setImage(result.Result!.Url) + .setURL(`https://reddit.com${result.Result!.Permalink}`) + .setFooter({ text: `r/Rabbits · ${result.Result!.Ups} upvotes` }); + + embed.SendToCurrentChannel(); + } else { + const errorEmbed = new ErrorEmbed(context, "There was an error using this command."); + errorEmbed.SendToCurrentChannel(); + } + } +} \ No newline at end of file diff --git a/src/commands/role.ts b/src/commands/role.ts index 9a0af00..9dbda89 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -6,6 +6,9 @@ import { ICommandContext } from "../contracts/ICommandContext"; import ICommandReturnContext from "../contracts/ICommandReturnContext"; import SettingsHelper from "../helpers/SettingsHelper"; import { readFileSync } from "fs"; +import { default as eRole } from "../entity/Role"; +import Server from "../entity/Server"; + export default class Role extends Command { constructor() { super(); @@ -30,27 +33,34 @@ export default class Role extends Command { // ======= private async UseDefault(context: ICommandContext) { - const roles = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id); - - if (!roles) { - const errorEmbed = new ErrorEmbed(context, "Unable to find any assignable roles"); - errorEmbed.SendToCurrentChannel(); - - return; - } - - const rolesArray = roles.split(","); - if (context.args.length == 0) { - await this.SendRolesList(context, rolesArray, context.message.guild!.id); + await this.SendRolesList(context, context.message.guild!.id); } else { - await this.ToggleRole(context, rolesArray); + await this.ToggleRole(context); } } - public async SendRolesList(context: ICommandContext, roles: String[], serverId: string): Promise { + public async GetRolesList(context: ICommandContext): Promise { + const rolesArray = await eRole.FetchAllByServerId(context.message.guild!.id); + + const stringArray: string[] = []; + + for (let i = 0; i < rolesArray.length; i++) { + const serverRole = context.message.guild!.roles.cache.find(x => x.id == rolesArray[i].RoleId); + + if (serverRole) { + stringArray.push(serverRole.name); + } + } + + return stringArray; + } + + public async SendRolesList(context: ICommandContext, serverId: string): Promise { + const roles = await this.GetRolesList(context); + const botPrefix = await SettingsHelper.GetServerPrefix(serverId); - const description = `Do ${botPrefix}role to get the role!\n${roles.join('\n')}`; + const description = roles.length == 0 ? "*no roles*" : `Do ${botPrefix}role to get the role!\n\n${roles.join('\n')}`; const embed = new PublicEmbed(context, "Roles", description); embed.SendToCurrentChannel(); @@ -61,7 +71,8 @@ export default class Role extends Command { }; } - public async ToggleRole(context: ICommandContext, roles: String[]): Promise { + public async ToggleRole(context: ICommandContext): Promise { + const roles = await this.GetRolesList(context); const requestedRole = context.args.join(" "); if (!roles.includes(requestedRole)) { @@ -164,16 +175,33 @@ export default class Role extends Command { this.SendConfigHelp(context); return; } - - let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id) || ""; - const settingArray = setting.split(","); + const existingRole = await eRole.FetchOneByRoleId(role.id); - settingArray.push(role.name); + if (existingRole) { + const errorEmbed = new ErrorEmbed(context, "This role has already been setup"); + errorEmbed.SendToCurrentChannel(); - setting = settingArray.join(","); + return; + } - await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting); + const server = await Server.FetchOneById(Server, context.message.guild!.id, [ + "Roles", + ]); + + if (!server) { + const errorEmbed = new ErrorEmbed(context, "Server not setup, please request the server owner runs the setup command."); + errorEmbed.SendToCurrentChannel(); + + return; + } + + const roleSetting = new eRole(role.id); + + await roleSetting.Save(eRole, roleSetting); + + server.AddRoleToServer(roleSetting); + await server.Save(Server, server); const embed = new PublicEmbed(context, "", `Added \`${role.name}\` as a new assignable role`); embed.SendToCurrentChannel(); @@ -187,21 +215,16 @@ export default class Role extends Command { return; } - let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id); + const existingRole = await eRole.FetchOneByRoleId(role.id); - if (!setting) return; + if (!existingRole) { + const errorEmbed = new ErrorEmbed(context, "Unable to find this role"); + errorEmbed.SendToCurrentChannel(); - const settingArray = setting.split(","); + return; + } - const index = settingArray.findIndex(x => x == role.name); - - if (index == -1) return; - - settingArray.splice(index, 1); - - setting = settingArray.join(","); - - await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting); + await eRole.Remove(eRole, existingRole); const embed = new PublicEmbed(context, "", `Removed \`${role.name}\` from the list of assignable roles`); embed.SendToCurrentChannel(); diff --git a/src/entity/Role.ts b/src/entity/Role.ts new file mode 100644 index 0000000..5b534ad --- /dev/null +++ b/src/entity/Role.ts @@ -0,0 +1,44 @@ +import { Column, Entity, EntityTarget, getConnection, ManyToOne } from "typeorm"; +import BaseEntity from "../contracts/BaseEntity" +import Server from "./Server"; + +@Entity() +export default class Role extends BaseEntity { + constructor(roleId: string) { + super(); + + this.RoleId = roleId; + } + + @Column() + RoleId: string; + + @ManyToOne(() => Server, x => x.Roles) + Server: Server; + + public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise { + const connection = getConnection(); + + const repository = connection.getRepository(Role); + + const single = await repository.findOne({ RoleId: roleId}, { relations: relations || [] }); + + return single; + } + + public static async FetchAllByServerId(serverId: string): Promise { + const connection = getConnection(); + + const repository = connection.getRepository(Server); + + const all = await repository.findOne(serverId, { relations: [ + "Roles", + ]}); + + if (!all) { + return []; + } + + return all.Roles; + } +} \ No newline at end of file diff --git a/src/entity/Server.ts b/src/entity/Server.ts index 180aef0..f669e58 100644 --- a/src/entity/Server.ts +++ b/src/entity/Server.ts @@ -1,5 +1,6 @@ -import { Column, Entity, getConnection, OneToMany } from "typeorm"; +import { Entity, OneToMany } from "typeorm"; import BaseEntity from "../contracts/BaseEntity"; +import Role from "./Role"; import Setting from "./Setting"; @Entity() @@ -13,7 +14,14 @@ export default class Server extends BaseEntity { @OneToMany(() => Setting, x => x.Server) Settings: Setting[]; + @OneToMany(() => Role, x => x.Server) + Roles: Role[]; + public AddSettingToServer(setting: Setting) { this.Settings.push(setting); } + + public AddRoleToServer(role: Role) { + this.Roles.push(role); + } } \ No newline at end of file diff --git a/src/migration/1652375907691-CreateRole.ts b/src/migration/1652375907691-CreateRole.ts new file mode 100644 index 0000000..255ed0f --- /dev/null +++ b/src/migration/1652375907691-CreateRole.ts @@ -0,0 +1,13 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class migration1652375907691 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(`CREATE TABLE role (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, RoleId varchar(255), serverId varchar(255), PRIMARY KEY (Id), FOREIGN KEY (serverId) REFERENCES server(Id))`); + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query(`DROP TABLE role`); + } + +} diff --git a/src/registry.ts b/src/registry.ts index 19a673a..9d073b1 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -24,11 +24,13 @@ import Lobby from "./commands/501231711271780357/lobby"; // Event Imports import MemberEvents from "./events/MemberEvents"; import MessageEvents from "./events/MessageEvents"; +import Bunny from "./commands/bunny"; export default class Registry { public static RegisterCommands() { CoreClient.RegisterCommand("about", new About()); CoreClient.RegisterCommand("ban", new Ban()); + CoreClient.RegisterCommand("bunny", new Bunny()); CoreClient.RegisterCommand("clear", new Clear()); CoreClient.RegisterCommand("help", new Help()); CoreClient.RegisterCommand("kick", new Kick()); From 1403619bdafc2c41920f8a1a0d9b7e7ddaec6ae1 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sun, 5 Jun 2022 14:11:01 +0100 Subject: [PATCH 2/2] Make bot check channel permissions before sending messages (#161) --- .env.template | 4 +-- ormconfig.json.dev.template | 24 ------------------ package.json | 2 +- src/commands/501231711271780357/entry.ts | 2 +- src/commands/501231711271780357/lobby.ts | 14 +++++------ src/commands/about.ts | 10 ++------ src/commands/ban.ts | 11 ++++++--- src/commands/bunny.ts | 4 +-- src/commands/clear.ts | 6 ++--- src/commands/code.ts | 6 ++--- src/commands/config.ts | 15 ++++++----- src/commands/disable.ts | 6 ++--- src/commands/help.ts | 20 +++++++-------- src/commands/kick.ts | 8 +++--- src/commands/mute.ts | 10 ++++---- src/commands/poll.ts | 2 +- src/commands/role.ts | 26 ++++++++++---------- src/commands/rules.ts | 21 ++++++---------- src/commands/setup.ts | 5 ++-- src/commands/unmute.ts | 10 ++++---- src/commands/warn.ts | 6 ++--- src/events/MemberEvents.ts | 8 +++--- src/events/MemberEvents/GuildMemberUpdate.ts | 4 +-- src/events/MessageEvents.ts | 8 +++--- src/helpers/embeds/ErrorEmbed.ts | 13 ++++++++-- src/helpers/embeds/EventEmbed.ts | 17 ++++++++++--- src/helpers/embeds/LogEmbed.ts | 13 ++++++++-- src/helpers/embeds/PublicEmbed.ts | 13 ++++++++-- 28 files changed, 146 insertions(+), 142 deletions(-) delete mode 100644 ormconfig.json.dev.template diff --git a/.env.template b/.env.template index 91d1655..785fe3e 100644 --- a/.env.template +++ b/.env.template @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=3.0.2 +BOT_VER=3.0.3 BOT_AUTHOR=Vylpes -BOT_DATE=16 May 2022 +BOT_DATE=04 Jun 2022 BOT_OWNERID=147392775707426816 diff --git a/ormconfig.json.dev.template b/ormconfig.json.dev.template deleted file mode 100644 index 6c48598..0000000 --- a/ormconfig.json.dev.template +++ /dev/null @@ -1,24 +0,0 @@ -{ - "type": "mysql", - "host": "localhost", - "port": 3306, - "username": "dev", - "password": "dev", - "database": "vylbot", - "synchronize": true, - "logging": false, - "entities": [ - "dist/entity/**/*.js" - ], - "migrations": [ - "dist/migration/**/*.js" - ], - "subscribers": [ - "dist/subscriber/**/*.js" - ], - "cli": { - "entitiesDir": "dist/entity", - "migrationsDir": "dist/migration", - "subscribersDir": "dist/subscriber" - } -} \ No newline at end of file diff --git a/package.json b/package.json index e4416f2..d0df259 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vylbot-app", - "version": "3.0.1", + "version": "3.0.3", "description": "A discord bot made for Vylpes' Den", "main": "./dist/vylbot", "typings": "./dist", diff --git a/src/commands/501231711271780357/entry.ts b/src/commands/501231711271780357/entry.ts index 6f9dd83..2c0f4ae 100644 --- a/src/commands/501231711271780357/entry.ts +++ b/src/commands/501231711271780357/entry.ts @@ -20,6 +20,6 @@ export default class Entry extends Command { const embedInfo = new PublicEmbed(context, "", `Welcome to the server! Please make sure to read the rules in the <#${rulesChannelId}> channel and type the code found there in here to proceed to the main part of the server.`); - embedInfo.SendToCurrentChannel(); + await embedInfo.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/501231711271780357/lobby.ts b/src/commands/501231711271780357/lobby.ts index 4d0bcfc..ec74a7f 100644 --- a/src/commands/501231711271780357/lobby.ts +++ b/src/commands/501231711271780357/lobby.ts @@ -80,7 +80,7 @@ export default class Lobby extends Command { if (!context.message.member?.roles.cache.find(x => x.name == moderatorRole)) { const errorEmbed = new ErrorEmbed(context, "Sorry, you must be a moderator to be able to configure this command"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return; } @@ -94,15 +94,15 @@ export default class Lobby extends Command { break; case "help": default: - this.SendConfigHelp(context); + await this.SendConfigHelp(context); } } - private SendConfigHelp(context: ICommandContext) { + private async SendConfigHelp(context: ICommandContext) { const helpText = readFileSync(`${process.cwd()}/data/usage/lobby.txt`).toString(); const embed = new PublicEmbed(context, "Configure Lobby Command", helpText); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async AddLobbyConfig(context: ICommandContext) { @@ -138,7 +138,7 @@ export default class Lobby extends Command { await entity.Save(eLobby, entity); const embed = new PublicEmbed(context, "", `Added \`${channel.name}\` as a new lobby channel with a cooldown of \`${cooldown} minutes\` and will ping \`${role.name}\` on use`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async RemoveLobbyConfig(context: ICommandContext) { @@ -146,7 +146,7 @@ export default class Lobby extends Command { if (!entity) { const errorEmbed = new ErrorEmbed(context, "The channel id you provided has not been setup as a lobby, unable to remove."); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return; } @@ -154,6 +154,6 @@ export default class Lobby extends Command { await BaseEntity.Remove(eLobby, entity); const embed = new PublicEmbed(context, "", `Removed <#${context.args[2]}> from the list of lobby channels`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/about.ts b/src/commands/about.ts index 0e2b7bf..4246113 100644 --- a/src/commands/about.ts +++ b/src/commands/about.ts @@ -1,5 +1,4 @@ import { ICommandContext } from "../contracts/ICommandContext"; -import ICommandReturnContext from "../contracts/ICommandReturnContext"; import PublicEmbed from "../helpers/embeds/PublicEmbed"; import { Command } from "../type/command"; @@ -9,17 +8,12 @@ export default class About extends Command { super.Category = "General"; } - public override execute(context: ICommandContext): ICommandReturnContext { + public override async execute(context: ICommandContext) { const embed = new PublicEmbed(context, "About", "") .addField("Version", process.env.BOT_VER!) .addField("Author", process.env.BOT_AUTHOR!) .addField("Date", process.env.BOT_DATE!); - embed.SendToCurrentChannel(); - - return { - commandContext: context, - embeds: [embed] - }; + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/ban.ts b/src/commands/ban.ts index 8656921..ad39f03 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -21,7 +21,8 @@ export default class Ban extends Command { if (!targetUser) { const embed = new ErrorEmbed(context, "User does not exist"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); + return { commandContext: context, embeds: [embed], @@ -32,7 +33,8 @@ export default class Ban extends Command { if (!targetMember) { const embed = new ErrorEmbed(context, "User is not in this server"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); + return { commandContext: context, embeds: [embed], @@ -53,7 +55,8 @@ export default class Ban extends Command { if (!targetMember.bannable) { const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); + return { commandContext: context, embeds: [embed], @@ -70,7 +73,7 @@ export default class Ban extends Command { await targetMember.ban({ reason: `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}` }); await logEmbed.SendToModLogsChannel(); - publicEmbed.SendToCurrentChannel(); + await publicEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/bunny.ts b/src/commands/bunny.ts index 758df44..47bafa6 100644 --- a/src/commands/bunny.ts +++ b/src/commands/bunny.ts @@ -20,10 +20,10 @@ export default class Bunny extends Command { .setURL(`https://reddit.com${result.Result!.Permalink}`) .setFooter({ text: `r/Rabbits · ${result.Result!.Ups} upvotes` }); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } else { const errorEmbed = new ErrorEmbed(context, "There was an error using this command."); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); } } } \ No newline at end of file diff --git a/src/commands/clear.ts b/src/commands/clear.ts index 243454a..1bd3e55 100644 --- a/src/commands/clear.ts +++ b/src/commands/clear.ts @@ -18,7 +18,7 @@ export default class Clear extends Command { public override async execute(context: ICommandContext): Promise { if (context.args.length == 0) { const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, @@ -30,7 +30,7 @@ export default class Clear extends Command { if (!totalToClear || totalToClear <= 0 || totalToClear > 100) { const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, embeds: [errorEmbed] @@ -40,7 +40,7 @@ export default class Clear extends Command { await (context.message.channel as TextChannel).bulkDelete(totalToClear); const embed = new PublicEmbed(context, "", `${totalToClear} message(s) were removed`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/code.ts b/src/commands/code.ts index 7c5c7da..cdfac96 100644 --- a/src/commands/code.ts +++ b/src/commands/code.ts @@ -58,7 +58,7 @@ export default class Code extends Command { ].join("\n"); const embed = new PublicEmbed(context, "", description); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async Randomise(context: ICommandContext) { @@ -71,7 +71,7 @@ export default class Code extends Command { await SettingsHelper.SetSetting("verification.code", context.message.guild.id, randomCode); const embed = new PublicEmbed(context, "Code", `Entry code has been set to \`${randomCode}\``); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async SendEmbed(context: ICommandContext) { @@ -89,6 +89,6 @@ export default class Code extends Command { } const embed = new PublicEmbed(context, "Entry Code", code!); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/config.ts b/src/commands/config.ts index b7e9c93..f3710e3 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -1,9 +1,7 @@ -import { Guild } from "discord.js"; import { readFileSync } from "fs"; import { CommandResponse } from "../constants/CommandResponse"; import DefaultValues from "../constants/DefaultValues"; import { ICommandContext } from "../contracts/ICommandContext"; -import ICommandReturnContext from "../contracts/ICommandReturnContext"; import Server from "../entity/Server"; import Setting from "../entity/Setting"; import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; @@ -83,7 +81,7 @@ export default class Config extends Command { const embed = new PublicEmbed(context, "Config", description); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async GetValue(context: ICommandContext, server: Server, key: string) { @@ -91,10 +89,10 @@ export default class Config extends Command { if (setting) { const embed = new PublicEmbed(context, "", `${key}: ${setting.Value}`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } else { const embed = new PublicEmbed(context, "", `${key}: ${DefaultValues.GetValue(key)} `); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } @@ -103,14 +101,15 @@ export default class Config extends Command { if (!setting) { const embed = new PublicEmbed(context, "", "Setting has been reset"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); + return; } await Setting.Remove(Setting, setting); const embed = new PublicEmbed(context, "", "Setting has been reset"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async SetValue(context: ICommandContext, server: Server, key: string, value: string) { @@ -131,6 +130,6 @@ export default class Config extends Command { } const embed = new PublicEmbed(context, "", "Setting has been set"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/disable.ts b/src/commands/disable.ts index b1d9f36..390da51 100644 --- a/src/commands/disable.ts +++ b/src/commands/disable.ts @@ -38,7 +38,7 @@ export default class Disable extends Command { ].join("\n"); const embed = new PublicEmbed(context, "", description); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async Add(context: ICommandContext) { @@ -61,7 +61,7 @@ export default class Disable extends Command { await SettingsHelper.SetSetting("commands.disabled", context.message.guild.id, disabledCommands!.join(",")); const embed = new PublicEmbed(context, "", `Disabled command: ${commandName}`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async Remove(context: ICommandContext) { @@ -88,6 +88,6 @@ export default class Disable extends Command { await SettingsHelper.SetSetting("commands.disabled", context.message.guild.id, disabledCommands!.join(",")); const embed = new PublicEmbed(context, "", `Enabled command: ${commandName}`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/help.ts b/src/commands/help.ts index aa056d5..5366acf 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -20,15 +20,15 @@ export default class Help extends Command { super.Category = "General"; } - public override execute(context: ICommandContext) { + public override async execute(context: ICommandContext) { if (context.args.length == 0) { - this.SendAll(context); + await this.SendAll(context); } else { - this.SendSingle(context); + await this.SendSingle(context); } } - public SendAll(context: ICommandContext) { + public async SendAll(context: ICommandContext) { const allCommands = CoreClient.commandItems .filter(x => !x.ServerId || x.ServerId == context.message.guild?.id); const cateogries = [...new Set(allCommands.map(x => x.Command.Category))]; @@ -41,10 +41,10 @@ export default class Help extends Command { embed.addField(StringTools.Capitalise(category || "Uncategorised"), StringTools.CapitaliseArray(filtered.flatMap(x => x.Name)).join(", ")); }); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } - public SendSingle(context: ICommandContext) { + public async SendSingle(context: ICommandContext) { const command = CoreClient.commandItems.find(x => x.Name == context.args[0] && !x.ServerId); const exclusiveCommand = CoreClient.commandItems.find(x => x.Name == context.args[0] && x.ServerId == context.message.guild?.id); @@ -53,18 +53,16 @@ export default class Help extends Command { embed.addField("Category", StringTools.Capitalise(exclusiveCommand.Command.Category || "Uncategorised")); embed.addField("Required Roles", StringTools.Capitalise(exclusiveCommand.Command.Roles.join(", ")) || "Everyone"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } else if (command) { const embed = new PublicEmbed(context, StringTools.Capitalise(command.Name), ""); embed.addField("Category", StringTools.Capitalise(command.Command.Category || "Uncategorised")); embed.addField("Required Roles", StringTools.Capitalise(command.Command.Roles.join(", ")) || "Everyone"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } else { const errorEmbed = new ErrorEmbed(context, "Command does not exist"); - errorEmbed.SendToCurrentChannel(); - - return; + await errorEmbed.SendToCurrentChannel(); } } } diff --git a/src/commands/kick.ts b/src/commands/kick.ts index 9f75080..81b1af9 100644 --- a/src/commands/kick.ts +++ b/src/commands/kick.ts @@ -21,7 +21,7 @@ export default class Kick extends Command { if (!targetUser) { const embed = new ErrorEmbed(context, "User does not exist"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -33,7 +33,7 @@ export default class Kick extends Command { if (!targetMember) { const embed = new ErrorEmbed(context, "User is not in this server"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -55,7 +55,7 @@ export default class Kick extends Command { if (!targetMember.kickable) { const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -73,7 +73,7 @@ export default class Kick extends Command { await targetMember.kick(`Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`); await logEmbed.SendToModLogsChannel(); - publicEmbed.SendToCurrentChannel(); + await publicEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/mute.ts b/src/commands/mute.ts index fbf0bcf..e5d43df 100644 --- a/src/commands/mute.ts +++ b/src/commands/mute.ts @@ -21,7 +21,7 @@ export default class Mute extends Command { if (!targetUser) { const embed = new ErrorEmbed(context, "User does not exist"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -33,7 +33,7 @@ export default class Mute extends Command { if (!targetMember) { const embed = new ErrorEmbed(context, "User is not in this server"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -55,7 +55,7 @@ export default class Mute extends Command { if (!targetMember.manageable) { const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -75,7 +75,7 @@ export default class Mute extends Command { if (!mutedRole) { const embed = new ErrorEmbed(context, ErrorMessages.RoleNotFound); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -86,7 +86,7 @@ export default class Mute extends Command { await targetMember.roles.add(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`); await logEmbed.SendToModLogsChannel(); - publicEmbed.SendToCurrentChannel(); + await publicEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/poll.ts b/src/commands/poll.ts index be88869..3e84220 100644 --- a/src/commands/poll.ts +++ b/src/commands/poll.ts @@ -17,7 +17,7 @@ export default class Poll extends Command { if (argsSplit.length < 3 || argsSplit.length > 10) { const errorEmbed = new ErrorEmbed(context, "Usage: ;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/role.ts b/src/commands/role.ts index 9dbda89..952b590 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -63,7 +63,7 @@ export default class Role extends Command { const description = roles.length == 0 ? "*no roles*" : `Do ${botPrefix}role <role> to get the role!\n\n${roles.join('\n')}`; const embed = new PublicEmbed(context, "Roles", description); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -77,7 +77,7 @@ export default class Role extends Command { if (!roles.includes(requestedRole)) { const errorEmbed = new ErrorEmbed(context, "This role isn't marked as assignable, to see a list of assignable roles, run this command without any parameters"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, @@ -89,7 +89,7 @@ export default class Role extends Command { if (!assignRole) { const errorEmbed = new ErrorEmbed(context, "The current server doesn't have this role. Please contact the server's moderators"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, @@ -115,7 +115,7 @@ export default class Role extends Command { await context.message.member?.roles.add(role, "Toggled with role command"); const embed = new PublicEmbed(context, "", `Gave role: \`${role.name}\``); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -127,7 +127,7 @@ export default class Role extends Command { await context.message.member?.roles.remove(role, "Toggled with role command"); const embed = new PublicEmbed(context, "", `Removed role: \`${role.name}\``); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -144,7 +144,7 @@ export default class Role extends Command { if (!context.message.member?.roles.cache.find(x => x.name == moderatorRole)) { const errorEmbed = new ErrorEmbed(context, "Sorry, you must be a moderator to be able to configure this command"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return; } @@ -157,15 +157,15 @@ export default class Role extends Command { await this.RemoveRoleConfig(context); break; default: - this.SendConfigHelp(context); + await this.SendConfigHelp(context); } } - private SendConfigHelp(context: ICommandContext) { + private async SendConfigHelp(context: ICommandContext) { const helpText = readFileSync(`${process.cwd()}/data/usage/role.txt`).toString(); const embed = new PublicEmbed(context, "Configure Role Command", helpText); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async AddRoleConfig(context: ICommandContext) { @@ -180,7 +180,7 @@ export default class Role extends Command { if (existingRole) { const errorEmbed = new ErrorEmbed(context, "This role has already been setup"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return; } @@ -191,7 +191,7 @@ export default class Role extends Command { if (!server) { const errorEmbed = new ErrorEmbed(context, "Server not setup, please request the server owner runs the setup command."); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return; } @@ -204,7 +204,7 @@ export default class Role extends Command { await server.Save(Server, server); const embed = new PublicEmbed(context, "", `Added \`${role.name}\` as a new assignable role`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } private async RemoveRoleConfig(context: ICommandContext) { @@ -227,6 +227,6 @@ export default class Role extends Command { await eRole.Remove(eRole, existingRole); const embed = new PublicEmbed(context, "", `Removed \`${role.name}\` from the list of assignable roles`); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } diff --git a/src/commands/rules.ts b/src/commands/rules.ts index 414b141..8730057 100644 --- a/src/commands/rules.ts +++ b/src/commands/rules.ts @@ -1,6 +1,5 @@ import { existsSync, readFileSync } from "fs"; import { ICommandContext } from "../contracts/ICommandContext"; -import ICommandReturnContext from "../contracts/ICommandReturnContext"; import ErrorEmbed from "../helpers/embeds/ErrorEmbed"; import PublicEmbed from "../helpers/embeds/PublicEmbed"; import { Command } from "../type/command"; @@ -22,15 +21,12 @@ export default class Rules extends Command { ]; } - public override execute(context: ICommandContext): ICommandReturnContext { + public override async execute(context: ICommandContext) { if (!existsSync(`${process.cwd()}/data/rules/${context.message.guild?.id}.json`)) { const errorEmbed = new ErrorEmbed(context, "Rules file doesn't exist"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); - return { - commandContext: context, - embeds: [errorEmbed] - }; + return; } const rulesFile = readFileSync(`${process.cwd()}/data/rules/${context.message.guild?.id}.json`).toString(); @@ -42,16 +38,15 @@ export default class Rules extends Command { const embed = new PublicEmbed(context, rule.title || "", rule.description?.join("\n") || ""); embed.setImage(rule.image || ""); - embed.setFooter(rule.footer || ""); + embed.setFooter({ text: rule.footer || "" }); embeds.push(embed); }); - embeds.forEach(x => x.SendToCurrentChannel()); + for (let i = 0; i < embeds.length; i++) { + const embed = embeds[i]; - return { - commandContext: context, - embeds: embeds - }; + await embed.SendToCurrentChannel(); + } } } \ No newline at end of file diff --git a/src/commands/setup.ts b/src/commands/setup.ts index 87be5a5..8267ff1 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -22,7 +22,8 @@ export default class Setup extends Command { if (server) { const embed = new ErrorEmbed(context, "This server has already been setup, please configure using the config command"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); + return; } @@ -31,6 +32,6 @@ export default class Setup extends Command { await newServer.Save(Server, newServer); const embed = new PublicEmbed(context, "Success", "Please configure using the config command"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); } } \ No newline at end of file diff --git a/src/commands/unmute.ts b/src/commands/unmute.ts index 26b82a2..b006dab 100644 --- a/src/commands/unmute.ts +++ b/src/commands/unmute.ts @@ -21,7 +21,7 @@ export default class Unmute extends Command { if (!targetUser) { const embed = new ErrorEmbed(context, "User does not exist"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -33,7 +33,7 @@ export default class Unmute extends Command { if (!targetMember) { const embed = new ErrorEmbed(context, "User is not in this server"); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -55,7 +55,7 @@ export default class Unmute extends Command { if (!targetMember.manageable) { const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -75,7 +75,7 @@ export default class Unmute extends Command { if (!mutedRole) { const embed = new ErrorEmbed(context, ErrorMessages.RoleNotFound); - embed.SendToCurrentChannel(); + await embed.SendToCurrentChannel(); return { commandContext: context, @@ -86,7 +86,7 @@ export default class Unmute extends Command { await targetMember.roles.remove(mutedRole, `Moderator: ${context.message.author.tag}, Reason: ${reason || "*none*"}`); await logEmbed.SendToModLogsChannel(); - publicEmbed.SendToCurrentChannel(); + await publicEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/commands/warn.ts b/src/commands/warn.ts index 3f42318..a36e28e 100644 --- a/src/commands/warn.ts +++ b/src/commands/warn.ts @@ -20,7 +20,7 @@ export default class Warn extends Command { if (!user) { const errorEmbed = new ErrorEmbed(context, "User does not exist"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, @@ -32,7 +32,7 @@ export default class Warn extends Command { if (!member) { const errorEmbed = new ErrorEmbed(context, "User is not in this server"); - errorEmbed.SendToCurrentChannel(); + await errorEmbed.SendToCurrentChannel(); return { commandContext: context, @@ -61,7 +61,7 @@ export default class Warn extends Command { publicEmbed.AddReason(reason); await logEmbed.SendToModLogsChannel(); - publicEmbed.SendToCurrentChannel(); + await publicEmbed.SendToCurrentChannel(); return { commandContext: context, diff --git a/src/events/MemberEvents.ts b/src/events/MemberEvents.ts index c0832f3..43d07e8 100644 --- a/src/events/MemberEvents.ts +++ b/src/events/MemberEvents.ts @@ -15,7 +15,7 @@ export default class MemberEvents extends Event { const enabled = await SettingsHelper.GetSetting("event.member.add.enabled", member.guild.id); if (!enabled || enabled.toLowerCase() != "true") return; - const embed = new EventEmbed(member.guild, "Member Joined"); + const embed = new EventEmbed(member.client, member.guild, "Member Joined"); embed.AddUser("User", member.user, true); embed.addField("Created", member.user.createdAt.toISOString()); embed.setFooter({ text: `Id: ${member.user.id}` }); @@ -23,7 +23,7 @@ export default class MemberEvents extends Event { const channel = await SettingsHelper.GetSetting("event.member.add.channel", member.guild.id); if (!channel || !member.guild.channels.cache.find(x => x.name == channel)) return; - embed.SendToChannel(channel); + await embed.SendToChannel(channel); } public override async guildMemberRemove(member: GuildMember) { @@ -32,7 +32,7 @@ export default class MemberEvents extends Event { const enabled = await SettingsHelper.GetSetting("event.member.remove.enabled", member.guild.id); if (!enabled || enabled.toLowerCase() != "true") return; - const embed = new EventEmbed(member.guild, "Member Left"); + const embed = new EventEmbed(member.client, member.guild, "Member Left"); embed.AddUser("User", member.user, true); embed.addField("Joined", member.joinedAt?.toISOString() || "n/a"); embed.setFooter({ text: `Id: ${member.user.id}` }); @@ -40,7 +40,7 @@ export default class MemberEvents extends Event { const channel = await SettingsHelper.GetSetting("event.member.remove.channel", member.guild.id); if (!channel || !member.guild.channels.cache.find(x => x.name == channel)) return; - embed.SendToChannel(channel); + await embed.SendToChannel(channel); } public override async guildMemberUpdate(oldMember: GuildMember, newMember: GuildMember) { diff --git a/src/events/MemberEvents/GuildMemberUpdate.ts b/src/events/MemberEvents/GuildMemberUpdate.ts index 3058f81..0abd37d 100644 --- a/src/events/MemberEvents/GuildMemberUpdate.ts +++ b/src/events/MemberEvents/GuildMemberUpdate.ts @@ -18,7 +18,7 @@ export default class GuildMemberUpdate { const oldNickname = this.oldMember.nickname || "*none*"; const newNickname = this.newMember.nickname || "*none*"; - const embed = new EventEmbed(this.newMember.guild, "Nickname Changed"); + const embed = new EventEmbed(this.oldMember.client, this.newMember.guild, "Nickname Changed"); embed.AddUser("User", this.newMember.user, true); embed.addField("Before", oldNickname, true); embed.addField("After", newNickname, true); @@ -27,6 +27,6 @@ export default class GuildMemberUpdate { const channel = await SettingsHelper.GetSetting("event.member.update.channel", this.newMember.guild.id); if (!channel || channel.toLowerCase() != "true") return; - embed.SendToChannel(channel); + await embed.SendToChannel(channel); } } \ No newline at end of file diff --git a/src/events/MessageEvents.ts b/src/events/MessageEvents.ts index 5586ea5..cfeb13e 100644 --- a/src/events/MessageEvents.ts +++ b/src/events/MessageEvents.ts @@ -16,7 +16,7 @@ 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 embed = new EventEmbed(message.guild, "Message Deleted"); + const embed = new EventEmbed(message.client, message.guild, "Message Deleted"); embed.AddUser("User", message.author, true); embed.addField("Channel", message.channel.toString(), true); embed.addField("Content", `\`\`\`${message.content || "*none*"}\`\`\``); @@ -28,7 +28,7 @@ export default class MessageEvents extends Event { const channel = await SettingsHelper.GetSetting("event.message.delete.channel", message.guild.id); if (!channel || !message.guild.channels.cache.find(x => x.name == channel)) return; - embed.SendToChannel(channel); + await embed.SendToChannel(channel); } public override async messageUpdate(oldMessage: Message, newMessage: Message) { @@ -39,7 +39,7 @@ 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 embed = new EventEmbed(newMessage.guild, "Message Edited"); + const embed = new EventEmbed(newMessage.client, newMessage.guild, "Message Edited"); embed.AddUser("User", newMessage.author, true); embed.addField("Channel", newMessage.channel.toString(), true); embed.addField("Before", `\`\`\`${oldMessage.content || "*none*"}\`\`\``); @@ -48,7 +48,7 @@ export default class MessageEvents extends Event { const channel = await SettingsHelper.GetSetting("event.message.update.channel", newMessage.guild.id); if (!channel || !newMessage.guild.channels.cache.find(x => x.name == channel)) return; - embed.SendToChannel(channel); + await embed.SendToChannel(channel); } public override async messageCreate(message: Message) { diff --git a/src/helpers/embeds/ErrorEmbed.ts b/src/helpers/embeds/ErrorEmbed.ts index 146461f..958b8fc 100644 --- a/src/helpers/embeds/ErrorEmbed.ts +++ b/src/helpers/embeds/ErrorEmbed.ts @@ -1,4 +1,4 @@ -import { MessageEmbed } from "discord.js"; +import { MessageEmbed, Permissions, TextChannel } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; export default class ErrorEmbed extends MessageEmbed { @@ -13,7 +13,16 @@ export default class ErrorEmbed extends MessageEmbed { this.context = context; } - public SendToCurrentChannel() { + public async SendToCurrentChannel() { + const channel = this.context.message.channel as TextChannel; + const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! }); + + if (!botMember) return; + + const permissions = channel.permissionsFor(botMember); + + if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return; + this.context.message.channel.send({ embeds: [ this ]}); } } \ No newline at end of file diff --git a/src/helpers/embeds/EventEmbed.ts b/src/helpers/embeds/EventEmbed.ts index 3dfd5dc..782f98a 100644 --- a/src/helpers/embeds/EventEmbed.ts +++ b/src/helpers/embeds/EventEmbed.ts @@ -1,17 +1,20 @@ -import { MessageEmbed, TextChannel, User, Guild } from "discord.js"; +import { MessageEmbed, TextChannel, User, Guild, Client, Permissions } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; import SettingsHelper from "../SettingsHelper"; export default class EventEmbed extends MessageEmbed { public guild: Guild; - constructor(guild: Guild, title: string) { + private client: Client; + + constructor(client: Client, guild: Guild, title: string) { super(); super.setColor(0x3050ba); super.setTitle(title); this.guild = guild; + this.client = client; } // Detail methods @@ -28,7 +31,7 @@ export default class EventEmbed extends MessageEmbed { } // Send methods - public SendToChannel(name: string) { + public async SendToChannel(name: string) { const channel = this.guild.channels.cache .find(channel => channel.name == name) as TextChannel; @@ -37,6 +40,14 @@ export default class EventEmbed extends MessageEmbed { return; } + const botMember = await this.guild.members.fetch({ user: this.client.user! }); + + if (!botMember) return; + + const permissions = channel.permissionsFor(botMember); + + if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return; + channel.send({embeds: [ this ]}); } diff --git a/src/helpers/embeds/LogEmbed.ts b/src/helpers/embeds/LogEmbed.ts index 0967b8b..105c6a9 100644 --- a/src/helpers/embeds/LogEmbed.ts +++ b/src/helpers/embeds/LogEmbed.ts @@ -1,4 +1,4 @@ -import { MessageEmbed, TextChannel, User } from "discord.js"; +import { MessageEmbed, Permissions, TextChannel, User } from "discord.js"; import ErrorMessages from "../../constants/ErrorMessages"; import { ICommandContext } from "../../contracts/ICommandContext"; import SettingsHelper from "../SettingsHelper"; @@ -30,7 +30,16 @@ export default class LogEmbed extends MessageEmbed { } // Send methods - public SendToCurrentChannel() { + public async SendToCurrentChannel() { + const channel = this.context.message.channel as TextChannel; + const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! }); + + if (!botMember) return; + + const permissions = channel.permissionsFor(botMember); + + if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return; + this.context.message.channel.send({ embeds: [ this ]}); } diff --git a/src/helpers/embeds/PublicEmbed.ts b/src/helpers/embeds/PublicEmbed.ts index ae28197..84be5c9 100644 --- a/src/helpers/embeds/PublicEmbed.ts +++ b/src/helpers/embeds/PublicEmbed.ts @@ -1,4 +1,4 @@ -import { MessageEmbed } from "discord.js"; +import { MessageEmbed, Permissions, TextChannel } from "discord.js"; import { ICommandContext } from "../../contracts/ICommandContext"; export default class PublicEmbed extends MessageEmbed { @@ -20,7 +20,16 @@ export default class PublicEmbed extends MessageEmbed { } // Send methods - public SendToCurrentChannel() { + public async SendToCurrentChannel() { + const channel = this.context.message.channel as TextChannel; + const botMember = await this.context.message.guild?.members.fetch({ user: this.context.message.client.user! }); + + if (!botMember) return; + + const permissions = channel.permissionsFor(botMember); + + if (!permissions.has(Permissions.FLAGS.SEND_MESSAGES)) return; + this.context.message.channel.send({ embeds: [ this ]}); } } \ No newline at end of file