diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-06 18:30:23 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-07-06 18:30:23 -0400 |
commit | c260809dadd1f45107a2b70ab6cb6c3ce12d1160 (patch) | |
tree | 67293be341953e00f6eaf9e3f56121cbd6ddb8b9 /src/lib/extensions | |
parent | 1dee5bcda6a43eaa7fcc88ed3b0e458f32104de4 (diff) | |
download | tanzanite-c260809dadd1f45107a2b70ab6cb6c3ce12d1160.tar.gz tanzanite-c260809dadd1f45107a2b70ab6cb6c3ce12d1160.tar.bz2 tanzanite-c260809dadd1f45107a2b70ab6cb6c3ce12d1160.zip |
kick command
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 55 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushSlashMessage.ts | 3 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 74 |
3 files changed, 104 insertions, 28 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 20ce365..4a38b3e 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -35,6 +35,7 @@ import { BushGuildMemberResolvable, BushGuildResolvable, BushMessage, + BushSlashMessage, Global, Guild, ModLog, @@ -287,7 +288,7 @@ export class BushClientUtil extends ClientUtil { /** Paginates an array of embeds using buttons. */ public async buttonPaginate( - message: BushMessage, + message: BushMessage | BushSlashMessage, embeds: MessageEmbed[], text: string | null = null, deleteOnExit?: boolean @@ -397,7 +398,7 @@ export class BushClientUtil extends ClientUtil { } /** Sends a message with a button for the user to delete it. */ - public async sendWithDeleteButton(message: BushMessage, options: MessageOptions): Promise<void> { + public async sendWithDeleteButton(message: BushMessage | BushSlashMessage, options: MessageOptions): Promise<void> { updateOptions(); const msg = await message.util.reply(options as MessageOptions & { split?: false }); const filter = (interaction: ButtonInteraction) => interaction.customID == 'paginate__stop' && interaction.message == msg; @@ -565,30 +566,45 @@ export class BushClientUtil extends ClientUtil { * Checks if a moderator can perform a moderation action on another user. * @param moderator - The person trying to perform the action. * @param victim - The person getting punished. + * @param type - The type of punishment - used to format the response. * @param checkModerator - Whether or not to check if the victim is a moderator. */ public moderationPermissionCheck( moderator: BushGuildMember, victim: BushGuildMember, + type: 'mute' | 'unmute' | 'warn' | 'kick' | 'ban' | 'unban' | 'add a punishment role to' | 'remove a punishment role from', checkModerator = true - ): true | 'user hierarchy' | 'client hierarchy' | 'moderator' | 'self' { - if (moderator.guild.id !== victim.guild.id) throw 'wtf'; + ): true | string { + if (moderator.guild.id !== victim.guild.id) { + throw 'moderator and victim not in same guild'; + } const isOwner = moderator.guild.ownerID === moderator.id; - if (moderator.id === victim.id) return 'self'; - if (moderator.roles.highest.position <= victim.roles.highest.position && !isOwner) return 'user hierarchy'; - if (victim.roles.highest.position >= victim.guild.me.roles.highest.position) return 'client hierarchy'; - if (checkModerator && victim.permissions.has('MANAGE_MESSAGES')) return 'moderator'; + if (moderator.id === victim.id) { + return `${this.client.util.emojis.error} You cannot ${type} yourself.`; + } + if (moderator.roles.highest.position <= victim.roles.highest.position && !isOwner) { + return `${this.client.util.emojis.error} You cannot ${type} **${victim.user.tag}** because they have higher or equal role hierarchy as you do.`; + } + if (victim.roles.highest.position >= victim.guild.me.roles.highest.position) { + return `${this.client.util.emojis.error} You cannot ${type} **${victim.user.tag}** because they have higher or equal role hierarchy as I do.`; + } + if (checkModerator && victim.permissions.has('MANAGE_MESSAGES')) { + return `${this.client.util.emojis.error} You cannot ${type} **${victim.user.tag}** because they are a moderator.`; + } return true; } - public async createModLogEntry(options: { - type: ModLogType; - user: BushGuildMemberResolvable; - moderator: BushGuildMemberResolvable; - reason: string; - duration: number; - guild: BushGuildResolvable; - }): Promise<ModLog> { + public async createModLogEntry( + options: { + type: ModLogType; + user: BushGuildMemberResolvable; + moderator: BushGuildMemberResolvable; + reason: string; + duration?: number; + guild: BushGuildResolvable; + }, + getCaseNumber = false + ): Promise<{ log: ModLog; caseNum: number }> { const user = this.client.users.resolveID(options.user); const moderator = this.client.users.resolveID(options.moderator); const guild = this.client.guilds.resolveID(options.guild); @@ -612,10 +628,15 @@ export class BushClientUtil extends ClientUtil { duration: duration, guild }); - return modLogEntry.save().catch((e) => { + const saveResult: ModLog = await modLogEntry.save().catch((e) => { this.client.console.error('createModLogEntry', e?.stack || e); return null; }); + + if (!getCaseNumber) return { log: saveResult, caseNum: null }; + + const caseNum = (await ModLog.findAll({ where: { type: options.type, user: options.user, guild: options.guild } }))?.length; + return { log: saveResult, caseNum }; } public async createPunishmentEntry(options: { diff --git a/src/lib/extensions/discord-akairo/BushSlashMessage.ts b/src/lib/extensions/discord-akairo/BushSlashMessage.ts index cf2f391..63358b0 100644 --- a/src/lib/extensions/discord-akairo/BushSlashMessage.ts +++ b/src/lib/extensions/discord-akairo/BushSlashMessage.ts @@ -1,12 +1,13 @@ import { AkairoMessage } from 'discord-akairo'; import { CommandInteraction } from 'discord.js'; -import { BushClient, BushCommandUtil, BushGuild, BushUser } from '..'; +import { BushClient, BushCommandUtil, BushGuild, BushGuildMember, BushUser } from '..'; export class BushSlashMessage extends AkairoMessage { public declare client: BushClient; public declare util: BushCommandUtil; public declare guild: BushGuild; public declare author: BushUser; + public declare member: BushGuildMember; public constructor( client: BushClient, interaction: CommandInteraction, diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index 54b26f0..e7f1ddf 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -15,7 +15,7 @@ interface BushPunishmentRoleOptions extends BushTimedPunishmentOptions { role: RoleResolvable; } -type PunishmentResponse = 'success'; +type PunishmentResponse = 'success' | 'error creating modlog entry' | 'failed to dm'; type WarnResponse = PunishmentResponse; @@ -28,13 +28,11 @@ type MuteResponse = | 'invalid mute role' | 'mute role not manageable' | 'error giving mute role' - | 'error creating modlog entry' - | 'error creating mute entry' - | 'failed to dm'; + | 'error creating mute entry'; type UnmuteResponse = PunishmentResponse; -type KickResponse = PunishmentResponse; +type KickResponse = PunishmentResponse | 'missing permissions' | 'error kicking'; interface BushBanOptions extends BushTimedPunishmentOptions { deleteDays?: number; @@ -51,8 +49,33 @@ export class BushGuildMember extends GuildMember { super(client, data, guild); } - public async warn(options: BushPunishmentOptions): Promise<WarnResponse> { - throw 'not implemented'; + public async warn(options: BushPunishmentOptions): Promise<{ result: WarnResponse; caseNum: number }> { + //add modlog entry + const { log, caseNum } = await this.client.util + .createModLogEntry( + { + type: ModLogType.WARN, + user: this, + moderator: options.moderator, + reason: options.reason, + guild: this.guild + }, + true + ) + .catch(() => null); + if (!log) return { result: 'error creating modlog entry', caseNum: null }; + + //dm user + const ending = this.guild.getSetting('punishmentEnding'); + const dmSuccess = await this.send({ + content: `You have been warned in **${this.guild}** for **${options.reason || 'No reason provided'}**.${ + ending ? `\n\n${ending}` : '' + }` + }).catch(() => null); + + if (!dmSuccess) return { result: 'failed to dm', caseNum }; + + return { result: 'success', caseNum }; } public punishRole(options: BushPunishmentRoleOptions): Promise<PunishmentRoleResponse> { @@ -68,9 +91,13 @@ export class BushGuildMember extends GuildMember { if (!muteRole) return 'invalid mute role'; if (muteRole.position >= this.guild.me.roles.highest.position || muteRole.managed) return 'mute role not manageable'; + const moderator = this.client.users.cache.get(this.client.users.resolveID(options.moderator)); + //add role - const success = await this.roles.add(muteRole).catch(() => null); - if (!success) return 'error giving mute role'; + const muteSuccess = await this.roles + .add(muteRole, `[Mute] ${moderator.tag} | ${options.reason || 'No reason provided.'}`) + .catch(() => null); + if (!muteSuccess) return 'error giving mute role'; //add modlog entry const modlog = await this.client.util @@ -115,7 +142,34 @@ export class BushGuildMember extends GuildMember { } public async bushKick(options: BushPunishmentOptions): Promise<KickResponse> { - throw 'not implemented'; + //checks + if (!this.guild.me.permissions.has('KICK_MEMBERS') || !this.kickable) return 'missing permissions'; + + //dm user + const ending = this.guild.getSetting('punishmentEnding'); + const dmSuccess = await this.send({ + content: `You have been kicked from **${this.guild}** for **${options.reason || 'No reason provided'}**.${ + ending ? `\n\n${ending}` : '' + }` + }).catch(() => null); + + //Kick + const kickSuccess = await this.kick().catch(() => null); + if (!kickSuccess) return 'error kicking'; + + //add modlog entry + const modlog = await this.client.util + .createModLogEntry({ + type: ModLogType.KICK, + user: this, + moderator: options.moderator, + reason: options.reason, + guild: this.guild + }) + .catch(() => null); + if (!modlog) return 'error creating modlog entry'; + if (!dmSuccess) return 'failed to dm'; + return 'success'; } public async bushBan(options?: BushBanOptions): Promise<BanResponse> { |