-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add "Manage Server Guard" that checks for "Manage Server" permi…
…ssion in favor of administrator guard for all commands
- Loading branch information
1 parent
4c0d0c2
commit b1baa29
Showing
9 changed files
with
74 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './admin.guard'; | ||
export * from './guild-admin.guard'; | ||
export * from './guild-moderator.guard'; | ||
export * from './manage-server.guard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { Client, PermissionsBitField } from 'discord.js'; | ||
import { NecordExecutionContext } from 'necord'; | ||
|
||
@Injectable() | ||
export class ManageServerGuard implements CanActivate { | ||
private readonly _logger = new Logger(ManageServerGuard.name); | ||
|
||
constructor(private _client: Client) {} | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const ctx = NecordExecutionContext.create(context); | ||
const [interaction] = ctx.getContext<'interactionCreate'>(); | ||
|
||
if (!interaction) { | ||
return true; | ||
} | ||
|
||
if ( | ||
interaction.isUserContextMenuCommand() || | ||
interaction.isContextMenuCommand() | ||
) { | ||
return false; | ||
} | ||
|
||
const admins = process.env['OWNER_IDS']!.split(','); | ||
if (admins.includes(interaction?.user?.id)) { | ||
return true; | ||
} | ||
|
||
const guild = await this._client.guilds.fetch(interaction.guildId!); | ||
const member = await guild.members.fetch(interaction.user.id); | ||
|
||
const hasAdminPermissions = member.permissions.has( | ||
PermissionsBitField.Flags.Administrator, | ||
); | ||
if (hasAdminPermissions) { | ||
return true; | ||
} | ||
|
||
const hasPermission = member.permissions.has( | ||
PermissionsBitField.Flags.ManageGuild, | ||
); | ||
if (hasPermission) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |