Allow the client to interact with the discord bot

- The client can now interact with the bot, this allows the client to offer bot controls via API
- The master server will now be able to instruct a specific client to join a specific channel via channel ID (master server function coming soon)
- Suppress some debug statements to make the output easier to read when mixed in with the client debug output
This commit is contained in:
Logan Cusano
2022-12-18 20:31:54 -05:00
parent 6756a5e632
commit c8c75e8b37
9 changed files with 236 additions and 34 deletions

View File

@@ -1,4 +1,8 @@
import {joinVoiceChannel} from "@discordjs/voice";
// Debug
import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js";
const log = new ModuleDebugBuilder("bot", "join");
// Modules
import { joinVoiceChannel, VoiceConnectionStatus } from "@discordjs/voice";
import {replyToInteraction} from "../utilities/messageHandler.js";
import {createAudioInstance} from "../controllers/audioController.js";
import OpusEncoderPkg from "@discordjs/opus";
@@ -11,17 +15,29 @@ const encoder = new OpusEncoder(48000, 2);
* Join the specified voice channel
*
* @param interaction Message interaction from discord
* @param {string||any} guildID The specified Guild ID if this function is run from the client instead of from an interaction in Discord
* @param {string||any} channelID The channel ID to join
* @param guild The guild object to be used to create a voice adapter
* @param {function} callback The callback that will be needed if this function is run with a Guild ID instead of an interaction
*/
export default async function join(interaction){
const voiceChannel = interaction.options.getChannel('voicechannel');
export default async function join({interaction= undefined, guildID= undefined, channelID = undefined, guildObj = undefined, callback = undefined}){
if (interaction){
const voiceChannel = interaction.options.getChannel('voicechannel');
channelID = voiceChannel.id;
guildID = interaction.guildId;
guildObj = interaction.guild;
if (interaction) replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
}
log.DEBUG("Channel ID: ", channelID)
log.DEBUG("Guild ID: ", guildID)
const voiceConnection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator,
channelId: channelID,
guildId: guildID,
adapterCreator: guildObj.voiceAdapterCreator,
selfMute: false,
selfDeaf: false,
});
replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
const audioInstance = createAudioInstance();
@@ -32,5 +48,12 @@ export default async function join(interaction){
voiceConnection.playOpusPacket(encoded);
})
// Exit the audio handler when the bot disconnects
voiceConnection.on(VoiceConnectionStatus.Destroyed, () => {
audioInstance.quit();
})
audioInstance.start();
if (guildID && callback) callback();
}

View File

@@ -8,11 +8,24 @@ import {replyToInteraction} from "../utilities/messageHandler.js";
* If in a voice channel for the specified guild, leave
*
* @param interaction Message interaction from discord
* @param guildID
* @param callback
*/
export default async function leave(interaction){
const guildId = interaction.guild.id;
const voiceConnection = getVoiceConnection(guildId);
if (!voiceConnection) return replyToInteraction(interaction, "Not in a voice channel.");
export default async function leave({interaction = undefined, guildID= undefined, callback = undefined}) {
if(interaction) {
guildID = interaction.guild.id;
}
const voiceConnection = getVoiceConnection(guildID);
let response;
if (!voiceConnection){
response = "Not in a voice channel."
if (interaction) return replyToInteraction(interaction, response);
else callback(response);
}
voiceConnection.destroy();
return replyToInteraction(interaction, `Goodbye`);
response = "Goodbye"
if (interaction) return replyToInteraction(interaction, response);
else callback(response);
}

View File

@@ -7,15 +7,18 @@ import {getVoiceConnection} from "@discordjs/voice";
import { replyToInteraction } from '../utilities/messageHandler.js';
export default async function status({interaction= undefined, guildID= undefined}) {
export default async function status({interaction= undefined, guildID= undefined, callback = undefined}) {
//if (!interaction && !guildID) // Need error of sorts
if (interaction){
guildID = interaction.guild.id;
}
const voiceConnection = getVoiceConnection(guildID);
log.DEBUG("guildID: ", guildID)
log.DEBUG("Voice Connection: ", voiceConnection)
const statusObj = {
"guildID": guildID, "voiceConnection": voiceConnection
}
//log.DEBUG('Status Object: ', statusObj);
// get the status and return it accordingly (message reply / module)
@@ -23,6 +26,6 @@ export default async function status({interaction= undefined, guildID= undefined
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
}
else {
callback(statusObj);
}
}