Major modifications to try out actual audio recording

This commit is contained in:
Bradley Bickford 2023-11-19 18:32:16 -05:00
parent d82c569363
commit 9e0c78d939
2 changed files with 51 additions and 5 deletions

View File

@ -3,7 +3,7 @@ const path = require('node:path');
const { Client, Events, GatewayIntentBits, Collection } = require('discord.js'); const { Client, Events, GatewayIntentBits, Collection } = require('discord.js');
const { token, mysql_username, mysql_password } = require('./config.json'); const { token, mysql_username, mysql_password } = require('./config.json');
const sqlutil = require('./utilities/sqlutil'); const sqlutil = require('./utilities/sqlutil');
const { sql } = require('googleapis/build/src/apis/sql'); const { Console } = require('node:console');
sqlutil.buildPool('breadbot_test') sqlutil.buildPool('breadbot_test')
@ -33,7 +33,7 @@ client.commands = new Collection();
const commandFiles = allFiles.filter(file => file.endsWith('.js')); const commandFiles = allFiles.filter(file => file.endsWith('.js'));
var activeCalls = {} var activeCalls = []
for (const file of commandFiles) { for (const file of commandFiles) {
const command = require(file); const command = require(file);
@ -85,6 +85,37 @@ client.on(Events.GuildCreate, async guild => {
client.on(Events.VoiceStateUpdate, async (oldState, newState) => { 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}`) 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) 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") console.log("Failed to properly set the end time of the call")
} }
} }
} }*/
}) })
client.on(Events.MessageCreate, async message => { client.on(Events.MessageCreate, async message => {

View File

@ -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) { 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) => { return connection_pool.query("INSERT INTO call_states VALUES (?, ?, ?)", [server_snowflake, channel_snowflake, call_start_time]).then(async (rows, fields) => {
if (rows.length == 0) { if (rows.length == 0) {
@ -177,5 +191,6 @@ module.exports = {
updateVoiceActiveUsers, updateVoiceActiveUsers,
getVoiceActiveUsers, getVoiceActiveUsers,
registerNewCall, registerNewCall,
updateCallEndTime updateCallEndTime,
inCall
} }