From 9e0c78d939f5cca6b77a97f2a3f3b0c91b34b96a Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sun, 19 Nov 2023 18:32:16 -0500 Subject: [PATCH] Major modifications to try out actual audio recording --- breadbot.js | 39 +++++++++++++++++++++++++++++++++++---- utilities/sqlutil.js | 17 ++++++++++++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/breadbot.js b/breadbot.js index 2c6e530..d23cbde 100644 --- a/breadbot.js +++ b/breadbot.js @@ -3,7 +3,7 @@ const path = require('node:path'); const { Client, Events, GatewayIntentBits, Collection } = require('discord.js'); const { token, mysql_username, mysql_password } = require('./config.json'); const sqlutil = require('./utilities/sqlutil'); -const { sql } = require('googleapis/build/src/apis/sql'); +const { Console } = require('node:console'); sqlutil.buildPool('breadbot_test') @@ -33,7 +33,7 @@ client.commands = new Collection(); const commandFiles = allFiles.filter(file => file.endsWith('.js')); -var activeCalls = {} +var activeCalls = [] for (const file of commandFiles) { const command = require(file); @@ -84,7 +84,38 @@ client.on(Events.GuildCreate, async guild => { }) client.on(Events.VoiceStateUpdate, async (oldState, newState) => { - if (oldState.channel== null && newState.channel != null) { + if (oldState.channel == null && newState.channel != null) { + var existingCallID = await sqlutil.inCall(newState.guild.id, newState.channelId) + + if (existingCallID == -1) { + var newCallID = await sqlutil.registerNewCall(newState.guild.id, newState.channelId, new Date()) + + // This should always have something to do, as all callIDs should be unique + fs.mkdirSync("." + path.sep + "media" + path.sep + newCallID, {recursive: true}) + + connection = newState.channel.join().then(conn => { + const receiver = conn.receiver + + conn.on("speaking", (user, speaking) => { + if (speaking) { + const audioStream = receiver.createStream(user, { mode: "pcm"}) + + const pathToFile = "." + path.sep + "media" + path.sep + newCallID + `${user.id}-${Date.now()}.pcm` + + audioStream.pipe(fs.createWriteStream(pathToFile)) + audioStream.on("end", () => { + // Do I really need to do anything here + }) + } + }) + }).catch(error => { + console.log(error) + }) + } + } else if (oldState.channel != null && newState.channel == null ) { + + } + /*if (oldState.channel== null && newState.channel != null) { console.log(`User ${newState.member.user.username} joined channel ${newState.channel.name} in guild ${newState.guild.name}`) var last_voice_active_users = await sqlutil.getVoiceActiveUsers(newState.guild.id, newState.channelId) @@ -141,7 +172,7 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => { console.log("Failed to properly set the end time of the call") } } - } + }*/ }) client.on(Events.MessageCreate, async message => { diff --git a/utilities/sqlutil.js b/utilities/sqlutil.js index a8967fe..073f7eb 100644 --- a/utilities/sqlutil.js +++ b/utilities/sqlutil.js @@ -143,6 +143,20 @@ async function getVoiceActiveUsers(server_snowflake, channel_snowflake) { }) } +async function inCall(server_snowflake, channel_snowflake) { + return connection_pool.query("SELECT call_id FROM call_states WHERE server_snowflake = ? AND channel_snowflake = ? AND call_end_time IS NULL", [server_snowflake, channel_snowflake]).then(async (rows, fields) => { + if (rows.length == 0) { + return -1; + } else { + return rows[0].call_id + } + }).catch((error) => { + console.log(error) + + return -1; + }) +} + async function registerNewCall(server_snowflake, channel_snowflake, call_start_time) { return connection_pool.query("INSERT INTO call_states VALUES (?, ?, ?)", [server_snowflake, channel_snowflake, call_start_time]).then(async (rows, fields) => { if (rows.length == 0) { @@ -177,5 +191,6 @@ module.exports = { updateVoiceActiveUsers, getVoiceActiveUsers, registerNewCall, - updateCallEndTime + updateCallEndTime, + inCall } \ No newline at end of file