Added a large amount of untested voice management code, stopping just short of actually connecting to/disconnecting from calls

This commit is contained in:
Bradley Bickford 2023-11-14 21:50:42 -05:00
parent 6effe2983e
commit d82c569363
2 changed files with 82 additions and 2 deletions

View File

@ -33,6 +33,8 @@ client.commands = new Collection();
const commandFiles = allFiles.filter(file => file.endsWith('.js'));
var activeCalls = {}
for (const file of commandFiles) {
const command = require(file);
@ -85,6 +87,8 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
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)
var did_update = await sqlutil.updateVoiceActiveUsers(newState.guild.id, newState.channelId, true)
if (did_update) {
@ -92,16 +96,51 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
} else {
console.log("\t Failed to register this user as participating in this voice channel")
}
var voice_active_users = await sqlutil.getVoiceActiveUsers(newState.guild.id, newState.channelId)
if (last_voice_active_users <= 0 && voice_active_users > 0) {
console.log("New call detected, getting set up")
var new_call_id = await sqlutil.registerNewCall(newState.guild.id, newState.channelId, new Date())
if (new_call_id != -1) {
console.log("New call successfully registered")
activeCalls[newState.guild.id.concat("|", newState.channelId)] = new_call_id
// Setup call connection for BreadBot and configure events here
} else {
console.log("Failed to generate a new call ID")
}
}
} else if (oldState.channel != null && newState.channel == null) {
console.log(`User ${oldState.member.user.username} left channel ${oldState.channel.name} in guild ${oldState.guild.name}`)
var did_update = await sqlutil.updateVoiceActiveUsers(newState.guild.id, newState.channelId, false)
var last_voice_active_users = await sqlutil.getVoiceActiveUsers(oldState.guild.id, oldState.channelId)
var did_update = await sqlutil.updateVoiceActiveUsers(oldState.guild.id, oldState.channelId, false)
if (did_update) {
console.log("\t Removed registered user as participating in this voice channel")
} else {
console.log("\t Failed to remove registered user as participating in this voice channel")
}
var voice_active_users = await sqlutil.getVoiceActiveUsers(oldState.guild.id, oldState.channelId)
if (last_voice_active_users > 0 && voice_active_users <= 0) {
console.log("End of call detected, tearing down")
var end_time_set = await sqlutil.updateCallEndTime(activeCalls[oldState.guild.id.concat("|", oldState.channelId)], new Date())
if (end_time_set) {
console.log("Call is ending, disconnecting BreadBot")
// Disconnect BreadBot and end connection here
delete activeCalls[oldState.guild.id.concat("|", oldState.channelId)]
} else {
console.log("Failed to properly set the end time of the call")
}
}
}
})

View File

@ -129,6 +129,44 @@ async function updateVoiceActiveUsers(server_snowflake, channel_snowflake, add_o
}
}
async function getVoiceActiveUsers(server_snowflake, channel_snowflake) {
return connection_pool.query("SELECT voice_active_users FROM voice_channel_active_users WHERE server_snowflake = ? AND channel_snowflake = ?", [server_snowflake, channel_snowflake]).then(async ([rows, fields]) => {
if (rows.length == 0) {
return -1;
} else {
return rows[0].voice_active_users
}
}).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) {
return -1;
} else {
return rows[0].insertId
}
}).catch((error) => {
console.log(error)
return -1;
})
}
async function updateCallEndTime(call_id, call_end_time) {
return await connection_pool.query("UPDATE call_states SET call_end_time = ? WHERE call_id = ?", [call_end_time, call_id]).then(async (rows, fields) => {
return true
}).catch((error) => {
console.log(error)
return false;
})
}
module.exports = {
buildPool,
unregisterServer,
@ -136,5 +174,8 @@ module.exports = {
registerServerIfMissing,
registerChannelIfMissing,
registerUserIfMissing,
updateVoiceActiveUsers
updateVoiceActiveUsers,
getVoiceActiveUsers,
registerNewCall,
updateCallEndTime
}