All files / src/common/guards CommandPermissions.guard.ts

0% Statements 0/25
0% Branches 0/18
0% Functions 0/4
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                                                                                           
import { CommandPermissions } from "@/common/decorators";
import { Config } from "@/modules/shared/config/types";
import { formatArray } from "@/utils/Tools";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { CanActivate, ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Reflector } from "@nestjs/core";
import { ChatInputCommandInteraction } from "discord.js";
import { NecordExecutionContext } from "necord";
 
@Injectable()
export class CommandPermissionsGuard implements CanActivate {
	public constructor(
		@Inject(LOCALIZATION_ADAPTER) private readonly translate: NestedLocalizationAdapter,
		private readonly reflector: Reflector,
		private readonly config: ConfigService,
	) {}
 
	public async canActivate(executionContext: ExecutionContext): Promise<boolean> {
		const permissions = this.reflector.get(CommandPermissions.KEY, executionContext.getHandler());
		const context = NecordExecutionContext.create(executionContext);
		const args = context.getArgByIndex(0);
		const interaction = args[0] as ChatInputCommandInteraction;
 
		if (permissions.bot) {
			if (!interaction.guild.members.me.permissions.has(permissions.bot)) {
				interaction.reply(
					this.translate.getTranslation("Tools.Commands.Permission.Bot", interaction.guildLocale, {
						PERMS: formatArray(permissions.bot as string[]),
					}),
				);
				return false;
			}
		}
 
		if (permissions.user) {
			if (!(await interaction.guild.members.fetch(interaction.user.id)).permissions.has(permissions.user)) {
				interaction.reply(
					this.translate.getTranslation("Tools.Commands.Permission.User", interaction.guildLocale, {
						PERMS: formatArray(permissions.user as string[]),
					}),
				);
			}
		}
 
		if (permissions.guildOnly && !this.checkGuild(interaction.guildId)) {
			interaction.reply(this.translate.getTranslation("Tools.Command.Checker.GuildOnly", interaction.guildLocale));
			return false;
		}
 
		if (permissions.ownerOnly && !this.checkOwner(interaction.user.id)) {
			interaction.reply(this.translate.getTranslation("Tools.Command.Checker.OwnerOnly", interaction.guildLocale));
			return false;
		}
 
		return true;
	}
 
	private checkGuild(target: string): boolean {
		return (
			this.config.getOrThrow<Config["Discord"]>("Discord").Servers.NDCommunity === target ||
			this.config.getOrThrow<Config["Discord"]>("Discord").Servers.TestGuild === target
		);
	}
 
	private checkOwner(target: string) {
		return this.config.getOrThrow<Config["Discord"]>("Discord").Client.Owners.includes(target);
	}
}