Update bot to use command controller for interactions and calls
This commit is contained in:
@@ -2,15 +2,8 @@
|
|||||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||||
const log = new DebugBuilder("client", "ping");
|
const log = new DebugBuilder("client", "ping");
|
||||||
// Modules
|
// Modules
|
||||||
const { joinVoiceChannel, VoiceConnectionStatus } = require("@discordjs/voice");
|
|
||||||
const {replyToInteraction} = require("../utilities/messageHandler.js");
|
|
||||||
const { SlashCommandBuilder } = require('discord.js');
|
const { SlashCommandBuilder } = require('discord.js');
|
||||||
const {createAudioInstance} = require("../controllers/audioController.js");
|
const { join } = require("../controllers/commandController")
|
||||||
const OpusEncoderPkg = require("@discordjs/opus");
|
|
||||||
|
|
||||||
// Declare the encoder (module is incompatible modern import method)
|
|
||||||
const { OpusEncoder } = OpusEncoderPkg;
|
|
||||||
const encoder = new OpusEncoder(48000, 2);
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@@ -19,53 +12,6 @@ module.exports = {
|
|||||||
example: "join",
|
example: "join",
|
||||||
isPrivileged: false,
|
isPrivileged: false,
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await this.join({ interaction: interaction });
|
await join({ interaction: interaction });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
exports.join = 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: channelID,
|
|
||||||
guildId: guildID,
|
|
||||||
adapterCreator: guildObj.voiceAdapterCreator,
|
|
||||||
selfMute: false,
|
|
||||||
selfDeaf: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const audioInstance = await createAudioInstance();
|
|
||||||
|
|
||||||
audioInstance.on('audio', (buffer) => {
|
|
||||||
buffer = Buffer.from(buffer);
|
|
||||||
log.DEBUG("Audio buffer: ", buffer);
|
|
||||||
const encoded = encoder.encode(buffer);
|
|
||||||
// TODO Add a function here to check the volume of either buffer and only play audio to discord when there is audio to be played
|
|
||||||
voiceConnection.playOpusPacket(encoded);
|
|
||||||
})
|
|
||||||
|
|
||||||
// Exit the audio handler when the bot disconnects
|
|
||||||
voiceConnection.on(VoiceConnectionStatus.Destroyed, () => {
|
|
||||||
audioInstance.quit();
|
|
||||||
})
|
|
||||||
|
|
||||||
if (guildID && callback) callback();
|
|
||||||
else return;
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
const {getVoiceConnection} = require("@discordjs/voice");
|
|
||||||
const {replyToInteraction} = require("../utilities/messageHandler.js");
|
|
||||||
const { SlashCommandBuilder } = require('discord.js');
|
|
||||||
// Debug
|
// Debug
|
||||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||||
const log = new DebugBuilder("client-bot", "leave");
|
const log = new DebugBuilder("client-bot", "leave");
|
||||||
|
// Modules
|
||||||
|
const { SlashCommandBuilder } = require('discord.js');
|
||||||
|
const { leave } = require("../controllers/commandController")
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@@ -12,33 +12,6 @@ module.exports = {
|
|||||||
example: "leave",
|
example: "leave",
|
||||||
isPrivileged: false,
|
isPrivileged: false,
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await this.leave({ interaction: interaction })
|
await leave({ interaction: interaction })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If in a voice channel for the specified guild, leave
|
|
||||||
*
|
|
||||||
* @param interaction Message interaction from discord
|
|
||||||
* @param guildID
|
|
||||||
* @param callback
|
|
||||||
*/
|
|
||||||
exports.leave = 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();
|
|
||||||
|
|
||||||
response = "Goodbye"
|
|
||||||
if (interaction) return replyToInteraction(interaction, response);
|
|
||||||
else callback(response);
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||||
const log = new DebugBuilder("client-bot", "status");
|
const log = new DebugBuilder("client-bot", "status");
|
||||||
// Modules
|
// Modules
|
||||||
const { status } = require('../controllers/botController');
|
const { status } = require('../controllers/commandController');
|
||||||
// Utilities
|
// Utilities
|
||||||
|
|
||||||
const { SlashCommandBuilder } = require('discord.js');
|
const { SlashCommandBuilder } = require('discord.js');
|
||||||
|
|||||||
@@ -2,15 +2,7 @@
|
|||||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||||
const log = new DebugBuilder("client", "clientController");
|
const log = new DebugBuilder("client", "clientController");
|
||||||
// Modules
|
// Modules
|
||||||
const path = require('path');
|
const { status, join, leave } = require("./commandController")
|
||||||
const fork = require('child_process').fork;
|
|
||||||
const discordBotPath = path.resolve('discord-bot/app.js');
|
|
||||||
const {getVoiceConnection} = require("@discordjs/voice");
|
|
||||||
const {replyToInteraction} = require("../utilities/messageHandler.js");
|
|
||||||
// Commands
|
|
||||||
const { ping } = require('../commands/ping.js');
|
|
||||||
const { join } = require('../commands/join.js');
|
|
||||||
const { leave } = require('../commands/leave.js');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an object of client guilds
|
* Get an object of client guilds
|
||||||
@@ -30,7 +22,7 @@ exports.getStatus = (res, req) => {
|
|||||||
log.DEBUG("Guild IDs: ", guildIds);
|
log.DEBUG("Guild IDs: ", guildIds);
|
||||||
var guildStatuses = []
|
var guildStatuses = []
|
||||||
for (const guildID of guildIds){
|
for (const guildID of guildIds){
|
||||||
this.status({guildID: guildID, callback: (statusObj) => {
|
status({guildID: guildID, callback: (statusObj) => {
|
||||||
log.DEBUG("Status Object string: ", statusObj);
|
log.DEBUG("Status Object string: ", statusObj);
|
||||||
if (!statusObj.voiceConnection) guildStatuses.push({ guildID : 201 });
|
if (!statusObj.voiceConnection) guildStatuses.push({ guildID : 201 });
|
||||||
else guildStatuses.push({ guildID: 202 })
|
else guildStatuses.push({ guildID: 202 })
|
||||||
@@ -61,26 +53,3 @@ exports.leaveServer = (res, req) => {
|
|||||||
return res.sendStatus(202);
|
return res.sendStatus(202);
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.status = 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);
|
|
||||||
|
|
||||||
const statusObj = {
|
|
||||||
"guildID": guildID, "voiceConnection": voiceConnection
|
|
||||||
}
|
|
||||||
|
|
||||||
//log.DEBUG('Status Object: ', statusObj);
|
|
||||||
|
|
||||||
// get the status and return it accordingly (message reply / module)
|
|
||||||
|
|
||||||
if (interaction) {
|
|
||||||
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
callback(statusObj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
114
Client/controllers/commandController.js
Normal file
114
Client/controllers/commandController.js
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
// Debug
|
||||||
|
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||||
|
const log = new DebugBuilder("client-bot", "commandController");
|
||||||
|
// Modules
|
||||||
|
const {getVoiceConnection} = require("@discordjs/voice");
|
||||||
|
const {replyToInteraction} = require("../utilities/messageHandler.js");
|
||||||
|
const { joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection } = require("@discordjs/voice");
|
||||||
|
const {replyToInteraction} = require("../utilities/messageHandler.js");
|
||||||
|
const {createAudioInstance} = require("../controllers/audioController.js");
|
||||||
|
const { OpusEncoder } = require("@discordjs/opus");
|
||||||
|
|
||||||
|
// Declare the encoder
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
exports.join = 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: channelID,
|
||||||
|
guildId: guildID,
|
||||||
|
adapterCreator: guildObj.voiceAdapterCreator,
|
||||||
|
selfMute: false,
|
||||||
|
selfDeaf: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const audioInstance = await createAudioInstance();
|
||||||
|
|
||||||
|
audioInstance.on('audio', (buffer) => {
|
||||||
|
buffer = Buffer.from(buffer);
|
||||||
|
log.DEBUG("Audio buffer: ", buffer);
|
||||||
|
const encoded = encoder.encode(buffer);
|
||||||
|
// TODO Add a function here to check the volume of either buffer and only play audio to discord when there is audio to be played
|
||||||
|
voiceConnection.playOpusPacket(encoded);
|
||||||
|
})
|
||||||
|
|
||||||
|
// Exit the audio handler when the bot disconnects
|
||||||
|
voiceConnection.on(VoiceConnectionStatus.Destroyed, () => {
|
||||||
|
audioInstance.quit();
|
||||||
|
})
|
||||||
|
|
||||||
|
if (guildID && callback) callback();
|
||||||
|
else return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If in a voice channel for the specified guild, leave
|
||||||
|
*
|
||||||
|
* @param interaction Message interaction from discord
|
||||||
|
* @param guildID
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
exports.leave = 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();
|
||||||
|
|
||||||
|
response = "Goodbye"
|
||||||
|
if (interaction) return replyToInteraction(interaction, response);
|
||||||
|
else callback(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the voice status of the bots
|
||||||
|
* @param {*} param0
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
exports.status = 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);
|
||||||
|
|
||||||
|
const statusObj = {
|
||||||
|
"guildID": guildID, "voiceConnection": voiceConnection
|
||||||
|
}
|
||||||
|
|
||||||
|
log.DEBUG('Status Object: ', statusObj);
|
||||||
|
|
||||||
|
// get the status and return it accordingly (message reply / module)
|
||||||
|
|
||||||
|
if (interaction) {
|
||||||
|
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callback(statusObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user