Initial update to try and fix #1
This commit is contained in:
42
Client/utilities/configHandler.js
Normal file
42
Client/utilities/configHandler.js
Normal file
@@ -0,0 +1,42 @@
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||
const log = new DebugBuilder("client-bot", "configController");
|
||||
// Modules
|
||||
const { readFileSync } = require('fs');
|
||||
const path = require("path");
|
||||
|
||||
exports.getConfig = function getConfig() {
|
||||
return JSON.parse(readFileSync(path.resolve("discord-bot/config/botConfig.json")));
|
||||
}
|
||||
|
||||
exports.getGuildID = function getGuildID() {
|
||||
const parsedJSON = getConfig();
|
||||
const guildID = parsedJSON.GuildID;
|
||||
|
||||
log.DEBUG("Guild ID: ", guildID);
|
||||
return guildID;
|
||||
}
|
||||
|
||||
exports.getApplicationID = function getApplicationID() {
|
||||
const parsedJSON = getConfig();
|
||||
const appID = parsedJSON.ApplicationID;
|
||||
|
||||
log.DEBUG("Application ID: ", appID);
|
||||
return appID;
|
||||
}
|
||||
|
||||
exports.getDeviceID = function getDeviceID(){
|
||||
const parsedJSON = getConfig();
|
||||
const deviceID = parseInt(parsedJSON.DeviceID);
|
||||
|
||||
log.DEBUG("Device ID: ", deviceID);
|
||||
return deviceID;
|
||||
}
|
||||
|
||||
exports.getDeviceName = function getDeviceName(){
|
||||
const parsedJSON = getConfig();
|
||||
const deviceName = parsedJSON.DeviceName;
|
||||
|
||||
log.DEBUG("Device Name: ", deviceName);
|
||||
return deviceName;
|
||||
}
|
||||
49
Client/utilities/deployCommands.js
Normal file
49
Client/utilities/deployCommands.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const { REST, Routes } = require('discord.js');
|
||||
|
||||
require('dotenv').config();
|
||||
const token = process.env.TOKEN;
|
||||
//const clientId = process.env.clientId;
|
||||
//const guildId = process.env.guildId;
|
||||
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const { DebugBuilder } = require("./debugBuilder");
|
||||
const log = new DebugBuilder("client", "deployCommands");
|
||||
|
||||
const commands = [];
|
||||
// Grab all the command files from the commands directory you created earlier
|
||||
const commandsPath = path.resolve(__dirname, '../commands');
|
||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||
|
||||
exports.deploy = (clientId, guildIDs) => {
|
||||
log.DEBUG("Deploying commands for: ", guildIDs);
|
||||
if (Array.isArray(guildIDs)) guildIDs = [guildIDs];
|
||||
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
||||
for (const file of commandFiles) {
|
||||
const command = require(`${path.resolve(commandsPath, file)}`);
|
||||
commands.push(command.data.toJSON());
|
||||
}
|
||||
|
||||
// Construct and prepare an instance of the REST module
|
||||
const rest = new REST({ version: '10' }).setToken(token);
|
||||
|
||||
// and deploy your commands!
|
||||
for (const guildId of guildIDs){
|
||||
(async () => {
|
||||
try {
|
||||
log.DEBUG(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`);
|
||||
// The put method is used to fully refresh all commands in the guild with the current set
|
||||
const data = await rest.put(
|
||||
Routes.applicationGuildCommands(clientId, guildId),
|
||||
{ body: commands },
|
||||
);
|
||||
|
||||
log.DEBUG(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guildId}.`);
|
||||
} catch (error) {
|
||||
// And of course, make sure you catch and log any errors!
|
||||
log.ERROR("ERROR Deploying commands: ", error, "Body from error: ", commands);
|
||||
}
|
||||
})()
|
||||
}
|
||||
};
|
||||
49
Client/utilities/executeConsoleCommands.js
Normal file
49
Client/utilities/executeConsoleCommands.js
Normal file
@@ -0,0 +1,49 @@
|
||||
// Modules
|
||||
const { promisify } = require('util');
|
||||
const { exec } = require("child_process");
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||
// Global Vars
|
||||
const log = new DebugBuilder("client-bot", "executeConsoleCommands");
|
||||
const execCommand = promisify(exec);
|
||||
|
||||
|
||||
exports.executeAsyncConsoleCommand = async function executeAsyncConsoleCommand(consoleCommand) {
|
||||
// Check to see if the command is a real command
|
||||
// TODO needs to be improved
|
||||
const acceptableCommands = [ "arecord -L" ];
|
||||
if (!acceptableCommands.includes(consoleCommand)) {
|
||||
log.WARN("Console command is not acceptable: ", consoleCommand);
|
||||
return undefined;
|
||||
}
|
||||
log.DEBUG("Running console command: ", consoleCommand);
|
||||
|
||||
const tempOutput = await execCommand(consoleCommand);
|
||||
const output = tempOutput.stdout.trim();
|
||||
|
||||
log.DEBUG("Executed Console Command Response: ", output)
|
||||
|
||||
// TODO add some error checking
|
||||
return output;
|
||||
}
|
||||
|
||||
exports.returnAlsaDeviceObject = async function returnAlsaDeviceObject() {
|
||||
const listAlsaDevicesCommand = "arecord -L";
|
||||
const commandResponse = await executeAsyncConsoleCommand(listAlsaDevicesCommand);
|
||||
const brokenCommand = String(commandResponse).split('\n');
|
||||
var devices = [];
|
||||
var i = 0;
|
||||
|
||||
for (const responseLine of brokenCommand) {
|
||||
if (String(responseLine) && !String(responseLine).match(/^\s/g)) {
|
||||
const tempDevice = {
|
||||
id: i,
|
||||
name: responseLine
|
||||
}
|
||||
devices.push(tempDevice);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return devices;
|
||||
}
|
||||
9
Client/utilities/messageHandler.js
Normal file
9
Client/utilities/messageHandler.js
Normal file
@@ -0,0 +1,9 @@
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||
const log = new DebugBuilder("client-bot", "messageHandler");
|
||||
|
||||
exports.replyToInteraction = async function replyToInteraction(interaction, message){
|
||||
interaction.reply({ content: message, fetchReply: true })
|
||||
.then((message) => log.DEBUG(`Reply sent with content ${message.content}`))
|
||||
.catch((err) => log.ERROR(err));
|
||||
}
|
||||
Reference in New Issue
Block a user