Skip to content

Commit

Permalink
various fixes language command #2273
Browse files Browse the repository at this point in the history
small fixes language command #2273

more fixes language command #2273

more fixes language command #2273

fix codacy

fix eslint
  • Loading branch information
BastLast committed Feb 29, 2024
1 parent 51a9c2d commit 7c0b6ae
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 123 deletions.
13 changes: 0 additions & 13 deletions Core/src/commands/admin/LanguageCommand.ts

This file was deleted.

5 changes: 0 additions & 5 deletions Core/src/core/constants/ServersConstants.ts

This file was deleted.

4 changes: 2 additions & 2 deletions Core/src/core/database/game/migrations/019-v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {readFileSync} from "fs";
import {KeycloakUtils} from "../../../../../../Lib/src/keycloak/KeycloakUtils";
import {KeycloakConfig} from "../../../../../../Lib/src/keycloak/KeycloakConfig";
import {logsV5NewIds} from "../../logs/migrations/006-v5";
import {StringConstants} from "../../../../../../Lib/src/constants/StringConstants";
import {LANGUAGE} from "../../../../../../Lib/src/Language";

export async function up({context}: { context: QueryInterface }): Promise<void> {
const configPath = `${process.cwd()}/config/keycloak.toml`;
Expand All @@ -29,7 +29,7 @@ clientSecret = "secret"

for (let i = 0; i < players.length; ++i) {
const player = players[i];
const user = await KeycloakUtils.getOrRegisterDiscordUser(config.keycloak, player.discordUserId as string, player.discordUserId as string, StringConstants.LANGUAGE.ENGLISH);
const user = await KeycloakUtils.getOrRegisterDiscordUser(config.keycloak, player.discordUserId as string, player.discordUserId as string, LANGUAGE.ENGLISH);
await context.sequelize.query(`UPDATE players SET discordUserId = "${user.id}" WHERE discordUserId = "${player.discordUserId}"`);
logsV5NewIds.set(player.discordUserId as string, user.id);
}
Expand Down
4 changes: 2 additions & 2 deletions Core/src/core/database/game/models/Server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DataTypes, Model, Sequelize} from "sequelize";
import {ServersConstants} from "../../../constants/ServersConstants";
import {LANGUAGE} from "../../../../../../Lib/src/Language";
import moment = require("moment");

