refactor(arabot): make restrictRun exported function

This commit is contained in:
smyalygames 2023-02-11 21:23:02 +00:00
parent 733a6609a8
commit a79cc496d8

View File

@ -17,7 +17,12 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Args, Command, RegisterBehavior } from '@sapphire/framework'; import {
Args,
Command,
RegisterBehavior,
container,
} from '@sapphire/framework';
import { import {
ChannelType, ChannelType,
EmbedBuilder, EmbedBuilder,
@ -35,102 +40,13 @@ import IDs from '#utils/ids';
import { addEmptyUser, updateUser, userExists } from '#utils/database/dbExistingUser'; import { addEmptyUser, updateUser, userExists } from '#utils/database/dbExistingUser';
import { restrict, checkActive } from '#utils/database/restriction'; import { restrict, checkActive } from '#utils/database/restriction';
export class RestrictCommand extends Command { export async function restrictRun(
public constructor(context: Command.Context, options: Command.Options) { userId: Snowflake,
super(context, { modId: Snowflake,
...options, reason: string,
name: 'restrict', guild: Guild,
aliases: ['r', 'rest', 'rr', 'rv'], tolerance = false,
description: 'Restricts a user', ) {
preconditions: ['ModOnly'],
});
}
// Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(
(builder) => builder
.setName(this.name)
.setDescription(this.description)
.addUserOption((option) => option.setName('user')
.setDescription('User to restrict')
.setRequired(true))
.addStringOption((option) => option.setName('reason')
.setDescription('Reason for restricting the user')
.setRequired(true)),
{
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
);
}
// Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
}
const info = await this.restrictRun(user?.id, mod.user.id, reason, guild);
await interaction.reply({
content: info.message,
fetchReply: true,
});
}
// 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 reason = args.finished ? null : await args.rest('string');
const mod = message.member;
if (reason === null) {
await message.react('❌');
await message.reply('Restrict 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 { guild } = message;
if (guild === null) {
await message.react('❌');
await message.reply('Guild not found! Try again or contact a developer!');
return;
}
const info = await this.restrictRun(user?.id, mod.user.id, reason, guild);
await message.reply(info.message);
await message.react(info.success ? '✅' : '❌');
}
private async restrictRun(userId: Snowflake, modId: Snowflake, reason: string, guild: Guild) {
const info = { const info = {
message: '', message: '',
success: false, success: false,
@ -272,6 +188,10 @@ export class RestrictCommand extends Command {
); );
await restrictedChannel.send({ embeds: [embed] }); await restrictedChannel.send({ embeds: [embed] });
} else if (tolerance) {
await member.roles.add(Math.random() > 0.5
? IDs.roles.restrictions.restricted3
: IDs.roles.restrictions.restricted4);
} else { } else {
await member.roles.add(Math.random() > 0.5 await member.roles.add(Math.random() > 0.5
? IDs.roles.restrictions.restricted1 ? IDs.roles.restrictions.restricted1
@ -304,7 +224,7 @@ export class RestrictCommand extends Command {
logChannel = await guild.channels logChannel = await guild.channels
.fetch(IDs.channels.logs.restricted) as TextChannel | undefined; .fetch(IDs.channels.logs.restricted) as TextChannel | undefined;
if (logChannel === undefined) { if (logChannel === undefined) {
this.container.logger.error('Restrict Error: Could not fetch log channel'); container.logger.error('Restrict Error: Could not fetch log channel');
info.message = `Restricted ${member} but could not find the log channel. This has been logged to the database.`; info.message = `Restricted ${member} but could not find the log channel. This has been logged to the database.`;
return info; return info;
} }
@ -325,5 +245,100 @@ export class RestrictCommand extends Command {
info.message = `Restricted ${member}`; info.message = `Restricted ${member}`;
return info; return info;
}
export class RestrictCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: 'restrict',
aliases: ['r', 'rest', 'rr', 'rv'],
description: 'Restricts a user',
preconditions: ['ModOnly'],
});
}
// Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(
(builder) => builder
.setName(this.name)
.setDescription(this.description)
.addUserOption((option) => option.setName('user')
.setDescription('User to restrict')
.setRequired(true))
.addStringOption((option) => option.setName('reason')
.setDescription('Reason for restricting the user')
.setRequired(true)),
{
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
);
}
// Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// Get the arguments
const user = interaction.options.getUser('user', true);
const reason = interaction.options.getString('reason', true);
const mod = interaction.member;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null || mod === null) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
}
const info = await restrictRun(user?.id, mod.user.id, reason, guild);
await interaction.reply({
content: info.message,
fetchReply: true,
});
}
// 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 reason = args.finished ? null : await args.rest('string');
const mod = message.member;
if (reason === null) {
await message.react('❌');
await message.reply('Restrict 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 { guild } = message;
if (guild === null) {
await message.react('❌');
await message.reply('Guild not found! Try again or contact a developer!');
return;
}
const info = await restrictRun(user?.id, mod.user.id, reason, guild);
await message.reply(info.message);
await message.react(info.success ? '✅' : '❌');
} }
} }