Migrate ban command
This commit is contained in:
parent
bb433749f8
commit
ecf9c5e4fc
8 changed files with 183 additions and 96 deletions
61
src/commands/ban.ts
Normal file
61
src/commands/ban.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { Command, ICommandContext } from "vylbot-core";
|
||||
import ErrorEmbed from "../helpers/ErrorEmbed";
|
||||
import ErrorMessages from "../constants/ErrorMessages";
|
||||
import LogEmbed from "../helpers/LogEmbed";
|
||||
import PublicEmbed from "../helpers/PublicEmbed";
|
||||
|
||||
export default class Bane extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
super._category = "Moderation";
|
||||
super._roles = [
|
||||
process.env.ROLES_MODERATOR!
|
||||
];
|
||||
}
|
||||
|
||||
public override async execute(context: ICommandContext) {
|
||||
const targetUser = context.message.mentions.users.first();
|
||||
|
||||
if (!targetUser) {
|
||||
const embed = new ErrorEmbed(context, "User does not exist");
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const targetMember = context.message.guild?.member(targetUser);
|
||||
|
||||
if (!targetMember) {
|
||||
const embed = new ErrorEmbed(context, "User is not in this server");
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const reasonArgs = context.args;
|
||||
reasonArgs.splice(0, 1)
|
||||
|
||||
const reason = reasonArgs.join(" ");
|
||||
|
||||
if (!context.message.guild?.available) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!targetMember.bannable) {
|
||||
const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions);
|
||||
embed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const logEmbed = new LogEmbed(context, "Member Banned");
|
||||
logEmbed.AddUser("User", targetUser, true);
|
||||
logEmbed.AddUser("Moderator", context.message.author);
|
||||
logEmbed.AddReason(reason);
|
||||
|
||||
const publicEmbed = new PublicEmbed(context, "", `${targetUser} has been banned`);
|
||||
|
||||
await targetMember.ban({ reason: reason });
|
||||
|
||||
logEmbed.SendToModLogsChannel();
|
||||
publicEmbed.SendToCurrentChannel();
|
||||
}
|
||||
}
|
4
src/constants/ErrorMessages.ts
Normal file
4
src/constants/ErrorMessages.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export default class ErrorMessages {
|
||||
public static readonly InsufficientBotPermissions = "Unable to do this action, am I missing permissions?";
|
||||
public static readonly CantFindChannel = "Unable to find channel";
|
||||
}
|
19
src/helpers/ErrorEmbed.ts
Normal file
19
src/helpers/ErrorEmbed.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { MessageEmbed } from "discord.js";
|
||||
import { ICommandContext } from "vylbot-core";
|
||||
|
||||
export default class ErrorEmbed extends MessageEmbed {
|
||||
private _context: ICommandContext;
|
||||
|
||||
constructor(context: ICommandContext, message: String) {
|
||||
super();
|
||||
|
||||
super.setColor(process.env.EMBED_COLOUR_ERROR!);
|
||||
super.setDescription(message);
|
||||
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
public SendToCurrentChannel() {
|
||||
this._context.message.channel.send(this);
|
||||
}
|
||||
}
|
60
src/helpers/LogEmbed.ts
Normal file
60
src/helpers/LogEmbed.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { MessageEmbed, TextChannel, User } from "discord.js";
|
||||
import { ICommandContext } from "vylbot-core";
|
||||
import ErrorMessages from "../constants/ErrorMessages";
|
||||
import ErrorEmbed from "./ErrorEmbed";
|
||||
|
||||
export default class LogEmbed extends MessageEmbed {
|
||||
private _context: ICommandContext;
|
||||
|
||||
constructor(context: ICommandContext, title: string) {
|
||||
super();
|
||||
|
||||
super.setColor(process.env.ERROR_EMBED!);
|
||||
super.setTitle(title);
|
||||
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
// Detail methods
|
||||
public AddUser(title: string, user: User, setThumbnail: boolean = false) {
|
||||
super.addField(title, `${user} \`${user.tag}\``, true);
|
||||
|
||||
if (setThumbnail) {
|
||||
super.setThumbnail(user.displayAvatarURL());
|
||||
}
|
||||
}
|
||||
|
||||
public AddReason(message: String) {
|
||||
super.addField("Reason", message || "*none*");
|
||||
}
|
||||
|
||||
// Send methods
|
||||
public SendToCurrentChannel() {
|
||||
this._context.message.channel.send(this);
|
||||
}
|
||||
|
||||
public SendToChannel(name: string) {
|
||||
const channel = this._context.message.guild?.channels.cache
|
||||
.find(channel => channel.name == name) as TextChannel;
|
||||
|
||||
if (!channel) {
|
||||
const errorEmbed = new ErrorEmbed(this._context, ErrorMessages.CantFindChannel);
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
channel.send(this);
|
||||
}
|
||||
|
||||
public SendToMessageLogsChannel() {
|
||||
this.SendToChannel(process.env.CHANNELS_LOGS_MESSAGE!)
|
||||
}
|
||||
|
||||
public SendToMemberLogsChannel() {
|
||||
this.SendToChannel(process.env.CHANNELS_LOGS_MEMBER!)
|
||||
}
|
||||
|
||||
public SendToModLogsChannel() {
|
||||
this.SendToChannel(process.env.CHANNELS_LOGS_MOD!)
|
||||
}
|
||||
}
|
21
src/helpers/PublicEmbed.ts
Normal file
21
src/helpers/PublicEmbed.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { MessageEmbed } from "discord.js";
|
||||
import { ICommandContext } from "vylbot-core";
|
||||
|
||||
export default class PublicEmbed extends MessageEmbed {
|
||||
private _context: ICommandContext;
|
||||
|
||||
constructor(context: ICommandContext, title: string, description: string) {
|
||||
super();
|
||||
|
||||
super.setColor(process.env.ERROR_EMBED!);
|
||||
super.setTitle(title);
|
||||
super.setDescription(description);
|
||||
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
// Send methods
|
||||
public SendToCurrentChannel() {
|
||||
this._context.message.channel.send(this);
|
||||
}
|
||||
}
|
|
@ -3,7 +3,15 @@ import * as dotenv from "dotenv";
|
|||
|
||||
dotenv.config();
|
||||
|
||||
// Ensure required data is in dotenv
|
||||
if (!process.env.EMBED_COLOUR) throw "EMBED_COLOUR is required in .env";
|
||||
if (!process.env.EMBED_COLOUR_ERROR) throw "EMBED_COLOUR_ERROR is required in .env";
|
||||
|
||||
if (!process.env.ROLES_MODERATOR) throw "ROLES_MODERATOR is required in .env";
|
||||
|
||||
if (!process.env.CHANNELS_LOGS_MESSAGE) throw "CHANNELS_LOGS_MESSAGE is required in .env";
|
||||
if (!process.env.CHANNELS_LOGS_MEMBER) throw "CHANNELS_LOGS_MEMBER is required in .env";
|
||||
if (!process.env.CHANNELS_LOGS_MOD) throw "CHANNELS_LOGS_MOD is required in .env";
|
||||
|
||||
const client = new CoreClient();
|
||||
client.start();
|
Loading…
Add table
Add a link
Reference in a new issue