export class Server extends Model {
Expand Down Expand Up @@ -33,7 +33,7 @@ export function initModel(sequelize: Sequelize): void {
},
language: {
type: DataTypes.STRING(2), // eslint-disable-line new-cap
defaultValue: ServersConstants.DEFAULT_LANGUAGE
defaultValue: LANGUAGE.DEFAULT_LANGUAGE
},
discordGuildId: {
type: DataTypes.STRING(64) // eslint-disable-line new-cap
Expand Down
24 changes: 12 additions & 12 deletions Discord/src/commands/CommandsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {DiscordWebSocket} from "../bot/Websocket";
import {PacketContext} from "../../../Lib/src/packets/DraftBotPacket";
import {DiscordCache} from "../bot/DiscordCache";
import {BotUtils} from "../utils/BotUtils";
import { StringConstants } from "../../../Lib/src/constants/StringConstants";
import {LANGUAGE} from "../../../Lib/src/Language";

export class CommandsManager {
static commands = new Map<string, ICommand>();
Expand Down Expand Up @@ -217,16 +217,16 @@ export class CommandsManager {
message.channel.send({
content: `
${i18n.t("bot:mentionHelp", {
lang: StringConstants.LANGUAGE.ENGLISH,
commandHelp: BotUtils.commandsMentions.get("help"),
commandLanguage: BotUtils.commandsMentions.get("language")
})}
lang: LANGUAGE.ENGLISH,

Check failure on line 220 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
commandHelp: BotUtils.commandsMentions.get("help"),

Check failure on line 221 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
commandLanguage: BotUtils.commandsMentions.get("language")

Check failure on line 222 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
})}

Check failure on line 223 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 1 tab but found 4
${i18n.t("bot:mentionHelp", {
lang: StringConstants.LANGUAGE.FRENCH,
commandHelp: BotUtils.commandsMentions.get("help"),
commandLanguage: BotUtils.commandsMentions.get("language")
})}`
lang: LANGUAGE.FRENCH,

Check failure on line 226 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
commandHelp: BotUtils.commandsMentions.get("help"),

Check failure on line 227 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
commandLanguage: BotUtils.commandsMentions.get("language")

Check failure on line 228 in Discord/src/commands/CommandsManager.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Expected indentation of 2 tabs but found 5
})}`
}).then();
});
}
Expand Down Expand Up @@ -302,7 +302,7 @@ ${i18n.t("bot:mentionHelp", {
attachmentList.push(new AttachmentBuilder(Buffer.from(message.content)).setName(`userMessage-${message.author.id}-${message.id}.txt`));
}
const supportAlert = i18n.t("bot:supportAlert", {
lang: StringConstants.LANGUAGE.FRENCH,
lang: LANGUAGE.FRENCH,
username: escapeUsername(message.author.username),
id: message.author.id
}) + (message.content.length > Constants.DM.MAX_MESSAGE_LENGTH_ALLOWED
Expand Down Expand Up @@ -336,7 +336,7 @@ ${i18n.t("bot:mentionHelp", {
if (!msg!.getFirstReaction()) {
return;
}
const language = msg!.getFirstReaction()!.emoji.name === Constants.REACTIONS.ENGLISH_FLAG ? StringConstants.LANGUAGE.ENGLISH : StringConstants.LANGUAGE.FRENCH;
const language = msg!.getFirstReaction()!.emoji.name === Constants.REACTIONS.ENGLISH_FLAG ? LANGUAGE.ENGLISH : LANGUAGE.FRENCH;
message.channel.send({
embeds: [new DraftBotEmbed()
.formatAuthor(i18n.t("bot:dmHelpMessageTitle", {
Expand All @@ -354,7 +354,7 @@ ${i18n.t("bot:mentionHelp", {
.formatAuthor(Constants.DM.TITLE_SUPPORT, author)
.setDescription(message instanceof DraftbotInteraction ? Constants.DM.INTERACTION_SUPPORT : Constants.DM.MESSAGE_SUPPORT);
const draftbotChannel = message.channel as unknown as DraftbotChannel;
draftbotChannel.language = StringConstants.LANGUAGE.ENGLISH;
draftbotChannel.language = LANGUAGE.ENGLISH;
message instanceof Message ? await helpMessage.send(draftbotChannel) : await helpMessage.reply(message);
}

Expand Down
27 changes: 13 additions & 14 deletions Discord/src/commands/SlashCommandBuilderGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
*/
import {ApplicationCommandOptionBase, SlashCommandBuilder} from "@discordjs/builders";
import i18n from "../translations/i18n";
import {Constants} from "../Constants";
import {SlashCommandStringOption} from "discord.js";
import {TopConstants} from "../../../Lib/src/constants/TopConstants";
import { StringConstants } from "../../../Lib/src/constants/StringConstants";
import {LANGUAGE} from "../../../Lib/src/Language";


export class SlashCommandBuilderGenerator {
Expand All @@ -17,13 +16,13 @@ export class SlashCommandBuilderGenerator {
*/
static generateBaseCommand(commandSectionName: string): SlashCommandBuilder {
return new SlashCommandBuilder()
.setName(i18n.t(`discordBuilder:${commandSectionName}.name`, { lng: StringConstants.LANGUAGE.ENGLISH }))
.setName(i18n.t(`discordBuilder:${commandSectionName}.name`, {lng: LANGUAGE.ENGLISH}))
.setNameLocalizations({
fr: i18n.t(`discordBuilder:${commandSectionName}.name`, { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t(`discordBuilder:${commandSectionName}.name`, {lng: LANGUAGE.FRENCH})
})
.setDescription(i18n.t(`discordBuilder:${commandSectionName}.description`, { lng: StringConstants.LANGUAGE.ENGLISH }))
.setDescription(i18n.t(`discordBuilder:${commandSectionName}.description`, {lng: LANGUAGE.ENGLISH}))
.setDescriptionLocalizations({
fr: i18n.t(`discordBuilder:${commandSectionName}.description`, { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t(`discordBuilder:${commandSectionName}.description`, {lng: LANGUAGE.FRENCH})
});
}

Expand All @@ -34,13 +33,13 @@ export class SlashCommandBuilderGenerator {
* @param option Option to populate
*/
static generateOption<T extends ApplicationCommandOptionBase>(commandSectionName: string, optionSectionName: string, option: T): T {
return option.setName(i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.name`, { lng: StringConstants.LANGUAGE.ENGLISH }))
return option.setName(i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.name`, {lng: LANGUAGE.ENGLISH}))
.setNameLocalizations({
fr: i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.name`, { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.name`, {lng: LANGUAGE.FRENCH})
})
.setDescription(i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.description`, { lng: StringConstants.LANGUAGE.ENGLISH }))
.setDescription(i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.description`, {lng: LANGUAGE.ENGLISH}))
.setDescriptionLocalizations({
fr: i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.description`, { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t(`discordBuilder:${commandSectionName}.options.${optionSectionName}.description`, {lng: LANGUAGE.FRENCH})
});
}

