Merge pull request #77 from veganhacktivists/main

update restrict branch
This commit is contained in:
Anthony Berg
2023-02-11 16:13:48 +00:00
committed by GitHub
8 changed files with 490 additions and 32 deletions

View File

@@ -0,0 +1,119 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Animal Rights Advocates Discord Bot
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { GuildMember, Message } from 'discord.js';
import IDs from '#utils/ids';
export class SoftMuteCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: 'softmute',
aliases: ['sm'],
description: 'Prevent a user from reacting to a message by giving '
+ 'the soft mute role',
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 soft mute')
.setRequired(true)),
{
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
);
}
// Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// TODO add database updates
// Get the arguments
const user = interaction.options.getUser('user', true);
const { guild } = interaction;
// Checks if all the variables are of the right type
if (guild === null) {
await interaction.reply({
content: 'Error fetching the guild!',
ephemeral: true,
fetchReply: true,
});
return;
}
// Gets guildMember whilst removing the ability of each other variables being null
const guildMember = guild.members.cache.get(user.id);
// Checks if guildMember is null
if (guildMember === undefined) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
}
if (guildMember.roles.cache.has(IDs.roles.restrictions.softMute)) {
await guildMember.roles.remove(IDs.roles.restrictions.softMute);
await interaction.reply({
content: `Removed soft muted for ${user}`,
fetchReply: true,
});
return;
}
await guildMember.roles.add(IDs.roles.restrictions.softMute);
await interaction.reply({
content: `Soft muted ${user}`,
fetchReply: true,
});
}
public async messageRun(message: Message, args: Args) {
// Get arguments
let user: GuildMember;
try {
user = await args.pick('member');
} catch {
await message.react('❌');
await message.reply('User was not provided!');
return;
}
if (user.roles.cache.has(IDs.roles.restrictions.softMute)) {
await user.roles.remove(IDs.roles.restrictions.softMute);
await message.reply(`Removed soft mute for ${user}`);
} else {
await user.roles.add(IDs.roles.restrictions.softMute);
await message.reply(`Soft muted ${user}`);
}
await message.react('✅');
}
}

163
src/commands/mod/vcMute.ts Normal file
View File

@@ -0,0 +1,163 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Animal Rights Advocates Discord Bot
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { GuildMember, Message } from 'discord.js';
import { addMute, removeMute, checkActive } from '#utils/database/vcMute';
import { addExistingUser, userExists } from '#utils/database/dbExistingUser';
export class VCMuteCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: 'vcmute',
aliases: ['vmute'],
description: 'Persists a server mute if a user is trying to bypass mute',
preconditions: [['CoordinatorOnly', '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 persistently mute')
.setRequired(true))
.addStringOption((option) => option.setName('reason')
.setDescription('Reason for persistently muting the user')),
{
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');
const modUser = interaction.member;
const { guild } = interaction;
// Checks if all the variables are of the right type
if (modUser === null || guild === null) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
}
// Gets guildMember whilst removing the ability of each other variables being null
const member = guild.members.cache.get(user.id);
const mod = guild.members.cache.get(modUser.user.id);
// Checks if guildMember is null
if (member === undefined || mod === undefined) {
await interaction.reply({
content: 'Error fetching user!',
ephemeral: true,
fetchReply: true,
});
return;
}
// Check if removing VC Mute
if (await checkActive(member.id)) {
await removeMute(member.id);
if (member.voice.channel !== null) {
await member.voice.setMute(false, reason === null ? undefined : reason);
}
await interaction.reply({
content: `Removed server mute from ${user}`,
fetchReply: true,
ephemeral: true,
});
return;
}
// Check if mod is in database
if (!await userExists(mod.id)) {
await addExistingUser(mod);
}
// Add VC Mute
if (member.voice.channel !== null) {
await member.voice.setMute(true, reason === null ? undefined : reason);
}
await addMute(member.id, mod.id, reason);
await interaction.reply({
content: `Server muted ${user}`,
fetchReply: true,
ephemeral: true,
});
}
public async messageRun(message: Message, args: Args) {
// Get arguments
let member: GuildMember;
try {
member = await args.pick('member');
} 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 (mod === null) {
await message.react('❌');
await message.reply('Moderator not found! Try again or contact a developer!');
return;
}
// Check if removing VC Mute
if (await checkActive(member.id)) {
await removeMute(member.id);
if (member.voice.channel !== null) {
await member.voice.setMute(false, reason === null ? undefined : reason);
}
await message.reply(`Removed server mute from ${member}`);
await message.react('✅');
return;
}
// Check if mod is in database
if (!await userExists(mod.id)) {
await addExistingUser(mod);
}
// Add VC Mute
if (member.voice.channel !== null) {
await member.voice.setMute(true, reason === null ? undefined : reason);
}
await addMute(member.id, mod.id, reason);
await message.reply(`Server muted ${member}`);
await message.react('✅');
}
}

77
src/listeners/vcMute.ts Normal file
View File

@@ -0,0 +1,77 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Animal Rights Advocates Discord Bot
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Listener } from '@sapphire/framework';
import type { VoiceState } from 'discord.js';
import { checkActive, removeMute } from '#utils/database/vcMute';
export class VCMuteListener extends Listener {
public constructor(context: Listener.Context, options: Listener.Options) {
super(context, {
...options,
event: 'voiceStateUpdate',
});
}
public async run(oldState: VoiceState, newState: VoiceState) {
// Check the user joining the channel if they need to be muted
if (oldState.channel === null && newState.channel !== null) {
const { member } = newState;
if (member === null) {
this.container.logger.error('VCMute Listener - GuildMember not found when joining');
return;
}
// Check if user is already muted
if (member.voice.serverMute) {
return;
}
// Check if user is muted on the database
if (!await checkActive(member.id)) {
return;
}
// Server mute the user
await member.voice.setMute(true);
return;
}
// Check if the user has been unmuted by a mod
if (oldState.channel !== null && newState.channel !== null) {
const { member } = newState;
if (member === null) {
this.container.logger.error('VCMute Listener - GuildMember not found when unmuting');
return;
}
// Check if user is muted on the database
if (!await checkActive(member.id)) {
return;
}
// Checks if the user has been unmuted
if (oldState.serverMute && !newState.serverMute) {
await removeMute(member.id);
}
}
}
}

View File

@@ -0,0 +1,69 @@
import { container } from '@sapphire/framework';
import type { Snowflake } from 'discord.js';
export async function addMute(userId: Snowflake, modId: Snowflake, reason: string | null) {
// Add the user to the database
await container.database.vCMute.create({
data: {
user: {
connect: {
id: userId,
},
},
mod: {
connect: {
id: modId,
},
},
reason,
},
});
}
export async function removeMute(userId: Snowflake) {
const mute = await container.database.vCMute.findFirst({
where: {
userId,
},
select: {
id: true,
},
orderBy: {
id: 'desc',
},
});
if (mute === null) {
return;
}
// Query to deactivate the specific sus note
await container.database.vCMute.update({
where: {
id: mute.id,
},
data: {
endTime: new Date(),
},
});
}
export async function checkActive(userId: Snowflake) {
const mute = await container.database.vCMute.findFirst({
where: {
userId,
},
select: {
endTime: true,
},
orderBy: {
id: 'desc',
},
});
if (mute === null) {
return false;
}
return mute.endTime === null;
}