Skip to content

Commit

Permalink
fix(topic): correct topic inference, inital pub of activity
Browse files Browse the repository at this point in the history
  • Loading branch information
kariudo committed Feb 1, 2024
1 parent 1686e60 commit ce189a5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/handleDiscordReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ export const createHandleDiscordReady = (
printInviteLink(discordClient);

console.info(`Discord: Logged in as "${discordClient.user.username}".`);
const initialActivity = "🏠 Watching the house";
discordClient.user.setPresence({
activities: [
{
name: "🏠 Watching the house",
name: initialActivity,
type: ActivityType.Custom,
},
],
status: "online",
});
mqttClient.publish(config.mqtt.topics.activity, initialActivity);
// If permissions allow, set the nickname to the custom one.
setBotNickname(config.bot.nickname, discordClient, config);
// Set initial state of the user.
Expand Down
5 changes: 5 additions & 0 deletions src/handleMqttConnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MqttClient } from "mqtt";
import { type BotConfig } from "./models/BotConfig";
import { publishDiscoveryMessages } from './publishDiscoveryMessages';
import type { Client } from 'discord.js';

/**
* Handles MQTT connection and subscribes to necessary topics. Publishes connected message and discovery messages.
Expand All @@ -9,6 +11,7 @@ import { type BotConfig } from "./models/BotConfig";
*/
export const CreateHandleMqttReady = (
mqttClient: MqttClient,
discordClient: Client,
config: BotConfig,
) => {
/**
Expand All @@ -25,6 +28,8 @@ export const CreateHandleMqttReady = (
qos: 1,
retain: true,
});
// Publish discovery messages.
publishDiscoveryMessages(mqttClient, discordClient, config)
};

return handleMqttConnect;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const discordClient: Client = createDiscordClient();
const mqttClient: MqttClient = createMqttClient(config);

// Bind handlers to listeners.
mqttClient.on("connect", CreateHandleMqttReady(mqttClient, config));
mqttClient.on("connect", CreateHandleMqttReady(mqttClient, discordClient, config));
mqttClient.on("error", handleMqttError);
mqttClient.on("close", handleMqttDisconnect);
mqttClient.on(
Expand Down
4 changes: 2 additions & 2 deletions src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function loadConfig(): BotConfig {
},
mqtt: {
url: process.env["MQTT_URL"] ?? throwEnvironmentError("MQTT_URL"),
port: process.env["MQTT_PORT"] ?? throwEnvironmentError("MQTT_PORT"),
port: process.env["MQTT_PORT"] ?? '1883',
username:
process.env["MQTT_USERNAME"] ?? throwEnvironmentError("MQTT_USERNAME"),
password:
Expand All @@ -29,10 +29,10 @@ export function loadConfig(): BotConfig {
process.env["MQTT_CLIENT_ID"] ??
throwEnvironmentError("MQTT_CLIENT_ID"),
topics: {
discovery: process.env['TOPIC_DISCOVERY'] ?? 'homeassistant',
bot: botTopic,
activity: `${botTopic}/activity`,
connected: `${botTopic}/connected`,
discovery: `${botTopic}/discovery`,
online: `${botTopic}/online`,
command: `${botTopic}/command`,
voice: `${botTopic}/voice`,
Expand Down

0 comments on commit ce189a5

Please sign in to comment.