Skip to content

Commit

Permalink
feat: add activity text component
Browse files Browse the repository at this point in the history
  • Loading branch information
kariudo committed Feb 1, 2024
1 parent 475aae0 commit fc8d72f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 31 deletions.
5 changes: 1 addition & 4 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ MQTT_PORT=1883
MQTT_USERNAME=homeassistant
MQTT_PASSWORD=<YOUR_MQTT_PASSWORD>
TOPIC_DISCOVERY=homeassistant
TOPIC_CONNECTED=discordbot/connected
TOPIC_ONLINE=discordbot/online
TOPIC_COMMAND=discordbot/command
TOPIC_VOICE=discordbot/voice
TOPIC_BOT=discordbot
GUILD_ID=<YOUR_GUILD_ID>
YOUR_ID=<YOUR_ID>
BOT_ID=<YOUR_BOT_ID>
Expand Down
10 changes: 7 additions & 3 deletions src/discordUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
PermissionFlagsBits,
VoiceChannel,
} from "discord.js";
import type { MqttClient } from "mqtt";
import type { BotConfig } from "./models/BotConfig";

/**
Expand Down Expand Up @@ -92,8 +93,10 @@ export async function moveToChannelByName(
*/

export function setBotActivity(
discordClient: Client,
botActivity: string,
discordClient: Client,
mqttClient: MqttClient,
config: BotConfig,
): void {
console.info(`Setting bot activity: ${botActivity}`);
if (!discordClient.user) {
Expand All @@ -108,6 +111,7 @@ export function setBotActivity(
],
status: "online",
});
mqttClient.publish(config.mqtt.topics.activity, botActivity);
}
/**
* Prints the invite link generated by the discordClient with specific permissions and scopes.
Expand Down Expand Up @@ -191,8 +195,8 @@ export async function getVoiceChannels(
channels = channels.filter((c) =>
c
? self
.permissionsIn(c)
.has(PermissionFlagsBits.ViewChannel | PermissionFlagsBits.Connect)
.permissionsIn(c)
.has(PermissionFlagsBits.ViewChannel | PermissionFlagsBits.Connect)
: false,
);
return channels.filter(
Expand Down
2 changes: 1 addition & 1 deletion src/handleMqttMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const createHandleMqttMessage = (
*/
const handleMqttMessage = (topic: string, message: Buffer): void => {
if (topic === config.mqtt.topics.command)
processCommand(message.toString(), discordClient, config);
processCommand(message.toString(), discordClient, mqttClient, config);
if (topic === "homeassistant/status" && message.toString() === "online") {
console.debug("Message: Home Assistant MQTT Online...");
publishDiscoveryMessages(mqttClient, discordClient, config);
Expand Down
41 changes: 20 additions & 21 deletions src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,40 @@ import { throwEnvironmentError } from "./throwError";
* @return {BotConfig} the loaded configuration object
*/
export function loadConfig(): BotConfig {
const botTopic =
process.env["TOPIC_BOT"] ?? throwEnvironmentError("TOPIC_BOT");
return {
bot: {
token: process.env['BOT_TOKEN'] ?? throwEnvironmentError("BOT_TOKEN"),
token: process.env["BOT_TOKEN"] ?? throwEnvironmentError("BOT_TOKEN"),
nickname:
process.env['BOT_NICKNAME'] ?? throwEnvironmentError("BOT_NICKNAME"),
id: process.env['BOT_ID'] ?? throwEnvironmentError("BOT_ID"),
process.env["BOT_NICKNAME"] ?? throwEnvironmentError("BOT_NICKNAME"),
id: process.env["BOT_ID"] ?? throwEnvironmentError("BOT_ID"),
},
mqtt: {
url: process.env['MQTT_URL'] ?? throwEnvironmentError("MQTT_URL"),
port: process.env['MQTT_PORT'] ?? throwEnvironmentError("MQTT_PORT"),
url: process.env["MQTT_URL"] ?? throwEnvironmentError("MQTT_URL"),
port: process.env["MQTT_PORT"] ?? throwEnvironmentError("MQTT_PORT"),
username:
process.env['MQTT_USERNAME'] ?? throwEnvironmentError("MQTT_USERNAME"),
process.env["MQTT_USERNAME"] ?? throwEnvironmentError("MQTT_USERNAME"),
password:
process.env['MQTT_PASSWORD'] ?? throwEnvironmentError("MQTT_PASSWORD"),
process.env["MQTT_PASSWORD"] ?? throwEnvironmentError("MQTT_PASSWORD"),
clientId:
process.env['MQTT_CLIENT_ID'] ?? throwEnvironmentError("MQTT_CLIENT_ID"),
process.env["MQTT_CLIENT_ID"] ??
throwEnvironmentError("MQTT_CLIENT_ID"),
topics: {
connected:
process.env['TOPIC_CONNECTED'] ??
throwEnvironmentError("TOPIC_CONNECTED"),
discovery:
process.env['TOPIC_DISCOVERY'] ??
throwEnvironmentError("TOPIC_DISCOVERY"),
online:
process.env['TOPIC_ONLINE'] ?? throwEnvironmentError("TOPIC_ONLINE"),
command:
process.env['TOPIC_COMMAND'] ?? throwEnvironmentError("TOPIC_COMMAND"),
voice: process.env['TOPIC_VOICE'] ?? throwEnvironmentError("TOPIC_VOICE"),
bot: botTopic,
activity: `${botTopic}/activity`,
connected: `${botTopic}/connected`,
discovery: `${botTopic}/discovery`,
online: `${botTopic}/online`,
command: `${botTopic}/command`,
voice: `${botTopic}/voice`,
},
},
guild: {
id: process.env['GUILD_ID'] ?? throwEnvironmentError("GUILD_ID"),
id: process.env["GUILD_ID"] ?? throwEnvironmentError("GUILD_ID"),
},
you: {
id: process.env['YOUR_ID'] ?? throwEnvironmentError("YOUR_ID"),
id: process.env["YOUR_ID"] ?? throwEnvironmentError("YOUR_ID"),
},
};
}
2 changes: 2 additions & 0 deletions src/models/BotConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export interface BotConfig {
password: string;
clientId: string;
topics: {
bot: string;
connected: string;
discovery: string;
online: string;
command: string;
voice: string;
activity: string;
};
};
guild: {
Expand Down
6 changes: 4 additions & 2 deletions src/processCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client, GuildMember } from "discord.js";
import type { MqttClient } from "mqtt";
import { setBotNickname } from "./discordUtility";
import { setBotActivity } from "./discordUtility";
import { moveToChannelByName } from "./discordUtility";
Expand All @@ -16,6 +17,7 @@ import { type BotConfig } from "./models/BotConfig";
export async function processCommand(
message: string,
discordClient: Client,
mqttClient: MqttClient,
config: BotConfig,
): Promise<void> {
const you: GuildMember = await getSelf(discordClient, config);
Expand All @@ -25,7 +27,7 @@ export async function processCommand(
return;
}
// Get the first word as the command, leave the rest as arguments.
const command: string = args.shift()?.toLowerCase();
const command: string = args.shift()?.toLowerCase() ?? "MISSING_COMMAND";
switch (command) {
case "mute":
// Confirm the user is connected to a voice channel.
Expand Down Expand Up @@ -88,7 +90,7 @@ export async function processCommand(
}
case "bot_activity": {
const botActivity: string = args.join(" ").trim();
setBotActivity(discordClient, botActivity);
setBotActivity(botActivity, discordClient, mqttClient, config);
break;
}
case "bot_nick": {
Expand Down
14 changes: 14 additions & 0 deletions src/publishDiscoveryMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ export async function publishDiscoveryMessages(
},
});

// Publish a text component for the bot activity.
discoveryComponents.push({
topic: `${config.mqtt.topics.discovery}/text/${deviceId}/activity/config`,
payload: {
name: "Bot Activity",
state_topic: config.mqtt.topics.activity,
command_topic: config.mqtt.topics.command,
command_template: "bot_activity {{ value }}",
unique_id: `${deviceId}_activity`,
device: device,
icon: "mdi:robot",
},
});

// Publish all the discovery components.
for (const component of discoveryComponents) {
mqttClient.publish(component.topic, JSON.stringify(component.payload), {
Expand Down

0 comments on commit fc8d72f

Please sign in to comment.