-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
guildElder command #2715
Draft
Ntalcme
wants to merge
7
commits into
draftbot-v5
Choose a base branch
from
guildElderCommand
base: draftbot-v5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
guildElder command #2715
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a2c10e
WIP on guildElder command
Ntalcme d2a6170
small fix
Ntalcme 5bd36fd
front guildElder command
Ntalcme d65f1be
Split du packet en 4 packets différent
Ntalcme ed82e5b
End of command guildelder
Ntalcme 2e8f6f7
small fix
Ntalcme 8c3d6f7
Another small fix
Ntalcme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import Player, {Players} from "../../core/database/game/models/Player"; | ||
import {DraftBotPacket, makePacket, PacketContext} from "../../../../Lib/src/packets/DraftBotPacket"; | ||
import {Guilds} from "../../core/database/game/models/Guild"; | ||
import { | ||
CommandGuildElderAcceptPacketRes, | ||
CommandGuildElderAlreadyElderPacketRes, | ||
CommandGuildElderFoundPlayerPacketRes, | ||
CommandGuildElderHimselfPacketRes, | ||
CommandGuildElderPacketReq, | ||
CommandGuildElderRefusePacketRes, | ||
CommandGuildElderSameGuildPacketRes | ||
} from "../../../../Lib/src/packets/commands/CommandGuildElderPacket"; | ||
import {draftBotInstance} from "../../index"; | ||
import {commandRequires, CommandUtils} from "../../core/utils/CommandUtils"; | ||
import {GuildConstants} from "../../../../Lib/src/constants/GuildConstants"; | ||
import {GuildRole} from "../../../../Lib/src/enums/GuildRole"; | ||
import {EndCallback, ReactionCollectorInstance} from "../../core/utils/ReactionsCollector"; | ||
import {ReactionCollectorAcceptReaction} from "../../../../Lib/src/packets/interaction/ReactionCollectorPacket"; | ||
import {BlockingUtils} from "../../core/utils/BlockingUtils"; | ||
import {BlockingConstants} from "../../../../Lib/src/constants/BlockingConstants"; | ||
import {ReactionCollectorGuildElder} from "../../../../Lib/src/packets/interaction/ReactionCollectorGuildElder"; | ||
|
||
/** | ||
* Return true if promotedPlayer can be promoted | ||
* @param player | ||
* @param promotedPlayer | ||
* @param response | ||
*/ | ||
async function isEligible(player: Player, promotedPlayer: Player, response: DraftBotPacket[]): Promise<boolean> { | ||
if (promotedPlayer === null) { | ||
response.push(makePacket(CommandGuildElderFoundPlayerPacketRes, {foundPlayer: false})); | ||
return false; | ||
} | ||
let promotedGuild; | ||
try { | ||
promotedGuild = await Guilds.getById(promotedPlayer.guildId); | ||
} | ||
catch (error) { | ||
promotedGuild = null; | ||
} | ||
|
||
const guild = await Guilds.getById(player.guildId); | ||
if (promotedGuild === null || promotedGuild.id !== player.guildId) { | ||
response.push(makePacket(CommandGuildElderSameGuildPacketRes, {sameGuild: false})); | ||
return false; | ||
} | ||
|
||
if (promotedPlayer.id === player.id) { | ||
response.push(makePacket(CommandGuildElderHimselfPacketRes, {himself: true})); | ||
return false; | ||
} | ||
|
||
if (promotedPlayer.id === guild.elderId) { | ||
response.push(makePacket(CommandGuildElderAlreadyElderPacketRes, {alreadyElder: true})); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Promote promotedPlayer as elder of the guild | ||
* @param player | ||
* @param promotedPlayer | ||
* @param response | ||
*/ | ||
async function acceptGuildElder(player: Player, promotedPlayer: Player, response: DraftBotPacket[]): Promise<void> { | ||
await player.reload(); | ||
await promotedPlayer.reload(); | ||
// Do all necessary checks again just in case something changed during the menu | ||
if (!await isEligible(player, promotedPlayer, response)) { | ||
return; | ||
} | ||
const guild = await Guilds.getById(player.guildId); | ||
guild.elderId = promotedPlayer.id; | ||
|
||
await Promise.all([ | ||
promotedPlayer.save(), | ||
guild.save() | ||
]); | ||
draftBotInstance.logsDatabase.logGuildElderAdd(guild, promotedPlayer.keycloakId).then(); | ||
|
||
response.push(makePacket(CommandGuildElderAcceptPacketRes, { | ||
promotedKeycloakId: promotedPlayer.keycloakId, | ||
guildName: guild.name | ||
})); | ||
} | ||
|
||
export default class GuildElderCommand { | ||
@commandRequires(CommandGuildElderPacketReq, { | ||
notBlocked: true, | ||
disallowedEffects: CommandUtils.DISALLOWED_EFFECTS.NOT_STARTED_OR_DEAD, | ||
level: GuildConstants.REQUIRED_LEVEL, | ||
guildNeeded: true, | ||
guildRoleNeeded: GuildRole.CHIEF | ||
}) | ||
async execute(response: DraftBotPacket[], player: Player, packet: CommandGuildElderPacketReq, context: PacketContext): Promise<void> { | ||
const promotedPlayer = await Players.getAskedPlayer({keycloakId: packet.askedPlayerKeycloakId}, player); | ||
|
||
if (!await isEligible(player, promotedPlayer, response)) { | ||
return; | ||
} | ||
const guildName = (await Guilds.getById(player.guildId)).name; | ||
|
||
const collector = new ReactionCollectorGuildElder( | ||
guildName, | ||
promotedPlayer.keycloakId | ||
); | ||
|
||
const endCallback: EndCallback = async (collector: ReactionCollectorInstance, response: DraftBotPacket[]): Promise<void> => { | ||
const reaction = collector.getFirstReaction(); | ||
if (reaction && reaction.reaction.type === ReactionCollectorAcceptReaction.name) { | ||
await acceptGuildElder(player, promotedPlayer, response); | ||
} | ||
else { | ||
response.push(makePacket(CommandGuildElderRefusePacketRes, { | ||
promotedKeycloakId: promotedPlayer.keycloakId | ||
})); | ||
} | ||
BlockingUtils.unblockPlayer(player.id, BlockingConstants.REASONS.GUILD_ELDER); | ||
}; | ||
|
||
const collectorPacket = new ReactionCollectorInstance( | ||
collector, | ||
context, | ||
{ | ||
allowedPlayerKeycloakIds: [player.keycloakId], | ||
reactionLimit: 1 | ||
}, | ||
endCallback | ||
) | ||
.block(player.id, BlockingConstants.REASONS.GUILD_ELDER) | ||
.build(); | ||
|
||
response.push(collectorPacket); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import {ReactionCollectorCreationPacket} from "../../../../Lib/src/packets/interaction/ReactionCollectorPacket"; | ||
import {makePacket, PacketContext} from "../../../../Lib/src/packets/DraftBotPacket"; | ||
import {DiscordCache} from "../../bot/DiscordCache"; | ||
import {KeycloakUtils} from "../../../../Lib/src/keycloak/KeycloakUtils"; | ||
import {keycloakConfig} from "../../bot/DraftBotShard"; | ||
import {DraftBotEmbed} from "../../messages/DraftBotEmbed"; | ||
import i18n from "../../translations/i18n"; | ||
import {DiscordCollectorUtils} from "../../utils/DiscordCollectorUtils"; | ||
import {ReactionCollectorGuildElderData} from "../../../../Lib/src/packets/interaction/ReactionCollectorGuildElder"; | ||
import { | ||
CommandGuildElderAcceptPacketRes, | ||
CommandGuildElderAlreadyElderPacketRes, | ||
CommandGuildElderFoundPlayerPacketRes, | ||
CommandGuildElderHimselfPacketRes, | ||
CommandGuildElderPacketReq, | ||
CommandGuildElderRefusePacketRes, | ||
CommandGuildElderSameGuildPacketRes | ||
} from "../../../../Lib/src/packets/commands/CommandGuildElderPacket"; | ||
import {sendErrorMessage, SendManner} from "../../utils/ErrorUtils"; | ||
import {ICommand} from "../ICommand"; | ||
import {SlashCommandBuilderGenerator} from "../SlashCommandBuilderGenerator"; | ||
import {DraftbotInteraction} from "../../messages/DraftbotInteraction"; | ||
import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser"; | ||
import {PacketUtils} from "../../utils/PacketUtils"; | ||
import {SlashCommandBuilder} from "@discordjs/builders"; | ||
|
||
/** | ||
* Create a collector to confirm the promotion | ||
* @param packet | ||
* @param context | ||
*/ | ||
export async function createGuildElderCollector(packet: ReactionCollectorCreationPacket, context: PacketContext): Promise<void> { | ||
const interaction = DiscordCache.getInteraction(context.discord!.interaction)!; | ||
await interaction.deferReply(); | ||
const data = packet.data.data as ReactionCollectorGuildElderData; | ||
const elderPlayer = (await KeycloakUtils.getUserByKeycloakId(keycloakConfig, data.promotedKeycloakId))!; | ||
const embed = new DraftBotEmbed().formatAuthor(i18n.t("commands:guildElder.title", { | ||
lng: interaction.userLanguage, | ||
pseudo: interaction.user.displayName | ||
}), interaction.user) | ||
.setDescription( | ||
i18n.t("commands:guildElder.confirmDesc", { | ||
lng: interaction.userLanguage, | ||
elderPseudo: elderPlayer.attributes.gameUsername, | ||
guildName: data.guildName | ||
}) | ||
); | ||
|
||
await DiscordCollectorUtils.createAcceptRefuseCollector(interaction, embed, packet, context); | ||
} | ||
|
||
|
||
export async function handleCommandGuildElderFoundPlayerPacketRes(packet: CommandGuildElderFoundPlayerPacketRes, context: PacketContext): Promise<void> { | ||
const interaction = DiscordCache.getInteraction(context.discord!.interaction); | ||
if (!interaction) { | ||
return; | ||
} | ||
if (!packet.foundPlayer) { | ||
await sendErrorMessage( | ||
interaction.user, | ||
interaction, | ||
i18n.t("commands:guildElder.playerNotFound", {lng: interaction.userLanguage}), | ||
{sendManner: SendManner.REPLY} | ||
); | ||
} | ||
} | ||
|
||
export async function handleCommandGuildElderSameGuildPacketRes(packet: CommandGuildElderSameGuildPacketRes, context: PacketContext): Promise<void> { | ||
const interaction = DiscordCache.getInteraction(context.discord!.interaction); | ||
if (!interaction) { | ||
return; | ||
} | ||
if (!packet.sameGuild) { | ||
await sendErrorMessage( | ||
interaction.user, | ||
interaction, | ||
i18n.t("commands:guildElder.notSameGuild", {lng: interaction.userLanguage}), | ||
{sendManner: SendManner.REPLY} | ||
); | ||
} | ||
} | ||
|
||
export async function handleCommandGuildElderHimselfPacketRes(packet: CommandGuildElderHimselfPacketRes, context: PacketContext): Promise<void> { | ||
const interaction = DiscordCache.getInteraction(context.discord!.interaction); | ||
if (!interaction) { | ||
return; | ||
} | ||
if (packet.himself) { | ||
await sendErrorMessage( | ||
interaction.user, | ||
interaction, | ||
i18n.t("commands:guildElder.chiefError", {lng: interaction.userLanguage}), | ||
{sendManner: SendManner.REPLY} | ||
); | ||
} | ||
} | ||
|
||
export async function handleCommandGuildElderAlreadyElderPacketRes(packet: CommandGuildElderAlreadyElderPacketRes, context: PacketContext): Promise<void> { | ||
const interaction = DiscordCache.getInteraction(context.discord!.interaction); | ||
if (!interaction) { | ||
return; | ||
} | ||
if (packet.alreadyElder) { | ||
await sendErrorMessage( | ||
interaction.user, | ||
interaction, | ||
i18n.t("commands:guildElder.alreadyElder", {lng: interaction.userLanguage}), | ||
{sendManner: SendManner.REPLY} | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Handle the response of the server after a guild elder, | ||
* this packet is only sent if the promotion is refused | ||
* @param packet | ||
* @param context | ||
*/ | ||
export async function handleCommandGuildElderRefusePacketRes(packet: CommandGuildElderRefusePacketRes, context: PacketContext): Promise<void> { | ||
const originalInteraction = DiscordCache.getInteraction(context.discord!.interaction!); | ||
if (!originalInteraction) { | ||
return; | ||
} | ||
const buttonInteraction = DiscordCache.getButtonInteraction(context.discord!.buttonInteraction!); | ||
const promotedPlayer = (await KeycloakUtils.getUserByKeycloakId(keycloakConfig, packet.promotedKeycloakId))!; | ||
await buttonInteraction?.editReply({ | ||
embeds: [ | ||
new DraftBotEmbed().formatAuthor(i18n.t("commands:guildElder.canceledTitle", { | ||
lng: originalInteraction.userLanguage, | ||
pseudo: originalInteraction.user.displayName | ||
}), originalInteraction.user) | ||
.setDescription( | ||
i18n.t("commands:guildElder.canceledDesc", { | ||
lng: originalInteraction.userLanguage, | ||
elderPseudo: promotedPlayer.attributes.gameUsername | ||
}) | ||
) | ||
.setErrorColor() | ||
] | ||
}); | ||
} | ||
|
||
/** | ||
* Handle the response of the server after a guild elder, | ||
* this packet is only sent if the promotion is accepted | ||
* @param packet | ||
* @param context | ||
*/ | ||
export async function handleCommandGuildElderAcceptPacketRes(packet: CommandGuildElderAcceptPacketRes, context: PacketContext): Promise<void> { | ||
const originalInteraction = DiscordCache.getInteraction(context.discord!.interaction!); | ||
const buttonInteraction = DiscordCache.getButtonInteraction(context.discord!.buttonInteraction!); | ||
const promotedPlayer = (await KeycloakUtils.getUserByKeycloakId(keycloakConfig, packet.promotedKeycloakId!))!; | ||
if (buttonInteraction && originalInteraction) { | ||
await buttonInteraction.editReply({ | ||
embeds: [ | ||
new DraftBotEmbed().formatAuthor(i18n.t("commands:guildElder.successElderAddTitle", { | ||
lng: originalInteraction.userLanguage, | ||
elderPseudo: promotedPlayer.attributes.gameUsername, | ||
guildName: packet.guildName | ||
}), originalInteraction.user) | ||
.setDescription( | ||
i18n.t("commands:guildElder.acceptedDesc", {lng: originalInteraction.userLanguage}) | ||
) | ||
] | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Promote a player from a guild | ||
*/ | ||
async function getPacket(interaction: DraftbotInteraction, user: KeycloakUser): Promise<CommandGuildElderPacketReq | null> { | ||
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, user); | ||
if (!askedPlayer || !askedPlayer.keycloakId) { | ||
return null; | ||
} | ||
return makePacket(CommandGuildElderPacketReq, {askedPlayerKeycloakId: askedPlayer.keycloakId}); | ||
} | ||
|
||
export const commandInfo: ICommand = { | ||
slashCommandBuilder: SlashCommandBuilderGenerator.generateBaseCommand("guildElder") | ||
.addUserOption(option => | ||
SlashCommandBuilderGenerator.generateOption("guildElder", "user", option) | ||
.setRequired(true)) as SlashCommandBuilder, | ||
getPacket, | ||
mainGuildCommand: false | ||
}; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Penser a bloquer / check le blocage / debloquer le joueur promu Aussi et pas seulement celui qui fait la commande