Skip to content

Commit 9c7bb86

Browse files
authored
Merge pull request #22 from CobaltScripts/refactor/checks-bitflags
refactor: handle checks in Command structure with bitflags
2 parents a02f70a + 1ff6324 commit 9c7bb86

14 files changed

Lines changed: 83 additions & 114 deletions

src/commands/admin/CrashCommand.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PermissionsBitField } from 'discord.js';
2-
import { Command, CommandContext } from '@structures/Command.js';
2+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
33
import { ExtendedClient } from '@structures/Client.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Constants } from '@utils/Constants.js';
@@ -10,19 +10,12 @@ export default class CrashCommand extends Command {
1010
name: 'crash',
1111
description: 'Crash the bot',
1212
requiredPermissions: [PermissionsBitField.Flags.Administrator],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
});
1415
}
1516

1617
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
17-
const author = context.interaction?.user || context.message?.author;
18-
19-
if (!author) {
20-
await context.reply({
21-
embeds: [Embeds.error('Unable to identify the command author.')],
22-
});
23-
24-
return;
25-
}
18+
const author = context.author!;
2619

2720
if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
2821
await context.reply({

src/commands/admin/DevResetCommand.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PermissionsBitField } from 'discord.js';
2-
import { Command, CommandContext } from '@structures/Command.js';
2+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
33
import { ExtendedClient } from '@structures/Client.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Constants } from '@utils/Constants.js';
@@ -10,19 +10,12 @@ export default class DevResetCommand extends Command {
1010
name: 'devreset',
1111
description: 'Reset the chat bot',
1212
requiredPermissions: [PermissionsBitField.Flags.Administrator],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
});
1415
}
1516

1617
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
17-
const author = context.interaction?.user || context.message?.author;
18-
19-
if (!author) {
20-
await context.reply({
21-
embeds: [Embeds.error('Unable to identify the command author.')],
22-
});
23-
24-
return;
25-
}
18+
const author = context.author!;
2619

2720
if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
2821
await context.reply({

src/commands/admin/SyncCommand.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import { PermissionFlagsBits } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
4-
import { Constants } from '@utils/Constants.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
54
import { Embeds } from '@utils/Embeds.js';
5+
import { Constants } from '@utils/Constants.js';
66

77
export default class SyncCommand extends Command {
88
constructor() {
99
super({
1010
name: 'sync',
1111
description: "Cleanup each member's roles",
1212
requiredPermissions: [PermissionFlagsBits.Administrator],
13+
checkFlags: CommandCheckFlags.Guild,
1314
});
1415
}
1516

1617
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
17-
const guild = context.interaction?.guild ?? context.message?.guild;
18-
19-
if (!guild) {
20-
return await context.reply({
21-
embeds: [Embeds.error('This command can only be used in a server.')],
22-
});
23-
}
18+
const guild = context.guild!;
2419

2520
const members = await guild.members.fetch();
2621
const communityRole = guild.roles.cache.get(Constants.ROLES.COMMUNITY);

src/commands/admin/UpdateGeminiKeyCommand.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MessageFlags, PermissionFlagsBits } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Argument } from '@structures/Argument.js';
55
import { Embeds } from '@utils/Embeds.js';
66
import { Constants } from '@utils/Constants.js';
@@ -11,6 +11,7 @@ export default class UpdateGeminiKeyCommand extends Command {
1111
name: 'updategeminikey',
1212
description: 'Update the Gemini API key',
1313
requiredPermissions: [PermissionFlagsBits.Administrator],
14+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1415
args: [
1516
new Argument({
1617
name: 'key',
@@ -23,15 +24,7 @@ export default class UpdateGeminiKeyCommand extends Command {
2324
}
2425

2526
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
26-
const author = context.interaction?.user || context.message?.author;
27-
28-
if (!author) {
29-
await context.reply({
30-
embeds: [Embeds.error('Unable to identify the command author.')],
31-
});
32-
33-
return;
34-
}
27+
const author = context.author!;
3528

3629
if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
3730
await context.reply({

src/commands/moderation/BanCommand.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PermissionFlagsBits } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Argument } from '@structures/Argument.js';
66

@@ -10,6 +10,7 @@ export default class BanCommand extends Command {
1010
name: 'ban',
1111
description: 'Ban a user from the server',
1212
requiredPermissions: [PermissionFlagsBits.BanMembers],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
args: [
1415
new Argument({
1516
name: 'user',
@@ -28,14 +29,8 @@ export default class BanCommand extends Command {
2829
}
2930

3031
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
31-
const guild = context.interaction?.guild ?? context.message?.guild;
32-
const author = context.interaction?.user ?? context.message?.author;
33-
34-
if (!guild) {
35-
return await context.reply({
36-
embeds: [Embeds.error('This command can only be used in a server.')],
37-
});
38-
}
32+
const guild = context.guild!;
33+
const author = context.author!;
3934

4035
const user = guild.members.cache.get(context.args.user as string);
4136

src/commands/moderation/ClearCommand.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PermissionFlagsBits, TextChannel } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Argument } from '@structures/Argument.js';
66

@@ -10,6 +10,7 @@ export default class ClearCommand extends Command {
1010
name: 'clear',
1111
description: 'Clear messages from a channel',
1212
requiredPermissions: [PermissionFlagsBits.ManageMessages],
13+
checkFlags: CommandCheckFlags.None,
1314
args: [
1415
new Argument({
1516
name: 'amount',
@@ -22,14 +23,6 @@ export default class ClearCommand extends Command {
2223
}
2324

2425
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
25-
const guild = context.interaction?.guild ?? context.message?.guild;
26-
27-
if (!guild) {
28-
return await context.reply({
29-
embeds: [Embeds.error('This command can only be used in a server.')],
30-
});
31-
}
32-
3326
const amount = context.args.amount as number;
3427

3528
if (!amount || isNaN(amount) || amount < 1 || amount > 100) {

src/commands/moderation/KickCommand.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PermissionFlagsBits } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Argument } from '@structures/Argument.js';
66

@@ -10,6 +10,7 @@ export default class KickCommand extends Command {
1010
name: 'kick',
1111
description: 'Kick a user from the server',
1212
requiredPermissions: [PermissionFlagsBits.KickMembers],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
args: [
1415
new Argument({
1516
name: 'user',
@@ -28,14 +29,8 @@ export default class KickCommand extends Command {
2829
}
2930

3031
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
31-
const guild = context.interaction?.guild ?? context.message?.guild;
32-
const author = context.interaction?.user ?? context.message?.author;
33-
34-
if (!guild) {
35-
return await context.reply({
36-
embeds: [Embeds.error('This command can only be used in a server.')],
37-
});
38-
}
32+
const guild = context.guild!;
33+
const author = context.author!;
3934

4035
const user = guild.members.cache.get(context.args.user as string);
4136

src/commands/moderation/LockCommand.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PermissionFlagsBits, TextChannel } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Argument } from '@structures/Argument.js';
66

@@ -10,6 +10,7 @@ export default class LockCommand extends Command {
1010
name: 'lock',
1111
description: 'Lock a channel',
1212
requiredPermissions: [PermissionFlagsBits.ManageChannels],
13+
checkFlags: CommandCheckFlags.Guild,
1314
args: [
1415
new Argument({
1516
name: 'channel',
@@ -22,16 +23,10 @@ export default class LockCommand extends Command {
2223
}
2324

2425
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
25-
const guild = context.interaction?.guild ?? context.message?.guild;
26-
27-
if (!guild) {
28-
return await context.reply({
29-
embeds: [Embeds.error('This command can only be used in a server.')],
30-
});
31-
}
26+
const guild = context.guild!;
3227

3328
const channel = context.args.channel
34-
? guild?.channels.cache.get(context.args.channel as string)
29+
? guild.channels.cache.get(context.args.channel as string)
3530
: (context.interaction?.channel ?? context.message?.channel);
3631

3732
if (!channel || !channel.isTextBased()) {

src/commands/moderation/MuteCommand.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Argument } from '@structures/Argument.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { PermissionsBitField } from 'discord.js';
55
import { Embeds } from '@utils/Embeds.js';
66

@@ -10,6 +10,7 @@ export default class MuteCommand extends Command {
1010
name: 'mute',
1111
description: 'Mute a user',
1212
requiredPermissions: [PermissionsBitField.Flags.ModerateMembers],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
args: [
1415
new Argument({
1516
name: 'user',
@@ -34,14 +35,8 @@ export default class MuteCommand extends Command {
3435
}
3536

3637
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
37-
const guild = context.interaction?.guild ?? context.message?.guild;
38-
const author = context.interaction?.user ?? context.message?.author;
39-
40-
if (!guild) {
41-
return await context.reply({
42-
embeds: [Embeds.error('This command can only be used in a server.')],
43-
});
44-
}
38+
const guild = context.guild!;
39+
const author = context.author!;
4540

4641
const user = guild.members.cache.get(context.args.user as string);
4742

src/commands/moderation/PurgeCommand.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PermissionFlagsBits, TextChannel } from 'discord.js';
22
import { ExtendedClient } from '@structures/Client.js';
3-
import { Command, CommandContext } from '@structures/Command.js';
3+
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
44
import { Embeds } from '@utils/Embeds.js';
55
import { Argument } from '@structures/Argument.js';
66

@@ -10,6 +10,7 @@ export default class PurgeCommand extends Command {
1010
name: 'purge',
1111
description: 'Purge and recreate a channel',
1212
requiredPermissions: [PermissionFlagsBits.ManageChannels],
13+
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
1314
args: [
1415
new Argument({
1516
name: 'channel',
@@ -22,17 +23,11 @@ export default class PurgeCommand extends Command {
2223
}
2324

2425
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
25-
const guild = context.interaction?.guild ?? context.message?.guild;
26-
const author = context.interaction?.user ?? context.message?.author;
27-
28-
if (!guild) {
29-
return await context.reply({
30-
embeds: [Embeds.error('This command can only be used in a server.')],
31-
});
32-
}
26+
const guild = context.guild!;
27+
const author = context.author!;
3328

3429
const originalChannel = context.args.channel
35-
? guild?.channels.cache.get(context.args.channel as string)
30+
? guild.channels.cache.get(context.args.channel as string)
3631
: (context.interaction?.channel ?? context.message?.channel);
3732

3833
if (!originalChannel || !originalChannel.isTextBased()) {
@@ -56,10 +51,10 @@ export default class PurgeCommand extends Command {
5651
}
5752

5853
await newChannel.setPosition(textChannel.position);
59-
await textChannel.delete(`Purged by ${author?.tag}`);
54+
await textChannel.delete(`Purged by ${author.tag}`);
6055

6156
await newChannel.send({
62-
embeds: [Embeds.success(`Channel purged by ${author?.tag}`)],
57+
embeds: [Embeds.success(`Channel purged by ${author.tag}`)],
6358
});
6459
}
6560
}

0 commit comments

Comments
 (0)