Make bot check channel permissions before sending messages (#161)

This commit is contained in:
Vylpes 2022-06-05 14:11:01 +01:00 committed by GitHub
parent aa070bb7a7
commit 1403619bda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 146 additions and 142 deletions

View file

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

View file

@ -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>(eLobby, entity);
const embed = new PublicEmbed(context, "", `Removed <#${context.args[2]}> from the list of lobby channels`);
embed.SendToCurrentChannel();
await embed.SendToCurrentChannel();
}
}

View file

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

View file

@ -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,

View file

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

View file

@ -18,7 +18,7 @@ export default class Clear extends Command {
public override async execute(context: ICommandContext): Promise<ICommandReturnContext> {
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,

View file

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

View file

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

View file

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

View file

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

View file

@ -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,

View file

@ -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,

View file

@ -17,7 +17,7 @@ export default class Poll extends Command {
if (argsSplit.length < 3 || argsSplit.length > 10) {
const errorEmbed = new ErrorEmbed(context, "Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options");
errorEmbed.SendToCurrentChannel();
await errorEmbed.SendToCurrentChannel();
return {
commandContext: context,

View file

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

View file

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

View file

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

View file

@ -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,

View file

@ -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,