Skip to content
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

Add prefix to MQTT topics + small config clean up #2681 #2723

Open
wants to merge 2 commits into
base: draftbot-v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Core/config/config.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ""
BastLast marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 5 additions & 5 deletions Core/src/core/bot/DraftBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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 !");
Expand Down
8 changes: 4 additions & 4 deletions Core/src/core/bot/DraftBotConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ 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;
}

type ConfigStructure = {
bot: {
maintenance: boolean;
test_mode: boolean;
prefix: string;
};
others: {
nasa_api_key: string;
Expand All @@ -32,7 +33,6 @@ type ConfigStructure = {
password: string;
root_password: string;
port: number;
prefix: string;
};
mqtt: {
host: string;
Expand All @@ -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
};
}
Expand All @@ -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
};
}
8 changes: 4 additions & 4 deletions Core/src/core/utils/PacketUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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}`);
}
}
3 changes: 2 additions & 1 deletion Core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Discord/config/config.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions Discord/src/bot/DiscordMQTT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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});
}
}
});
Expand All @@ -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;
}
Expand All @@ -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));
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions Discord/src/config/DiscordConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -35,6 +35,7 @@ type ConfigStructure = {
token: string;
main_server_id: string;
test_mode: boolean;
prefix: string;
};
roles: {
badge_manager_ids: string;
Expand Down Expand Up @@ -65,7 +66,6 @@ type ConfigStructure = {
password: string;
root_password: string;
port: number;
prefix: string;
};
discord_bot_list: {
token: string;
Expand Down Expand Up @@ -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
};
}
Expand All @@ -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
};
}
6 changes: 3 additions & 3 deletions Discord/src/utils/PacketUtils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
12 changes: 0 additions & 12 deletions Lib/src/constants/MqttConstants.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading