Skip to content

Commit

Permalink
refactor: Add check function to Tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayfri committed Sep 5, 2021
1 parent 9b1c4e5 commit ff3f837
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/classes/commands/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ export enum Tag {
threadOnly = 'threadOnly',
}

export namespace Tag {
export function check(ctx: CommandContext, tags: Array<Tag | keyof typeof Tag | string>) {
const missingTags: Tag[] = [];
for (const tag of tags ?? []) {
if (tag === Tag.channelOnly && ctx.channel.isThread()) missingTags.push(Tag.threadOnly);
if (tag === Tag.dmOnly && ctx.channel.type !== 'DM') missingTags.push(Tag.dmOnly);
if (tag === Tag.guildOnly && !ctx.guild) missingTags.push(Tag.guildOnly);
if (tag === Tag.guildOwnerOnly && ctx.guild?.ownerId !== ctx.user.id) missingTags.push(Tag.guildOwnerOnly);
if (tag === Tag.nsfw && ctx.channel instanceof GuildChannel && !ctx.channel.nsfw) missingTags.push(Tag.nsfw);
if (tag === Tag.ownerOnly && !isOwner(ctx.user.id)) missingTags.push(Tag.ownerOnly);
if (tag === Tag.threadOnly && !ctx.channel.isThread()) missingTags.push(Tag.threadOnly);
}

return missingTags;
}
}

/**
* The cooldown object.
*/
Expand Down Expand Up @@ -326,18 +343,7 @@ export abstract class Command {
* @returns - Tags that are not validated by the message.
*/
public getMissingTags(ctx: CommandContext) {
const missingTags: Tag[] = [];
for (const tag of this.tags ?? []) {
if (tag === Tag.channelOnly && ctx.channel.isThread()) missingTags.push(Tag.threadOnly);
if (tag === Tag.dmOnly && ctx.channel.type !== 'DM') missingTags.push(Tag.dmOnly);
if (tag === Tag.guildOnly && !ctx.guild) missingTags.push(Tag.guildOnly);
if (tag === Tag.guildOwnerOnly && ctx.guild?.ownerId !== ctx.user.id) missingTags.push(Tag.guildOwnerOnly);
if (tag === Tag.nsfw && ctx.channel instanceof GuildChannel && !ctx.channel.nsfw) missingTags.push(Tag.nsfw);
if (tag === Tag.ownerOnly && !isOwner(ctx.user.id)) missingTags.push(Tag.ownerOnly);
if (tag === Tag.threadOnly && !ctx.channel.isThread()) missingTags.push(Tag.threadOnly);
}

return missingTags;
return Tag.check(ctx, this.tags ?? []);
}

/**
Expand Down

0 comments on commit ff3f837

Please sign in to comment.