diff --git a/Core/config/config.default.toml b/Core/config/config.default.toml index 176ba5266..6091a83c3 100644 --- a/Core/config/config.default.toml +++ b/Core/config/config.default.toml @@ -3,14 +3,14 @@ maintenance = false # Boolean to tell if the bot is under test mode test_mode = false +# Prefix used to prefix the MQTT topics and the databases. It must be the same for all the services. +prefix = "draftbot" [mqtt] # The MQTT server host host = "mqtt://127.0.0.1" [database] -# Database name prefix -prefix = "" # The host (ip or DNS) of the database host = "127.0.0.1" # The port of the database @@ -21,12 +21,3 @@ user = "draftbot" password = "secret_password" # The root password, used to create the databases root_password = "super_secret_password" - - -[backups] -# Enabled backups. May contain LOCAL and DROPBOX and must be separated with commas (e.g., LOCAL, DROPBOX) -enabled = "" -# Zip backup archive password -archive_password = "" -# The dropbox token used to back up the database -dropbox_token = "" \ No newline at end of file diff --git a/Core/src/core/bot/DraftBot.ts b/Core/src/core/bot/DraftBot.ts index 9e1537c6d..364cc4370 100644 --- a/Core/src/core/bot/DraftBot.ts +++ b/Core/src/core/bot/DraftBot.ts @@ -20,13 +20,13 @@ import {LeagueInfoConstants} from "../../../../Lib/src/constants/LeagueInfoConst import {PacketUtils} from "../utils/PacketUtils"; import {makePacket} from "../../../../Lib/src/packets/DraftBotPacket"; import {TopWeekAnnouncementPacket} from "../../../../Lib/src/packets/announcements/TopWeekAnnouncementPacket"; -import {MqttConstants} from "../../../../Lib/src/constants/MqttConstants"; import {TopWeekFightAnnouncementPacket} from "../../../../Lib/src/packets/announcements/TopWeekFightAnnouncementPacket"; import PlayerMissionsInfo from "../database/game/models/PlayerMissionsInfo"; import {ScheduledReportNotifications} from "../database/game/models/ScheduledReportNotification"; import {ReachDestinationNotificationPacket} from "../../../../Lib/src/packets/notifications/ReachDestinationNotificationPacket"; import {MapLocationDataController} from "../../data/MapLocation"; import * as fs from "fs"; +import {MqttTopicUtils} from "../../../../Lib/src/utils/MqttTopicUtils"; export class DraftBot { public readonly packetListener: PacketListenerServer; @@ -148,12 +148,12 @@ export class DraftBot { draftBotInstance.logsDatabase.log15BestSeason().then(); const winner = await DraftBot.findSeasonWinner(); if (winner !== null) { - PacketUtils.announce(makePacket(TopWeekFightAnnouncementPacket, {winnerKeycloakId: winner.keycloakId}), MqttConstants.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC); + PacketUtils.announce(makePacket(TopWeekFightAnnouncementPacket, {winnerKeycloakId: winner.keycloakId}), MqttTopicUtils.getDiscordTopWeekFightAnnouncementTopic(botConfig.PREFIX)); winner.addBadge("✨"); await winner.save(); } else { - PacketUtils.announce(makePacket(TopWeekFightAnnouncementPacket, {}), MqttConstants.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC); + PacketUtils.announce(makePacket(TopWeekFightAnnouncementPacket, {}), MqttTopicUtils.getDiscordTopWeekFightAnnouncementTopic(botConfig.PREFIX)); } await DraftBot.seasonEndQueries(); @@ -180,12 +180,12 @@ export class DraftBot { limit: 1 }); if (winner !== null) { - PacketUtils.announce(makePacket(TopWeekAnnouncementPacket, {winnerKeycloakId: winner.keycloakId}), MqttConstants.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC); + PacketUtils.announce(makePacket(TopWeekAnnouncementPacket, {winnerKeycloakId: winner.keycloakId}), MqttTopicUtils.getDiscordTopWeekAnnouncementTopic(botConfig.PREFIX)); winner.addBadge("🎗️"); await winner.save(); } else { - PacketUtils.announce(makePacket(TopWeekAnnouncementPacket, {}), MqttConstants.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC); + PacketUtils.announce(makePacket(TopWeekAnnouncementPacket, {}), MqttTopicUtils.getDiscordTopWeekAnnouncementTopic(botConfig.PREFIX)); } await Player.update({weeklyScore: 0}, {where: {}}); console.log("# WARNING # Weekly leaderboard has been reset !"); diff --git a/Core/src/core/bot/DraftBotConfig.ts b/Core/src/core/bot/DraftBotConfig.ts index 0acb5038a..7ca99eb7a 100644 --- a/Core/src/core/bot/DraftBotConfig.ts +++ b/Core/src/core/bot/DraftBotConfig.ts @@ -8,12 +8,12 @@ import {DatabaseConfiguration} from "../../../../Lib/src/database/DatabaseConfig export interface DraftBotConfig { MODE_MAINTENANCE: boolean; TEST_MODE: boolean; + PREFIX: string; MARIADB_HOST: string; MARIADB_USER: string; MARIADB_PASSWORD: string; MARIADB_ROOT_PASSWORD: string; MARIADB_PORT: number; - MARIADB_PREFIX: string; MQTT_HOST: string; } @@ -21,6 +21,7 @@ type ConfigStructure = { bot: { maintenance: boolean; test_mode: boolean; + prefix: string; }; others: { nasa_api_key: string; @@ -32,7 +33,6 @@ type ConfigStructure = { password: string; root_password: string; port: number; - prefix: string; }; mqtt: { host: string; @@ -47,12 +47,12 @@ export function loadConfig(): DraftBotConfig { return { MODE_MAINTENANCE: config.bot.maintenance, TEST_MODE: config.bot.test_mode, + PREFIX: config.bot.prefix, MARIADB_HOST: config.database.host, MARIADB_USER: config.database.user, MARIADB_PASSWORD: config.database.password, MARIADB_ROOT_PASSWORD: config.database.root_password, MARIADB_PORT: config.database.port, - MARIADB_PREFIX: config.database.prefix, MQTT_HOST: config.mqtt.host }; } @@ -66,6 +66,6 @@ export function getDatabaseConfiguration(config: DraftBotConfig, databaseName: s user: config.MARIADB_USER, userPassword: config.MARIADB_PASSWORD, databaseName, - prefix: config.MARIADB_PREFIX + prefix: config.PREFIX }; } \ No newline at end of file diff --git a/Core/src/core/utils/PacketUtils.ts b/Core/src/core/utils/PacketUtils.ts index e73b277fd..31015c341 100644 --- a/Core/src/core/utils/PacketUtils.ts +++ b/Core/src/core/utils/PacketUtils.ts @@ -1,9 +1,9 @@ import {DraftBotPacket, PacketContext} from "../../../../Lib/src/packets/DraftBotPacket"; -import {mqttClient} from "../../index"; +import {botConfig, mqttClient} from "../../index"; import {AnnouncementPacket} from "../../../../Lib/src/packets/announcements/AnnouncementPacket"; -import {MqttConstants} from "../../../../Lib/src/constants/MqttConstants"; import {NotificationPacket} from "../../../../Lib/src/packets/notifications/NotificationPacket"; import {NotificationsSerializedPacket} from "../../../../Lib/src/packets/notifications/NotificationsSerializedPacket"; +import {MqttTopicUtils} from "../../../../Lib/src/utils/MqttTopicUtils"; export abstract class PacketUtils { static sendPackets(context: PacketContext, packets: DraftBotPacket[]): void { @@ -17,7 +17,7 @@ export abstract class PacketUtils { if (context.discord !== null) { const response = JSON.stringify(responsePacket); - mqttClient.publish(MqttConstants.DISCORD_TOPIC, response); + mqttClient.publish(MqttTopicUtils.getDiscordTopic(botConfig.PREFIX), response); console.log(`Sent ${response} to discord front`); } else { @@ -45,7 +45,7 @@ export abstract class PacketUtils { packet: notification })) }; const json = JSON.stringify(serializedPackets); - mqttClient.publish(MqttConstants.NOTIFICATIONS, json, { retain: true, qos: 2 }); + mqttClient.publish(MqttTopicUtils.getNotificationsTopic(botConfig.PREFIX), json, { retain: true, qos: 2 }); console.log(`Sent notifications: ${json}`); } } \ No newline at end of file diff --git a/Core/src/index.ts b/Core/src/index.ts index 4f89cbaf7..0cbaa015b 100644 --- a/Core/src/index.ts +++ b/Core/src/index.ts @@ -9,6 +9,7 @@ import { connect } from "mqtt"; import {PacketUtils} from "./core/utils/PacketUtils"; import {MqttConstants} from "../../Lib/src/constants/MqttConstants"; import {RightGroup} from "../../Lib/src/enums/RightGroup"; +import {MqttTopicUtils} from "../../Lib/src/utils/MqttTopicUtils"; export const botConfig = loadConfig(); export let draftBotInstance: DraftBot = null; @@ -20,7 +21,7 @@ export const mqttClient = connect(botConfig.MQTT_HOST, { }); mqttClient.on("connect", () => { - mqttClient.subscribe(MqttConstants.CORE_TOPIC, (err) => { + mqttClient.subscribe(MqttTopicUtils.getCoreTopic(botConfig.PREFIX), (err) => { if (err) { console.error(err); process.exit(1); diff --git a/Discord/config/config.default.toml b/Discord/config/config.default.toml index 4b51b1779..3a6320fc7 100644 --- a/Discord/config/config.default.toml +++ b/Discord/config/config.default.toml @@ -5,14 +5,14 @@ token = "" main_server_id = "" # Test mode test_mode = false +# Prefix used to prefix the MQTT topics and the databases. It must be the same for all the services +prefix = "draftbot" [mqtt] # MQTT host host = "mqtt://127.0.0.1" [database] -# Database name prefix -prefix = "" # The host (ip or DNS) of the database host = "127.0.0.1" # The port of the database diff --git a/Discord/src/bot/DiscordMQTT.ts b/Discord/src/bot/DiscordMQTT.ts index 201048587..1bd33f072 100644 --- a/Discord/src/bot/DiscordMQTT.ts +++ b/Discord/src/bot/DiscordMQTT.ts @@ -12,6 +12,7 @@ import {LANGUAGE} from "../../../Lib/src/Language"; import {TextChannel} from "discord.js"; import {DraftBotEmbed} from "../messages/DraftBotEmbed"; import i18n from "../translations/i18n"; +import {MqttTopicUtils} from "../../../Lib/src/utils/MqttTopicUtils"; export class DiscordMQTT { static mqttClient: MqttClient; @@ -34,7 +35,7 @@ export class DiscordMQTT { private static handleGlobalMqttMessage(): void { DiscordMQTT.mqttClient.on("message", async (topic, message) => { - if (topic === MqttConstants.DISCORD_TOPIC) { + if (topic === MqttTopicUtils.getDiscordTopic(discordConfig.PREFIX)) { // Todo ignore if not the right shard const messageString = message.toString(); console.log(`Received message from topic ${topic}: ${messageString}`); @@ -72,7 +73,7 @@ export class DiscordMQTT { } } } - else if (topic === MqttConstants.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC) { + else if (topic === MqttTopicUtils.getDiscordTopWeekAnnouncementTopic(discordConfig.PREFIX)) { if (message.toString() === "") { console.log("No top week announcement in the MQTT topic"); return; @@ -82,10 +83,10 @@ export class DiscordMQTT { await DiscordAnnouncement.announceTopWeek(JSON.parse(message.toString())); // Clear the announcement so it doesn't get processed again - DiscordMQTT.mqttClient.publish(MqttConstants.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC, "", {retain: true}); + DiscordMQTT.mqttClient.publish(MqttTopicUtils.getDiscordTopWeekAnnouncementTopic(discordConfig.PREFIX), "", {retain: true}); } } - else if (topic === MqttConstants.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC) { + else if (topic === MqttTopicUtils.getDiscordTopWeekFightAnnouncementTopic(discordConfig.PREFIX)) { if (message.toString() === "") { console.log("No top week fight announcement in the MQTT topic"); return; @@ -95,7 +96,7 @@ export class DiscordMQTT { await DiscordAnnouncement.announceTopWeekFight(JSON.parse(message.toString())); // Clear the announcement so it doesn't get processed again - DiscordMQTT.mqttClient.publish(MqttConstants.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC, "", {retain: true}); + DiscordMQTT.mqttClient.publish(MqttTopicUtils.getDiscordTopWeekFightAnnouncementTopic(discordConfig.PREFIX), "", {retain: true}); } } }); @@ -116,18 +117,18 @@ export class DiscordMQTT { private static connectSubscribeAndHandleNotifications(): void { DiscordMQTT.notificationMqttClient = connect(discordConfig.MQTT_HOST, { connectTimeout: MqttConstants.CONNECTION_TIMEOUT, - clientId: MqttConstants.NOTIFICATIONS_CONSUMER, + clientId: MqttTopicUtils.getNotificationsConsumerTopic(discordConfig.PREFIX), clean: false // Keeps session active even if the client goes offline }); DiscordMQTT.notificationMqttClient.on("connect", () => { - DiscordMQTT.notificationMqttClient.publish(MqttConstants.NOTIFICATIONS, "", {retain: true}); // Clear the last notification to avoid processing it twice + DiscordMQTT.notificationMqttClient.publish(MqttTopicUtils.getNotificationsTopic(discordConfig.PREFIX), "", {retain: true}); // Clear the last notification to avoid processing it twice - DiscordMQTT.subscribeTo(DiscordMQTT.notificationMqttClient, MqttConstants.NOTIFICATIONS); + DiscordMQTT.subscribeTo(DiscordMQTT.notificationMqttClient, MqttTopicUtils.getNotificationsTopic(discordConfig.PREFIX)); }); DiscordMQTT.notificationMqttClient.on("message", (topic, message) => { - if (topic === MqttConstants.NOTIFICATIONS) { + if (topic === MqttTopicUtils.getNotificationsTopic(discordConfig.PREFIX)) { if (message.toString() === "") { return; } @@ -147,9 +148,9 @@ export class DiscordMQTT { }); DiscordMQTT.mqttClient.on("connect", () => { - DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttConstants.DISCORD_TOPIC); - DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttConstants.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC); - DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttConstants.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC); + DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttTopicUtils.getDiscordTopic(discordConfig.PREFIX)); + DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttTopicUtils.getDiscordTopWeekAnnouncementTopic(discordConfig.PREFIX)); + DiscordMQTT.subscribeTo(DiscordMQTT.mqttClient, MqttTopicUtils.getDiscordTopWeekFightAnnouncementTopic(discordConfig.PREFIX)); }); } } diff --git a/Discord/src/config/DiscordConfig.ts b/Discord/src/config/DiscordConfig.ts index f37623867..8c1e8b300 100644 --- a/Discord/src/config/DiscordConfig.ts +++ b/Discord/src/config/DiscordConfig.ts @@ -26,7 +26,7 @@ export interface DraftBotConfig { MARIADB_PASSWORD: string; MARIADB_ROOT_PASSWORD: string; MARIADB_PORT: number; - MARIADB_PREFIX: string; + PREFIX: string; DBL_TOKEN: string; } @@ -35,6 +35,7 @@ type ConfigStructure = { token: string; main_server_id: string; test_mode: boolean; + prefix: string; }; roles: { badge_manager_ids: string; @@ -65,7 +66,6 @@ type ConfigStructure = { password: string; root_password: string; port: number; - prefix: string; }; discord_bot_list: { token: string; @@ -98,7 +98,7 @@ export function loadConfig(): DraftBotConfig { MARIADB_PASSWORD: config.database.password, MARIADB_ROOT_PASSWORD: config.database.root_password, MARIADB_PORT: config.database.port, - MARIADB_PREFIX: config.database.prefix, + PREFIX: config.general.prefix, DBL_TOKEN: config.discord_bot_list.token }; } @@ -112,6 +112,6 @@ export function getDatabaseConfiguration(config: DraftBotConfig, databaseName: s user: config.MARIADB_USER, userPassword: config.MARIADB_PASSWORD, databaseName, - prefix: config.MARIADB_PREFIX + prefix: config.PREFIX }; } \ No newline at end of file diff --git a/Discord/src/utils/PacketUtils.ts b/Discord/src/utils/PacketUtils.ts index a1a3987c5..3517f00d6 100644 --- a/Discord/src/utils/PacketUtils.ts +++ b/Discord/src/utils/PacketUtils.ts @@ -1,12 +1,12 @@ import {DraftBotPacket, PacketContext} from "../../../Lib/src/packets/DraftBotPacket"; import {DiscordMQTT} from "../bot/DiscordMQTT"; -import {MqttConstants} from "../../../Lib/src/constants/MqttConstants"; import {KeycloakUtils} from "../../../Lib/src/keycloak/KeycloakUtils"; -import {keycloakConfig} from "../bot/DraftBotShard"; +import {discordConfig, keycloakConfig} from "../bot/DraftBotShard"; import {DraftBotErrorEmbed} from "../messages/DraftBotErrorEmbed"; import i18n from "../translations/i18n"; import {DraftbotInteraction} from "../messages/DraftbotInteraction"; import {KeycloakUser} from "../../../Lib/src/keycloak/KeycloakUser"; +import {MqttTopicUtils} from "../../../Lib/src/utils/MqttTopicUtils"; export type AskedPlayer = { keycloakId?: string, @@ -15,7 +15,7 @@ export type AskedPlayer = { export abstract class PacketUtils { static sendPacketToBackend(context: PacketContext, packet: DraftBotPacket): void { - DiscordMQTT.mqttClient!.publish(MqttConstants.CORE_TOPIC, JSON.stringify({ + DiscordMQTT.mqttClient!.publish(MqttTopicUtils.getCoreTopic(discordConfig.PREFIX), JSON.stringify({ packet: { name: packet.constructor.name, data: packet diff --git a/Lib/src/constants/MqttConstants.ts b/Lib/src/constants/MqttConstants.ts index 0d4dbef7f..814820572 100644 --- a/Lib/src/constants/MqttConstants.ts +++ b/Lib/src/constants/MqttConstants.ts @@ -1,15 +1,3 @@ export abstract class MqttConstants { - static readonly CORE_TOPIC = "draftbot_core"; - - static readonly DISCORD_TOPIC = "draftbot_discord"; - - static readonly DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC = "draftbot_discord_top_week_announcement"; - - static readonly DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC = "draftbot_discord_top_week_fight_announcement"; - - static readonly NOTIFICATIONS = "draftbot_notifications"; - - static readonly NOTIFICATIONS_CONSUMER = "notifications-consumer"; - static readonly CONNECTION_TIMEOUT = 10 * 1000; } \ No newline at end of file diff --git a/Lib/src/utils/MqttTopicUtils.ts b/Lib/src/utils/MqttTopicUtils.ts new file mode 100644 index 000000000..d634347f6 --- /dev/null +++ b/Lib/src/utils/MqttTopicUtils.ts @@ -0,0 +1,38 @@ +export abstract class MqttTopicUtils { + private static readonly CORE_TOPIC = "draftbot_core"; + + private static readonly DISCORD_TOPIC = "draftbot_discord"; + + private static readonly DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC = "draftbot_discord_top_week_announcement"; + + private static readonly DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC = "draftbot_discord_top_week_fight_announcement"; + + private static readonly NOTIFICATIONS = "draftbot_notifications"; + + private static readonly NOTIFICATIONS_CONSUMER = "notifications-consumer"; + + + static getCoreTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.CORE_TOPIC}`; + } + + static getDiscordTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.DISCORD_TOPIC}`; + } + + static getDiscordTopWeekAnnouncementTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.DISCORD_TOP_WEEK_ANNOUNCEMENT_TOPIC}`; + } + + static getDiscordTopWeekFightAnnouncementTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.DISCORD_TOP_WEEK_FIGHT_ANNOUNCEMENT_TOPIC}`; + } + + static getNotificationsTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.NOTIFICATIONS}`; + } + + static getNotificationsConsumerTopic(prefix: string): string { + return `${prefix}/${MqttTopicUtils.NOTIFICATIONS_CONSUMER}`; + } +} \ No newline at end of file