mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-19 15:04:29 +02:00
feat(arabot): add restrict command
This commit is contained in:
parent
c0c1c16c37
commit
5ed15e6206
@ -18,16 +18,29 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
|
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
|
||||||
import type { User, Message, TextChannel } from 'discord.js';
|
import {
|
||||||
|
ChannelType,
|
||||||
|
EmbedBuilder,
|
||||||
|
PermissionsBitField,
|
||||||
|
time,
|
||||||
|
} from 'discord.js';
|
||||||
|
import type {
|
||||||
|
User,
|
||||||
|
Message,
|
||||||
|
TextChannel,
|
||||||
|
Guild,
|
||||||
|
Snowflake,
|
||||||
|
} from 'discord.js';
|
||||||
import IDs from '#utils/ids';
|
import IDs from '#utils/ids';
|
||||||
import { addEmptyUser, addExistingUser, userExists } from '#utils/database/dbExistingUser';
|
import { addEmptyUser, addExistingUser, userExists } from '#utils/database/dbExistingUser';
|
||||||
|
import { restrict, checkActive } from '#utils/database/restriction';
|
||||||
|
|
||||||
export class RestrictCommand extends Command {
|
export class RestrictCommand extends Command {
|
||||||
public constructor(context: Command.Context, options: Command.Options) {
|
public constructor(context: Command.Context, options: Command.Options) {
|
||||||
super(context, {
|
super(context, {
|
||||||
...options,
|
...options,
|
||||||
name: 'restrict',
|
name: 'restrict',
|
||||||
aliases: ['r', 'rest', 'rr'],
|
aliases: ['r', 'rest', 'rr', 'rv'],
|
||||||
description: 'Restricts a user',
|
description: 'Restricts a user',
|
||||||
preconditions: ['ModOnly'],
|
preconditions: ['ModOnly'],
|
||||||
});
|
});
|
||||||
@ -69,81 +82,12 @@ export class RestrictCommand extends Command {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets mod's GuildMember
|
const info = await this.restrictRun(user?.id, mod.user.id, reason, guild);
|
||||||
const modGuildMember = guild.members.cache.get(mod.user.id);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets guildMember
|
|
||||||
let guildMember = guild.members.cache.get(user.id);
|
|
||||||
|
|
||||||
if (guildMember === undefined) {
|
|
||||||
guildMember = await guild.members.fetch(user.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
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({
|
await interaction.reply({
|
||||||
content: `${user} has been banned.`,
|
content: info.message,
|
||||||
ephemeral: true,
|
|
||||||
fetchReply: 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}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non Application Command method of banning a user
|
// Non Application Command method of banning a user
|
||||||
@ -180,17 +124,25 @@ export class RestrictCommand extends Command {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await checkActive(user.id)) {
|
const info = await this.restrictRun(user?.id, mod.user.id, reason, guild);
|
||||||
await message.react('❌');
|
|
||||||
await message.reply(`${user} is already banned!`);
|
await message.reply(info.message);
|
||||||
return;
|
await message.react(info.success ? '✅' : '❌');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.channel.id !== IDs.channels.restricted.moderators) {
|
private async restrictRun(userId: Snowflake, modId: Snowflake, reason: string, guild: Guild) {
|
||||||
await message.react('❌');
|
const info = {
|
||||||
await message.reply(`You can only run this command in <#${IDs.channels.restricted.moderators}> `
|
message: '',
|
||||||
+ 'or alternatively use the slash command!');
|
success: false,
|
||||||
return;
|
};
|
||||||
|
|
||||||
|
// 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
|
// Check if mod is in database
|
||||||
@ -198,43 +150,155 @@ export class RestrictCommand extends Command {
|
|||||||
await addExistingUser(mod);
|
await addExistingUser(mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets guildMember
|
if (await checkActive(userId)) {
|
||||||
let guildMember = await guild.members.cache.get(user.id);
|
info.message = `<@${userId}> is already restricted!`;
|
||||||
|
return info;
|
||||||
if (guildMember === undefined) {
|
|
||||||
guildMember = await guild.members.fetch(user.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guildMember !== undefined) {
|
// Gets guildMember
|
||||||
|
let member = guild.members.cache.get(userId);
|
||||||
|
|
||||||
|
if (member === undefined) {
|
||||||
|
member = await guild.members.fetch(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (member !== undefined) {
|
||||||
// Checks if the user is not restricted
|
// Checks if the user is not restricted
|
||||||
if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)) {
|
if (member.roles.cache.hasAny(
|
||||||
await message.react('❌');
|
IDs.roles.restrictions.restricted1,
|
||||||
await message.reply({
|
IDs.roles.restrictions.restricted2,
|
||||||
content: 'You need to restrict the user first!',
|
IDs.roles.restrictions.restricted3,
|
||||||
});
|
IDs.roles.restrictions.restricted4,
|
||||||
return;
|
)) {
|
||||||
|
info.message = `${member} is already restricted!`;
|
||||||
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user and mod are on the database
|
// Check if user and mod are on the database
|
||||||
if (!await userExists(guildMember.id)) {
|
if (!await userExists(member.id)) {
|
||||||
await addExistingUser(guildMember);
|
await addExistingUser(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send DM for reason of ban
|
if (member.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||||
await user.send(`You have been banned from ARA for: ${reason}`
|
await member.roles.add(IDs.roles.restrictions.restricted1);
|
||||||
+ '\n\nhttps://vbcamp.org/ARA')
|
|
||||||
.catch(() => {});
|
|
||||||
|
|
||||||
// Ban the user
|
const voiceChannel = await guild.channels.create({
|
||||||
await guildMember.ban({ reason });
|
name: 'Restricted Voice Channel',
|
||||||
} else if (!await userExists(user.id)) {
|
type: ChannelType.GuildVoice,
|
||||||
await addEmptyUser(user.id);
|
parent: IDs.categories.restricted,
|
||||||
|
permissionOverwrites: [
|
||||||
|
{
|
||||||
|
id: guild.roles.everyone,
|
||||||
|
deny: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: member.id,
|
||||||
|
allow: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: IDs.roles.staff.restricted,
|
||||||
|
allow: [PermissionsBitField.Flags.SendMessages,
|
||||||
|
PermissionsBitField.Flags.ViewChannel,
|
||||||
|
PermissionsBitField.Flags.Connect,
|
||||||
|
PermissionsBitField.Flags.MuteMembers],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
let restrictedChannel: TextChannel;
|
||||||
|
let bannedName = false;
|
||||||
|
try {
|
||||||
|
restrictedChannel = await guild.channels.create({
|
||||||
|
name: `⛔┃${member.user.username}-restricted`,
|
||||||
|
type: ChannelType.GuildText,
|
||||||
|
topic: `Restricted channel. ${member.id} ${voiceChannel.id} (Please do not change this)`,
|
||||||
|
parent: IDs.categories.private,
|
||||||
|
permissionOverwrites: [
|
||||||
|
{
|
||||||
|
id: guild.roles.everyone,
|
||||||
|
allow: [PermissionsBitField.Flags.ReadMessageHistory],
|
||||||
|
deny: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: member.id,
|
||||||
|
allow: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: IDs.roles.staff.restricted,
|
||||||
|
allow: [PermissionsBitField.Flags.SendMessages,
|
||||||
|
PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
restrictedChannel = await guild.channels.create({
|
||||||
|
name: `⛔┃${member.user.id}-restricted`,
|
||||||
|
type: ChannelType.GuildText,
|
||||||
|
topic: `Restricted channel. ${member.id} ${voiceChannel.id} (Please do not change this)`,
|
||||||
|
parent: IDs.categories.private,
|
||||||
|
permissionOverwrites: [
|
||||||
|
{
|
||||||
|
id: guild.roles.everyone,
|
||||||
|
allow: [PermissionsBitField.Flags.ReadMessageHistory],
|
||||||
|
deny: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: member.id,
|
||||||
|
allow: [PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: IDs.roles.staff.restricted,
|
||||||
|
allow: [PermissionsBitField.Flags.SendMessages,
|
||||||
|
PermissionsBitField.Flags.ViewChannel],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
bannedName = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add ban to database
|
if (!bannedName) {
|
||||||
await addBan(user.id, mod.id, reason);
|
await voiceChannel.setName(`${member.user.username}-restricted`);
|
||||||
|
} else {
|
||||||
|
await voiceChannel.setName(`${member.user.id}-restricted`);
|
||||||
|
}
|
||||||
|
|
||||||
await message.react('✅');
|
const joinTime = time(member.joinedAt!);
|
||||||
|
const registerTime = time(member.user.createdAt);
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setColor(member.displayHexColor)
|
||||||
|
.setTitle(`Private channel for ${member.user.username}`)
|
||||||
|
.setDescription(`${member}`)
|
||||||
|
.setThumbnail(member.user.avatarURL()!)
|
||||||
|
.addFields(
|
||||||
|
{ name: 'Joined:', value: `${joinTime}`, inline: true },
|
||||||
|
{ name: 'Created:', value: `${registerTime}`, inline: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
await restrictedChannel.send({ embeds: [embed] });
|
||||||
|
} else {
|
||||||
|
await member.roles.add(Math.random() > 0.5
|
||||||
|
? IDs.roles.restrictions.restricted1
|
||||||
|
: IDs.roles.restrictions.restricted2);
|
||||||
|
}
|
||||||
|
|
||||||
|
await member.roles.remove([
|
||||||
|
IDs.roles.vegan.vegan,
|
||||||
|
IDs.roles.vegan.plus,
|
||||||
|
IDs.roles.vegan.activist,
|
||||||
|
IDs.roles.trusted,
|
||||||
|
IDs.roles.nonvegan.nonvegan,
|
||||||
|
IDs.roles.nonvegan.convinced,
|
||||||
|
IDs.roles.nonvegan.vegCurious,
|
||||||
|
]);
|
||||||
|
} else if (!await userExists(userId)) {
|
||||||
|
await addEmptyUser(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restrict the user on the database
|
||||||
|
await restrict(userId, modId, reason);
|
||||||
|
|
||||||
|
info.success = true;
|
||||||
|
|
||||||
// Log the ban
|
// Log the ban
|
||||||
let logChannel = guild.channels.cache
|
let logChannel = guild.channels.cache
|
||||||
@ -244,11 +308,26 @@ 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('Ban Error: Could not fetch log channel');
|
this.container.logger.error('Restrict Error: Could not fetch log channel');
|
||||||
return;
|
info.message = `Restricted ${member} but could not find the log channel. This has been logged to the database.`;
|
||||||
|
return info;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await logChannel.send(`${user} was banned for: ${reason} by ${mod}`);
|
const message = new EmbedBuilder()
|
||||||
|
.setColor('#FF6700')
|
||||||
|
.setAuthor({ name: `Restricted ${member.user.tag}`, iconURL: `${member.user.avatarURL()}` })
|
||||||
|
.addFields(
|
||||||
|
{ name: 'User', value: `${member}` },
|
||||||
|
{ name: 'Moderator', value: `${mod}` },
|
||||||
|
)
|
||||||
|
.addFields({ name: 'Reason', value: reason })
|
||||||
|
.setTimestamp()
|
||||||
|
.setFooter({ text: `ID: ${member.id}` });
|
||||||
|
|
||||||
|
await logChannel.send({ embeds: [message] });
|
||||||
|
|
||||||
|
info.message = `Restricted ${member}`;
|
||||||
|
return info;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { container } from '@sapphire/framework';
|
import { container } from '@sapphire/framework';
|
||||||
|
import type { Snowflake } from 'discord.js';
|
||||||
|
|
||||||
export async function addRestriction(userId: string, modId: string, reason: string) {
|
export async function restrict(userId: Snowflake, modId: Snowflake, reason: string) {
|
||||||
|
// Add the user to the database
|
||||||
await container.database.restrict.create({
|
await container.database.restrict.create({
|
||||||
data: {
|
data: {
|
||||||
user: {
|
user: {
|
||||||
@ -18,62 +20,55 @@ export async function addRestriction(userId: string, modId: string, reason: stri
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeRestriction(userId: string, modId: string) {
|
export async function unRestrict(userId: Snowflake, modId: Snowflake) {
|
||||||
const restrict = await container.database.restrict.findFirst({
|
const restriction = await container.database.restrict.findFirst({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
id: 'desc',
|
id: 'desc',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (restrict === null) {
|
if (restriction === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query to deactivate the specific sus note
|
// Query to deactivate the specific sus note
|
||||||
await container.database.restrict.update({
|
await container.database.restrict.update({
|
||||||
where: {
|
where: {
|
||||||
id: restrict.id,
|
id: restriction.id,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
endModId: modId,
|
endMod: {
|
||||||
|
connect: {
|
||||||
|
id: modId,
|
||||||
|
},
|
||||||
|
},
|
||||||
endTime: new Date(),
|
endTime: new Date(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkActive(userId: string) {
|
export async function checkActive(userId: Snowflake) {
|
||||||
const restrict = await container.database.ban.findFirst({
|
const restriction = await container.database.restrict.findFirst({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
endTime: true,
|
||||||
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
id: 'desc',
|
id: 'desc',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (restrict === null) {
|
if (restriction === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return restrict.active;
|
return restriction.endTime === null;
|
||||||
}
|
|
||||||
|
|
||||||
export async function getReason(userId: string) {
|
|
||||||
const restrict = await container.database.restrict.findFirst({
|
|
||||||
where: {
|
|
||||||
userId,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
id: 'desc',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (restrict === null) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return restrict.reason;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user