Adds transfer of leadership of a group

This commit is contained in:
Michel Fedde 2025-05-27 20:00:10 +02:00
parent fccc30626a
commit 13f2bf30fa
4 changed files with 67 additions and 4 deletions

View file

@ -9,7 +9,7 @@ import {
GuildMember,
EmbedBuilder,
AutocompleteInteraction,
formatEmoji, roleMention, time
formatEmoji, roleMention, time, userMention
} from "discord.js";
import {AutocompleteCommand, ChatInteractionCommand, Command} from "./Command";
import {GroupModel} from "../../Models/GroupModel";
@ -26,6 +26,7 @@ import {GroupConfigurationRepository} from "../../Repositories/GroupConfiguratio
import {IconCache} from "../../Icons/IconCache";
import {PlaydateRepository} from "../../Repositories/PlaydateRepository";
import playdate from "../../Database/tables/Playdate";
import Commands from "./Commands";
export class GroupCommand implements Command, ChatInteractionCommand, AutocompleteCommand {
private static GOODBYE_MESSAGES: string[] = [
@ -68,6 +69,16 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
.setName("remove")
.setDescription("Deletes a group you are the leader for.")
.addIntegerOption(GroupSelection.createOptionSetup())
)
.addSubcommand(command => command
.setName("transfer")
.setDescription("Transfers leadership of a group to a different person")
.addIntegerOption(GroupSelection.createOptionSetup)
.addUserOption(option => option
.setName("target")
.setDescription("The member, that is the new leader")
.setRequired(true)
)
);
}
@ -85,6 +96,9 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
case "config":
await this.runConfigurator(interaction);
break;
case "transfer":
await this.transferLeadership(interaction);
break;
default:
throw new Error("Unsupported command");
}
@ -201,4 +215,40 @@ export class GroupCommand implements Command, ChatInteractionCommand, Autocomple
await configurationRenderer.setup(interaction);
}
private async transferLeadership(interaction: ChatInputCommandInteraction) {
const group = GroupSelection.getGroup(interaction);
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."
);
}
const newLeader = <GuildMember>interaction.options.getMember("target", true);
if (!newLeader.roles.cache.has(group.role.roleid)) {
throw new UserError(
"Can't transfer leadership: The target member is not part of your group.",
"Add the user to the role this group is part in or ask your server admin to do it."
);
}
group.leader.memberid = newLeader.id
repo.update(group);
const embed = new EmbedBuilder()
.setTitle("Leadership transfered")
.setDescription(
`Leadership was successfully transfered to ${userMention(newLeader.user.id)}`
)
await interaction.reply({
embeds: [
embed
],
flags: MessageFlags.Ephemeral,
})
}
}