From 5fb9ae39a0d5ab1864231c27d563746a5b8cf35f Mon Sep 17 00:00:00 2001 From: smyalygames Date: Thu, 23 Feb 2023 02:03:50 +0000 Subject: [PATCH] feat(arabot): add message command for temp ban --- package.json | 2 +- src/commands/mod/ban/tempBan.ts | 94 ++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 25e2482..0bc6cda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arabot", - "version": "0.2.3", + "version": "0.2.4", "description": "A Discord bot for Animal Rights Advocates", "main": "dist/index.js", "scripts": { diff --git a/src/commands/mod/ban/tempBan.ts b/src/commands/mod/ban/tempBan.ts index 760e9eb..56e3459 100644 --- a/src/commands/mod/ban/tempBan.ts +++ b/src/commands/mod/ban/tempBan.ts @@ -17,7 +17,7 @@ along with this program. If not, see . */ -import { Command, RegisterBehavior } from '@sapphire/framework'; +import { Args, Command, RegisterBehavior } from '@sapphire/framework'; import { Duration, DurationFormatter } from '@sapphire/time-utilities'; import type { User, @@ -25,7 +25,7 @@ import type { TextChannel, Guild, } from 'discord.js'; -import { EmbedBuilder } from 'discord.js'; +import { EmbedBuilder, Message } from 'discord.js'; import IDs from '#utils/ids'; import { addTempBan, checkTempBan } from '#utils/database/tempBan'; import { addEmptyUser, updateUser, userExists } from '#utils/database/dbExistingUser'; @@ -94,6 +94,96 @@ export class TempBanCommand extends Command { await interaction.reply({ content: ban.message }); } + // Non Application Command method of banning a user + public async messageRun(message: Message, args: Args) { + // Get arguments + let user: User; + try { + user = await args.pick('user'); + } catch { + await message.react('❌'); + await message.reply('User was not provided!'); + return; + } + const arg = args.finished ? null : await args.rest('string'); + const mod = message.member; + + if (arg === null) { + await message.react('❌'); + await message.reply('Ban reason was not provided!'); + return; + } + + if (mod === null) { + await message.react('❌'); + await message.reply('Moderator not found! Try again or contact a developer!'); + return; + } + + const { duration, reason } = this.findTimeAndReason(arg); + + if (Number.isNaN(duration.offset)) { + await message.react('❌'); + await message.reply('Invalid time length for ban!'); + return; + } + + if (reason.length === 0) { + await message.react('❌'); + await message.reply('Reason was not provided!'); + return; + } + + const { guild } = message; + + if (guild === null) { + await message.react('❌'); + await message.reply('Guild not found! Try again or contact a developer!'); + return; + } + + if (message.channel.id !== IDs.channels.restricted.moderators) { + await message.react('❌'); + await message.reply(`You can only run this command in <#${IDs.channels.restricted.moderators}> ` + + 'or alternatively use the slash command!'); + return; + } + + const ban = await this.ban(user.id, mod.user.id, duration, reason, guild); + + await message.reply(ban.message); + await message.react(ban.success ? '✅' : '❌'); + } + + private findTimeAndReason(args: string) { + const info = { + duration: new Duration(''), + reason: '', + }; + + if (Number.isNaN(new Duration(args).offset)) { + return info; + } + + const spliced = args.split(' '); + let time = ''; + for (let i = 0; i < spliced.length; i += 1) { + if (!Number.isNaN(new Duration(spliced[i]).offset)) { + time += spliced[i]; + } else { + info.reason = args.slice(args.indexOf(spliced[i])); + break; + } + } + + if (time.length === 0) { + return info; + } + + info.duration = new Duration(time); + return info; + } + private async ban( userId: Snowflake, modId: Snowflake,