mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-11-02 22:19:49 +01:00
refactor(arabot): add function to remove duplicate code and add embeds for logs
This commit is contained in:
parent
dedd72c731
commit
ba99a6be4a
@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/*
|
||||
Animal Rights Advocates Discord Bot
|
||||
Copyright (C) 2022 Anthony Berg
|
||||
Copyright (C) 2023 Anthony Berg
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@ -18,10 +18,17 @@
|
||||
*/
|
||||
|
||||
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
|
||||
import type { User, Message, TextChannel } from 'discord.js';
|
||||
import type {
|
||||
User,
|
||||
Message,
|
||||
Snowflake,
|
||||
TextChannel,
|
||||
Guild,
|
||||
} from 'discord.js';
|
||||
import { EmbedBuilder } from 'discord.js';
|
||||
import IDs from '#utils/ids';
|
||||
import { addBan, checkActive } from '#utils/database/ban';
|
||||
import { addEmptyUser, addExistingUser, userExists } from '#utils/database/dbExistingUser';
|
||||
import { addEmptyUser, updateUser, userExists } from '#utils/database/dbExistingUser';
|
||||
|
||||
export class BanCommand extends Command {
|
||||
public constructor(context: Command.Context, options: Command.Options) {
|
||||
@ -54,13 +61,13 @@ export class BanCommand extends Command {
|
||||
// Command run
|
||||
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
|
||||
// Get the arguments
|
||||
const user = interaction.options.getUser('user');
|
||||
const reason = interaction.options.getString('reason');
|
||||
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 (user === null || guild === null || reason === null || mod === null) {
|
||||
if (guild === null || mod === null) {
|
||||
await interaction.reply({
|
||||
content: 'Error fetching user!',
|
||||
ephemeral: true,
|
||||
@ -69,87 +76,9 @@ export class BanCommand extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
// Gets mod's GuildMember
|
||||
const modGuildMember = guild.members.cache.get(mod.user.id);
|
||||
const ban = await this.ban(user.id, mod.user.id, reason, guild);
|
||||
|
||||
// Checks if guildMember is null
|
||||
if (modGuildMember === undefined) {
|
||||
await interaction.reply({
|
||||
content: 'Error fetching mod!',
|
||||
ephemeral: true,
|
||||
fetchReply: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (await checkActive(user.id)) {
|
||||
await interaction.reply(`${user} is already banned!`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if mod is in database
|
||||
if (!await userExists(modGuildMember.id)) {
|
||||
await addExistingUser(modGuildMember);
|
||||
}
|
||||
|
||||
// Gets guildMember
|
||||
let guildMember = guild.members.cache.get(user.id);
|
||||
|
||||
if (guildMember === undefined) {
|
||||
guildMember = await guild.members.fetch(user.id)
|
||||
.catch(() => undefined);
|
||||
}
|
||||
|
||||
if (guildMember !== undefined) {
|
||||
// Checks if the user is not restricted
|
||||
if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||
await interaction.reply({
|
||||
content: 'You need to restrict the user first!',
|
||||
ephemeral: true,
|
||||
fetchReply: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if user and mod are on the database
|
||||
if (!await userExists(guildMember.id)) {
|
||||
await addExistingUser(guildMember);
|
||||
}
|
||||
|
||||
// Send DM for reason of ban
|
||||
await user.send(`You have been banned from ARA for: ${reason}`
|
||||
+ '\n\nhttps://vbcamp.org/ARA')
|
||||
.catch(() => {});
|
||||
|
||||
// Ban the user
|
||||
await guildMember.ban({ reason });
|
||||
} else if (!await userExists(user.id)) {
|
||||
await addEmptyUser(user.id);
|
||||
}
|
||||
|
||||
await interaction.reply({
|
||||
content: `${user} has been banned.`,
|
||||
ephemeral: true,
|
||||
fetchReply: true,
|
||||
});
|
||||
|
||||
// Add ban to database
|
||||
await addBan(user.id, mod.user.id, reason);
|
||||
|
||||
// Log the ban
|
||||
let logChannel = guild.channels.cache
|
||||
.get(IDs.channels.logs.restricted) as TextChannel | undefined;
|
||||
|
||||
if (logChannel === undefined) {
|
||||
logChannel = await guild.channels
|
||||
.fetch(IDs.channels.logs.restricted) as TextChannel | undefined;
|
||||
if (logChannel === undefined) {
|
||||
this.container.logger.error('Ban Error: Could not fetch log channel');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await logChannel.send(`${user} was banned for: ${reason} by ${mod}`);
|
||||
await interaction.reply({ content: ban.message });
|
||||
}
|
||||
|
||||
// Non Application Command method of banning a user
|
||||
@ -186,62 +115,81 @@ export class BanCommand extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
if (await checkActive(user.id)) {
|
||||
await message.react('❌');
|
||||
await message.reply(`${user} is already banned!`);
|
||||
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!');
|
||||
+ 'or alternatively use the slash command!');
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if mod is in database
|
||||
if (!await userExists(mod.id)) {
|
||||
await addExistingUser(mod);
|
||||
const ban = await this.ban(user.id, mod.user.id, reason, guild);
|
||||
|
||||
await message.reply(ban.message);
|
||||
await message.react(ban.success ? '✅' : '❌');
|
||||
}
|
||||
|
||||
private async ban(userId: Snowflake, modId: Snowflake, reason: string, guild: Guild) {
|
||||
const info = {
|
||||
message: '',
|
||||
success: false,
|
||||
};
|
||||
|
||||
let user = guild.client.users.cache.get(userId);
|
||||
|
||||
if (user === undefined) {
|
||||
user = await guild.client.users.fetch(userId) as User;
|
||||
}
|
||||
|
||||
// Gets guildMember
|
||||
let guildMember = await guild.members.cache.get(user.id);
|
||||
// Gets mod's GuildMember
|
||||
const mod = guild.members.cache.get(modId);
|
||||
|
||||
if (guildMember === undefined) {
|
||||
guildMember = await guild.members.fetch(user.id)
|
||||
// Checks if guildMember is null
|
||||
if (mod === undefined) {
|
||||
info.message = 'Error fetching mod!';
|
||||
return info;
|
||||
}
|
||||
|
||||
if (await checkActive(userId)) {
|
||||
info.message = `${user} is already banned!`;
|
||||
return info;
|
||||
}
|
||||
|
||||
// Check if mod is in database
|
||||
await updateUser(mod);
|
||||
|
||||
// Gets guildMember
|
||||
let member = guild.members.cache.get(userId);
|
||||
|
||||
if (member === undefined) {
|
||||
member = await guild.members.fetch(userId)
|
||||
.catch(() => undefined);
|
||||
}
|
||||
|
||||
if (guildMember !== undefined) {
|
||||
if (member !== undefined) {
|
||||
// Checks if the user is not restricted
|
||||
if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||
await message.react('❌');
|
||||
await message.reply({
|
||||
content: 'You need to restrict the user first!',
|
||||
});
|
||||
return;
|
||||
if (member.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||
info.message = 'You need to restrict the user first!';
|
||||
return info;
|
||||
}
|
||||
|
||||
// Check if user and mod are on the database
|
||||
if (!await userExists(guildMember.id)) {
|
||||
await addExistingUser(guildMember);
|
||||
}
|
||||
await updateUser(member);
|
||||
|
||||
// Send DM for reason of ban
|
||||
await user.send(`You have been banned from ARA for: ${reason}`
|
||||
await member.send(`You have been banned from ARA for: ${reason}`
|
||||
+ '\n\nhttps://vbcamp.org/ARA')
|
||||
.catch(() => {});
|
||||
|
||||
// Ban the user
|
||||
await guildMember.ban({ reason });
|
||||
} else if (!await userExists(user.id)) {
|
||||
await addEmptyUser(user.id);
|
||||
await member.ban({ reason });
|
||||
} else if (!await userExists(userId)) {
|
||||
await addEmptyUser(userId);
|
||||
}
|
||||
|
||||
// Add ban to database
|
||||
await addBan(user.id, mod.id, reason);
|
||||
await addBan(userId, modId, reason);
|
||||
|
||||
await message.react('✅');
|
||||
info.message = `${user} has been banned.`;
|
||||
info.success = true;
|
||||
|
||||
// Log the ban
|
||||
let logChannel = guild.channels.cache
|
||||
@ -252,10 +200,24 @@ export class BanCommand extends Command {
|
||||
.fetch(IDs.channels.logs.restricted) as TextChannel | undefined;
|
||||
if (logChannel === undefined) {
|
||||
this.container.logger.error('Ban Error: Could not fetch log channel');
|
||||
return;
|
||||
info.message = `${user} has been banned. This hasn't been logged in a text channel as log channel could not be found`;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
await logChannel.send(`${user} was banned for: ${reason} by ${mod}`);
|
||||
const log = new EmbedBuilder()
|
||||
.setColor('#FF0000')
|
||||
.setAuthor({ name: `Banned ${user.tag}`, iconURL: `${user.avatarURL()}` })
|
||||
.addFields(
|
||||
{ name: 'User', value: `${user}`, inline: true },
|
||||
{ name: 'Moderator', value: `${mod}`, inline: true },
|
||||
{ name: 'Reason', value: reason },
|
||||
)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `ID: ${user.id}` });
|
||||
|
||||
await logChannel.send({ embeds: [log] });
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/*
|
||||
Animal Rights Advocates Discord Bot
|
||||
Copyright (C) 2022 Anthony Berg
|
||||
Copyright (C) 2023 Anthony Berg
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@ -21,9 +21,12 @@ import { Args, Command, RegisterBehavior } from '@sapphire/framework';
|
||||
import type {
|
||||
User,
|
||||
Message,
|
||||
Snowflake,
|
||||
TextChannel,
|
||||
Guild,
|
||||
GuildBan,
|
||||
} from 'discord.js';
|
||||
import { EmbedBuilder } from 'discord.js';
|
||||
import IDs from '#utils/ids';
|
||||
import { removeBan, checkActive, addBan } from '#utils/database/ban';
|
||||
import { addEmptyUser, addExistingUser, userExists } from '#utils/database/dbExistingUser';
|
||||
@ -56,12 +59,12 @@ export class UnbanCommand extends Command {
|
||||
// Command run
|
||||
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
|
||||
// Get the arguments
|
||||
const user = interaction.options.getUser('user');
|
||||
const user = interaction.options.getUser('user', true);
|
||||
const mod = interaction.member;
|
||||
const { guild } = interaction;
|
||||
|
||||
// Checks if all the variables are of the right type
|
||||
if (user === null || guild === null || mod === null) {
|
||||
if (guild === null || mod === null) {
|
||||
await interaction.reply({
|
||||
content: 'Error fetching user!',
|
||||
ephemeral: true,
|
||||
@ -70,80 +73,9 @@ export class UnbanCommand extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
// Gets mod's GuildMember
|
||||
const modGuildMember = guild.members.cache.get(mod.user.id);
|
||||
const unban = await this.unban(user.id, mod.user.id, guild);
|
||||
|
||||
// Checks if guildMember is null
|
||||
if (modGuildMember === undefined) {
|
||||
await interaction.reply({
|
||||
content: 'Error fetching mod!',
|
||||
ephemeral: true,
|
||||
fetchReply: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if mod is in database
|
||||
if (!await userExists(modGuildMember.id)) {
|
||||
await addExistingUser(modGuildMember);
|
||||
}
|
||||
|
||||
if (!await checkActive(user.id)) {
|
||||
let ban: GuildBan;
|
||||
try {
|
||||
ban = await guild.bans.fetch(user.id);
|
||||
} catch {
|
||||
try {
|
||||
ban = await guild.bans.fetch({ user, force: true });
|
||||
} catch {
|
||||
await interaction.reply({
|
||||
content: `${user} is not banned.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
let { reason } = ban;
|
||||
|
||||
if (reason === null || reason === undefined) {
|
||||
reason = '';
|
||||
}
|
||||
|
||||
// Check if user and mod are on the database
|
||||
if (!await userExists(user.id)) {
|
||||
await addEmptyUser(user.id);
|
||||
}
|
||||
|
||||
// Add missing ban
|
||||
await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`);
|
||||
}
|
||||
|
||||
// Unban the user
|
||||
await guild.members.unban(user)
|
||||
.catch(() => {});
|
||||
|
||||
// Add unban to database
|
||||
await removeBan(user.id, mod.user.id);
|
||||
|
||||
await interaction.reply({
|
||||
content: `${user} has been unbanned.`,
|
||||
ephemeral: true,
|
||||
fetchReply: true,
|
||||
});
|
||||
|
||||
// Log the ban
|
||||
let modRestrict = guild.channels.cache
|
||||
.get(IDs.channels.restricted.moderators) as TextChannel | undefined;
|
||||
|
||||
if (modRestrict === undefined) {
|
||||
modRestrict = await guild.channels
|
||||
.fetch(IDs.channels.restricted.moderators) as TextChannel | undefined;
|
||||
if (modRestrict === undefined) {
|
||||
this.container.logger.error('Unban Error: Could not fetch mod channel');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await modRestrict.send(`${user} was unbanned by ${mod}`);
|
||||
await interaction.reply({ content: unban.message });
|
||||
}
|
||||
|
||||
// Non Application Command method of banning a user
|
||||
@ -174,24 +106,48 @@ export class UnbanCommand extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
const unban = await this.unban(user.id, mod.user.id, guild);
|
||||
|
||||
await message.reply(unban.message);
|
||||
await message.react(unban.success ? '✅' : '❌');
|
||||
}
|
||||
|
||||
private async unban(userId: Snowflake, modId: Snowflake, guild: Guild) {
|
||||
const info = {
|
||||
message: '',
|
||||
success: false,
|
||||
};
|
||||
|
||||
// Gets mod's GuildMember
|
||||
const mod = guild.members.cache.get(modId);
|
||||
|
||||
// Checks if guildMember is null
|
||||
if (mod === undefined) {
|
||||
info.message = 'Error fetching mod!';
|
||||
return info;
|
||||
}
|
||||
|
||||
// Check if mod is in database
|
||||
if (!await userExists(mod.id)) {
|
||||
if (!await userExists(modId)) {
|
||||
await addExistingUser(mod);
|
||||
}
|
||||
|
||||
if (!await checkActive(user.id)) {
|
||||
let user = guild.client.users.cache.get(userId);
|
||||
|
||||
if (user === undefined) {
|
||||
user = await guild.client.users.fetch(userId) as User;
|
||||
}
|
||||
|
||||
if (!await checkActive(userId)) {
|
||||
let ban: GuildBan;
|
||||
try {
|
||||
ban = await guild.bans.fetch(user.id);
|
||||
ban = await guild.bans.fetch(userId);
|
||||
} catch {
|
||||
try {
|
||||
ban = await guild.bans.fetch({ user, force: true });
|
||||
} catch {
|
||||
await message.react('❌');
|
||||
await message.reply({
|
||||
content: `${user} is not banned.`,
|
||||
});
|
||||
return;
|
||||
info.message = `${user} is not banned.`;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
let { reason } = ban;
|
||||
@ -206,7 +162,7 @@ export class UnbanCommand extends Command {
|
||||
}
|
||||
|
||||
// Add missing ban
|
||||
await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`);
|
||||
await addBan(userId, modId, `(Mod who banned is not accurate) - ${reason}`);
|
||||
}
|
||||
|
||||
// Unban the user
|
||||
@ -214,27 +170,37 @@ export class UnbanCommand extends Command {
|
||||
.catch(() => {});
|
||||
|
||||
// Add unban to database
|
||||
await removeBan(user.id, mod.id);
|
||||
await removeBan(user.id, mod.user.id);
|
||||
|
||||
await message.react('✅');
|
||||
info.message = `${user} has been unbanned.`;
|
||||
info.success = true;
|
||||
|
||||
await message.reply({
|
||||
content: `${user} has been unbanned.`,
|
||||
});
|
||||
// Log unban
|
||||
let logChannel = guild.channels.cache
|
||||
.get(IDs.channels.logs.restricted) as TextChannel | undefined;
|
||||
|
||||
// Log the ban
|
||||
let modRestrict = guild.channels.cache
|
||||
.get(IDs.channels.restricted.moderators) as TextChannel | undefined;
|
||||
|
||||
if (modRestrict === undefined) {
|
||||
modRestrict = await guild.channels
|
||||
.fetch(IDs.channels.restricted.moderators) as TextChannel | undefined;
|
||||
if (modRestrict === undefined) {
|
||||
this.container.logger.error('Unban Error: Could not fetch mod channel');
|
||||
return;
|
||||
if (logChannel === undefined) {
|
||||
logChannel = await guild.channels
|
||||
.fetch(IDs.channels.logs.restricted) as TextChannel | undefined;
|
||||
if (logChannel === undefined) {
|
||||
this.container.logger.error('Ban Error: Could not fetch log channel');
|
||||
info.message = `${user} has been banned. This hasn't been logged in a text channel as log channel could not be found`;
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
await modRestrict.send(`${user} was unbanned by ${mod}`);
|
||||
const log = new EmbedBuilder()
|
||||
.setColor('#28A745')
|
||||
.setAuthor({ name: `Unbanned ${user.tag}`, iconURL: `${user.avatarURL()}` })
|
||||
.addFields(
|
||||
{ name: 'User', value: `${user}`, inline: true },
|
||||
{ name: 'Moderator', value: `${mod}`, inline: true },
|
||||
)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `ID: ${user.id}` });
|
||||
|
||||
await logChannel.send({ embeds: [log] });
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user