Expand All @@ -58,16 +57,16 @@ export class SlashCommandBuilderGenerator {
return SlashCommandBuilderGenerator.generateOption(commandSectionName, optionSectionName, option)
.addChoices(
{
name: i18n.t("discordBuilder:scopes.global", { lng: StringConstants.LANGUAGE.ENGLISH }),
name: i18n.t("discordBuilder:scopes.global", {lng: LANGUAGE.ENGLISH}),
"name_localizations": {
fr: i18n.t("discordBuilder:scopes.global", { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t("discordBuilder:scopes.global", {lng: LANGUAGE.FRENCH})
}, value: TopConstants.GLOBAL_SCOPE
},
{
name: i18n.t("discordBuilder:scopes.server", { lng: StringConstants.LANGUAGE.ENGLISH }),
name: i18n.t("discordBuilder:scopes.server", {lng: LANGUAGE.ENGLISH}),
"name_localizations":
{
fr: i18n.t("discordBuilder:scopes.server", { lng: StringConstants.LANGUAGE.FRENCH })
fr: i18n.t("discordBuilder:scopes.server", {lng: LANGUAGE.FRENCH})
}
,
value: TopConstants.SERVER_SCOPE
Expand Down
35 changes: 18 additions & 17 deletions Discord/src/commands/admin/LanguageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ import {
import {DraftbotInteraction} from "../../messages/DraftbotInteraction";
import i18n from "../../translations/i18n";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {StringConstants} from "../../../../Lib/src/constants/StringConstants";
import {PermissionsConstants} from "../../constants/PermissionsConstants";
import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser";
import {KeycloakUtils} from "../../../../Lib/src/keycloak/KeycloakUtils";
import {keycloakConfig} from "../../bot/DraftBotShard";
import {Constants} from "../../Constants";
import {sendInteractionNotForYou} from "../../utils/ErrorUtils";
import {Language} from "../../../../Lib/src/Language";
import {LANGUAGE, Language} from "../../../../Lib/src/Language";

/**
* Allow an admin to change the prefix the bot uses in a specific server
* Change the language used by the bot to interact with the player
*/
async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<null> {
const selectLanguageMenuId = "languageSelectionMenu";
const selectLanguageMenuOptions = Object.keys(StringConstants.LANGUAGE)
.map((key) => {
const languageCode = StringConstants.LANGUAGE[key as keyof typeof StringConstants.LANGUAGE];
return new StringSelectMenuOptionBuilder()
.setLabel(i18n.t(`commands:language.languages.${languageCode}.name`, {lng: interaction.userLanguage}))
.setEmoji(i18n.t(`commands:language.languages.${languageCode}.emoji`, {lng: interaction.userLanguage}))
.setValue(languageCode);
});

const selectLanguageMenuOptions = LANGUAGE.LANGUAGES
.map((languageCode) => new StringSelectMenuOptionBuilder()
.setLabel(i18n.t(`commands:language.languages.${languageCode}.name`, {lng: interaction.userLanguage}))
.setEmoji(i18n.t(`commands:language.languages.${languageCode}.emoji`, {lng: interaction.userLanguage}))
.setValue(languageCode)
);

const languageSelectionMenu = new StringSelectMenuBuilder()
.setCustomId(selectLanguageMenuId)
.setPlaceholder(i18n.t("commands:language.selectLanguage", {lng: interaction.userLanguage}))
.addOptions(selectLanguageMenuOptions);

const row = new ActionRowBuilder<StringSelectMenuBuilder>()
.addComponents(languageSelectionMenu);

const msg = await interaction.reply({
embeds: [new DraftBotEmbed()
.setTitle(i18n.t("commands:language.title", {
Expand All @@ -54,18 +54,21 @@ async function getPacket(interaction: DraftbotInteraction, keycloakUser: Keycloa
});

collector.on("collect", async (menuInteraction: StringSelectMenuInteraction) => {

if (menuInteraction.user.id !== interaction.user.id) {
await sendInteractionNotForYou(menuInteraction.user, menuInteraction, interaction.userLanguage);
return;
}

await KeycloakUtils.updateUserLanguage(keycloakConfig, keycloakUser, menuInteraction.values[0] as Language);

await menuInteraction.reply({
embeds: [new DraftBotEmbed()
.setTitle(i18n.t("commands:language.newLanguageSetTitle", {
lng: menuInteraction.values[0] as Language
lng: menuInteraction.values[0] as Language
}))
.setDescription(i18n.t("commands:language.newLanguageSetDescription", {
lng: menuInteraction.values[0] as Language
lng: menuInteraction.values[0] as Language
}))]
})
});
Expand All @@ -75,8 +78,6 @@ async function getPacket(interaction: DraftbotInteraction, keycloakUser: Keycloa
export const commandInfo: ICommand = {
slashCommandBuilder: SlashCommandBuilderGenerator.generateBaseCommand("language"),
getPacket,
requirements: {
userPermission: PermissionsConstants.ROLES.USER.ADMINISTRATOR
},
requirements: {},
mainGuildCommand: false
};
6 changes: 2 additions & 4 deletions Discord/src/commands/player/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import {SlashCommandBuilderGenerator} from "../SlashCommandBuilderGenerator";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {SlashCommandBuilder} from "@discordjs/builders";
import {BotUtils} from "../../utils/BotUtils";
import {Language} from "../../../../Lib/src/Language";
import {LANGUAGE, Language} from "../../../../Lib/src/Language";
import {PetConstants} from "../../../../Lib/src/constants/PetConstants";
import {HelpConstants} from "../../constants/HelpConstants";
import {Constants} from "../../Constants";
import { StringConstants } from "../../../../Lib/src/constants/StringConstants";

/**
* Get the list of commands mention from the command data
Expand Down Expand Up @@ -183,7 +181,7 @@ function generateReplacementObjectForHelpCommand(interaction: DraftbotInteractio
*/
async function getPacket(interaction: DraftbotInteraction): Promise<null> {
const helpMessage = new DraftBotEmbed();
const command = interaction.options.get(i18n.t("discordBuilder:help.options.commandName.name", {lng: StringConstants.LANGUAGE.ENGLISH}));
const command = interaction.options.get(i18n.t("discordBuilder:help.options.commandName.name", {lng: LANGUAGE.ENGLISH}));
const askedCommand = command ? command.value as string : null;
if (!askedCommand) {
generateGenericHelpMessage(helpMessage, interaction);
Expand Down
1 change: 0 additions & 1 deletion Discord/src/messages/DraftBotErrorEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {DraftBotEmbed} from "./DraftBotEmbed";
import {User} from "discord.js";
import {DraftbotInteraction} from "./DraftbotInteraction";
import i18n from "../translations/i18n";
import {Language} from "../../../Lib/src/Language";

/**
* Default error embed with the title and description formatted. If you just want the red color, see {@link DraftBotEmbed#setErrorColor}
Expand Down
13 changes: 7 additions & 6 deletions Discord/src/messages/DraftbotInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import {
BaseGuildTextChannel,
Client,
CommandInteraction,
GuildTextBasedChannel, InteractionEditReplyOptions,
GuildTextBasedChannel,
InteractionEditReplyOptions,
InteractionReplyOptions,
Message,
MessageCreateOptions, MessagePayload
MessageCreateOptions,
MessagePayload
} from "discord.js";
import {RawInteractionData, RawWebhookData} from "discord.js/typings/rawDataTypes";
import i18n from "../translations/i18n";
import {Language} from "../../../Lib/src/Language";
import { StringConstants } from "../../../Lib/src/constants/StringConstants";
import {LANGUAGE, Language} from "../../../Lib/src/Language";

type DraftbotInteractionWithoutSendCommands = new(client: Client<true>, data: RawInteractionData) => Omit<CommandInteraction, "reply" | "followUp" | "channel">;
const DraftbotInteractionWithoutSendCommands: DraftbotInteractionWithoutSendCommands = CommandInteraction as unknown as DraftbotInteractionWithoutSendCommands;
Expand All @@ -27,7 +28,7 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
// @ts-ignore
private _channel: DraftbotChannel;

public userLanguage: Language = StringConstants.DEFAULT_LANGUAGE;
public userLanguage: Language = LANGUAGE.DEFAULT_LANGUAGE;

private _replyEdited = false;

Expand Down Expand Up @@ -102,7 +103,7 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
* @private
*/
private async manageFallback(functionPrototype: ReplyFunctionLike): Promise<void> {
const errorText = i18n.t("bot:noSpeakPermission", { lang: this.channel.language });
const errorText = i18n.t("bot:noSpeakPermission", {lang: this.channel.language});
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion Discord/src/packetHandlers/handlers/CommandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ export default class CommandHandlers {

@packetHandler(CommandRarityPacketRes)
async rarityRes(socket: WebSocket, packet: CommandRarityPacketRes, context: PacketContext): Promise<void> {
handleCommandRarityPacketRes(packet, context).then();
await handleCommandRarityPacketRes(packet, context);
}
}
4 changes: 2 additions & 2 deletions Discord/src/translations/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import * as deEvents from "../../../Lang/de/events.json";
import * as esEvents from "../../../Lang/es/events.json";
import * as ptEvents from "../../../Lang/pt/events.json";
import * as itEvents from "../../../Lang/it/events.json";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";
import {LANGUAGE} from "../../../Lib/src/Language";

// Todo load automatically modules

i18next.init({
fallbackLng: StringConstants.DEFAULT_LANGUAGE,
fallbackLng: LANGUAGE.DEFAULT_LANGUAGE,
resources: {
en: {
bot: enBot,
Expand Down
4 changes: 0 additions & 4 deletions Lang/en/discordBuilder.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"description": "Displays information about badges.",
"name": "badges"
},
"vote": {
"description": "Displays the link to vote for the bot.",
"name": "vote"
},
"idea": {
"description": "Share with us your idea to improve the game.",
"name": "idea"
Expand Down
17 changes: 17 additions & 0 deletions Lib/src/Language.ts
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
export type Language = "fr" | "en" | "it" | "es" | "de" | "pt";

export class LANGUAGE {
static readonly FRENCH: Language = "fr";
static readonly ENGLISH: Language = "en";

Check warning on line 5 in Lib/src/Language.ts

View workflow job for this annotation

GitHub Actions / eslint-lib-module

Expected blank line between class members
static readonly ITALIAN: Language = "it";

Check warning on line 6 in Lib/src/Language.ts

View workflow job for this annotation

GitHub Actions / eslint-lib-module

Expected blank line between class members
static readonly SPANISH: Language = "es";

Check warning on line 7 in Lib/src/Language.ts

View workflow job for this annotation

GitHub Actions / eslint-lib-module

Expected blank line between class members
static readonly PORTUGUESE: Language = "pt";

Check warning on line 8 in Lib/src/Language.ts

View workflow job for this annotation

GitHub Actions / eslint-lib-module

Expected blank line between class members
static readonly GERMAN: Language = "de";

Check warning on line 9 in Lib/src/Language.ts

View workflow job for this annotation

GitHub Actions / eslint-lib-module

Expected blank line between class members

static get LANGUAGES(): Language[] {
return Object.values(LANGUAGE);
}

static get DEFAULT_LANGUAGE(): Language {
return LANGUAGE.ENGLISH;
}
}
Loading

0 comments on commit 7c0b6ae

Please sign in to comment.