feat(permissions): Adds server permissions

This commit is contained in:
Michel Fedde 2025-06-24 20:58:46 +02:00
parent d46bbd84c5
commit cf9c88a2d6
24 changed files with 415 additions and 69 deletions

View file

@ -5,9 +5,9 @@ import {
GuildMember,
GuildMemberRoleManager,
InteractionReplyOptions,
MessageFlags,
MessageFlags, PermissionFlagsBits,
roleMention,
SlashCommandBuilder,
SlashCommandBuilder, Snowflake,
time,
userMention
} from "discord.js";
@ -28,6 +28,9 @@ import {MenuTraversal} from "../../Menu/MenuTraversal";
import {ConfigurationHandler} from "../../Configuration/ConfigurationHandler";
import {GroupConfigurationProvider} from "../../Configuration/Groups/GroupConfigurationProvider";
import {MenuHandler} from "../../Configuration/MenuHandler";
import {ServerConfigurationProvider} from "../../Configuration/Server/ServerConfigurationProvider";
import {ServerConfigurationRepository} from "../../Database/Repositories/ServerConfigurationRepository";
import {PermissionError} from "../PermissionError";
export class GroupCommand implements Command, ChatInteractionCommand, AutocompleteCommand {
private static GOODBYE_MESSAGES: string[] = [
@ -114,6 +117,10 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
}
private create(interaction: ChatInputCommandInteraction): void {
if (!this.allowedCreate(interaction)) {
throw new PermissionError("You don't have the permissions for it!")
}
const name = interaction.options.getString("name") ?? '';
const role = interaction.options.getRole("role", true);
@ -151,6 +158,22 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
interaction.reply({content: `:white_check_mark: Created group \`${name}\``, flags: MessageFlags.Ephemeral})
}
private allowedCreate(interaction: ChatInputCommandInteraction): boolean {
if ((<GuildMember>interaction.member)?.permissions.has(PermissionFlagsBits.Administrator)) {
return true;
}
const config = new ConfigurationHandler(
new ServerConfigurationProvider(
Container.get<ServerConfigurationRepository>(ServerConfigurationRepository.name),
<Snowflake>interaction.guildId
)
);
const configValue = config.getConfigurationByPath("permissions.groupCreation.allowEveryone").value;
return configValue === true;
}
private validateGroupName(name: string): string | null {
const lowercaseName = name.toLowerCase();
for (const invalidcharactersequence of GroupCommand.INVALID_CHARACTER_SEQUENCES) {
@ -209,7 +232,7 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
const repo = Container.get<GroupRepository>(GroupRepository.name);
if (group.leader.memberid != interaction.member?.user.id) {
throw new UserError("Can't remove group. You are not the leader.");
throw new PermissionError("You are not the leader.");
}
repo.deleteGroup(group);
@ -320,8 +343,8 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
const repo = Container.get<GroupRepository>(GroupRepository.name);
if (group.leader.memberid != interaction.member?.user.id) {
throw new UserError(
"Can't transfer leadership. You are not the leader."
throw new PermissionError(
"You are not the leader."
);
}