fix(arabot): change precondition to look for roles and change precondition in sus from and to or

This commit is contained in:
Anthony 2022-07-24 05:02:34 +01:00
parent 2259533bf8
commit f369c2f108
5 changed files with 69 additions and 12 deletions

View File

@ -48,6 +48,7 @@ export class HugCommand extends Command {
// Command run // Command run
public async chatInputRun(interaction: Command.ChatInputInteraction) { public async chatInputRun(interaction: Command.ChatInputInteraction) {
// Get the users // Get the users
// TODO exception handling
const user = interaction.options.getUser('user')!; const user = interaction.options.getUser('user')!;
const hugger = interaction.member!.user; const hugger = interaction.member!.user;
const huggerGuildMember = interaction.guild!.members.cache.get(hugger.id)!; const huggerGuildMember = interaction.guild!.members.cache.get(hugger.id)!;

View File

@ -27,7 +27,7 @@ export class SusCommand extends Command {
super(context, { super(context, {
name: 'sus', name: 'sus',
description: 'Notes about users that are sus', description: 'Notes about users that are sus',
preconditions: ['ModOnly', 'VerifierOnly'], preconditions: [['ModOnly', 'VerifierOnly']],
}); });
} }
@ -83,6 +83,7 @@ export class SusCommand extends Command {
// Subcommand to add sus note // Subcommand to add sus note
public async addNote(interaction: Command.ChatInputInteraction) { public async addNote(interaction: Command.ChatInputInteraction) {
// Get the arguments // Get the arguments
// TODO exception handling
const user = interaction.options.getUser('user')!; const user = interaction.options.getUser('user')!;
const mod = interaction.member!.user; const mod = interaction.member!.user;
const note = interaction.options.getString('note')!; const note = interaction.options.getString('note')!;
@ -90,6 +91,7 @@ export class SusCommand extends Command {
// Add the data to the database // Add the data to the database
// Check if the user exists on the database // Check if the user exists on the database
// TODO exception handling
const userGuildMember = interaction.guild!.members.cache.get(user.id)!; const userGuildMember = interaction.guild!.members.cache.get(user.id)!;
if (!await userExists(userGuildMember)) { if (!await userExists(userGuildMember)) {
await addExistingUser(userGuildMember); await addExistingUser(userGuildMember);
@ -99,7 +101,6 @@ export class SusCommand extends Command {
if (!await userExists(modGuildMember)) { if (!await userExists(modGuildMember)) {
await addExistingUser(modGuildMember); await addExistingUser(modGuildMember);
} }
// TODO check if user is on the database and if not, add them to the database
await addToDatabase(user.id, mod.id, note); await addToDatabase(user.id, mod.id, note);
await interaction.reply({ await interaction.reply({
@ -116,6 +117,7 @@ export class SusCommand extends Command {
const notes = await findNote(user.id, true); const notes = await findNote(user.id, true);
// Gets the username of the mod // Gets the username of the mod
const modId = notes[notes.length - 1].modId; const modId = notes[notes.length - 1].modId;
// TODO exception handling
const mod = interaction.guild!.members.cache.get(modId)!.user.username; const mod = interaction.guild!.members.cache.get(modId)!.user.username;
// Creates the embed to display the sus note // Creates the embed to display the sus note

View File

@ -20,25 +20,26 @@
import { AllFlowsPrecondition } from '@sapphire/framework'; import { AllFlowsPrecondition } from '@sapphire/framework';
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js'; import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';
import { IDs } from '../utils/ids'; import { IDs } from '../utils/ids';
import type { GuildMember } from 'discord.js';
export class ModOnlyPrecondition extends AllFlowsPrecondition { export class ModOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) { public override async messageRun(message: Message) {
// for message command // for message command
return this.checkMod(message.author.id); return this.checkMod(message.member!);
} }
public override async chatInputRun(interaction: CommandInteraction) { public override async chatInputRun(interaction: CommandInteraction) {
// for slash command // for slash command
return this.checkMod(interaction.user.id); return this.checkMod(interaction.member! as GuildMember);
} }
public override async contextMenuRun(interaction: ContextMenuInteraction) { public override async contextMenuRun(interaction: ContextMenuInteraction) {
// for context menu command // for context menu command
return this.checkMod(interaction.user.id); return this.checkMod(interaction.member! as GuildMember);
} }
private async checkMod(userId: string) { private async checkMod(user: GuildMember) {
return userId === IDs.roles.staff.moderator return user.roles.cache.has(IDs.roles.staff.moderator)
? this.ok() ? this.ok()
: this.error({ message: 'Only moderators can run this command!' }); : this.error({ message: 'Only moderators can run this command!' });
} }

View File

@ -0,0 +1,52 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Animal Rights Advocates Discord Bot
Copyright (C) 2022 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 { AllFlowsPrecondition } from '@sapphire/framework';
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';
import { IDs } from '../utils/ids';
import type { GuildMember } from 'discord.js';
export class PatreonOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) {
// for message command
return this.checkPatreon(message.member!);
}
public override async chatInputRun(interaction: CommandInteraction) {
// for slash command
return this.checkPatreon(interaction.member! as GuildMember);
}
public override async contextMenuRun(interaction: ContextMenuInteraction) {
// for context menu command
return this.checkPatreon(interaction.member! as GuildMember);
}
private async checkPatreon(user: GuildMember) {
return user.roles.cache.has(IDs.roles.patron)
? this.ok()
: this.error({ message: 'Only Patreon members can run this command!' });
}
}
declare module '@sapphire/framework' {
interface Preconditions {
PatreonOnly: never;
}
}

View File

@ -20,25 +20,26 @@
import { AllFlowsPrecondition } from '@sapphire/framework'; import { AllFlowsPrecondition } from '@sapphire/framework';
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js'; import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';
import { IDs } from '../utils/ids'; import { IDs } from '../utils/ids';
import type { GuildMember } from 'discord.js';
export class VerifierOnlyPrecondition extends AllFlowsPrecondition { export class VerifierOnlyPrecondition extends AllFlowsPrecondition {
public override async messageRun(message: Message) { public override async messageRun(message: Message) {
// for message command // for message command
return this.checkVerifier(message.author.id); return this.checkVerifier(message.member!);
} }
public override async chatInputRun(interaction: CommandInteraction) { public override async chatInputRun(interaction: CommandInteraction) {
// for slash command // for slash command
return this.checkVerifier(interaction.user.id); return this.checkVerifier(interaction.member! as GuildMember);
} }
public override async contextMenuRun(interaction: ContextMenuInteraction) { public override async contextMenuRun(interaction: ContextMenuInteraction) {
// for context menu command // for context menu command
return this.checkVerifier(interaction.user.id); return this.checkVerifier(interaction.member! as GuildMember);
} }
private async checkVerifier(userId: string) { private async checkVerifier(user: GuildMember) {
return userId === IDs.roles.staff.verifier return user.roles.cache.has(IDs.roles.staff.verifier)
? this.ok() ? this.ok()
: this.error({ message: 'Only verifiers can run this command!' }); : this.error({ message: 'Only verifiers can run this command!' });
} }