From 6effe2983e31a6dac75a1097100a147c27ac26c2 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sun, 12 Nov 2023 14:54:42 -0500 Subject: [PATCH] Some final updates for today, trying to keep track of how many people are in a particular voice channel --- breadbot.js | 24 ++++++++++++++++++++++-- utilities/sqlutil.js | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/breadbot.js b/breadbot.js index 8b9c66d..d96d92b 100644 --- a/breadbot.js +++ b/breadbot.js @@ -3,6 +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'); sqlutil.buildPool('breadbot_test') @@ -82,9 +83,25 @@ client.on(Events.GuildCreate, async guild => { client.on(Events.VoiceStateUpdate, async (oldState, newState) => { if (oldState.channel== null && newState.channel != null) { - console.log(`User ${newState.member.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 did_update = await sqlutil.updateVoiceActiveUsers(newState.guild.id, newState.channelId, true) + + if (did_update) { + console.log("\t Registered another user as participating in this voice channel") + } else { + console.log("\t Failed to register this user as participating in this voice channel") + } } else if (oldState.channel != null && newState.channel == null) { - console.log(`User ${oldState.member.username} left channel ${oldState.channel.name} in guild ${oldState.guild.name}`) + 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) + + 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") + } } }) @@ -94,6 +111,9 @@ client.on(Events.MessageCreate, async message => { var channel_ok = await sqlutil.registerChannelIfMissing(message.channelId, message.channel.guild.id, message.channel.name) var user_ok = await sqlutil.registerUserIfMissing(message.author.id, message.author.username, message.author.displayName) + console.log(`Channel OK? ${channel_ok}`) + console.log(`User OK? ${user_ok}`) + if (channel_ok && user_ok) { sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(message_add => { if(message_add) { diff --git a/utilities/sqlutil.js b/utilities/sqlutil.js index 9b9569d..88867f7 100644 --- a/utilities/sqlutil.js +++ b/utilities/sqlutil.js @@ -92,11 +92,49 @@ async function registerMessage(message_snowflake, channel_snowflake, user_snowfl }) } +async function registerVoiceChannelIfMissing(server_snowflake, channel_snowflake) { + return connection_pool.query("SELECT * FROM voice_channel_active_users WHERE server_snowflake = ? AND channel_snowflake = ?", [server_snowflake, channel_snowflake]).then(async ([rows, fields]) => { + if(rows.length != 0) { + return true + } else { + return await connection_pool.query("INSERT INTO voice_channel_active_users VALUES (?, ?, 0)", [server_snowflake, channel_snowflake]).then((rows, fields) => { + return true + }) + } + }).catch((error) => { + console.log(error) + + return false + }) +} + +//Add is true, subtract is false +async function updateVoiceActiveUsers(server_snowflake, channel_snowflake, add_or_subtract) { + var voice_channel_ok = await registerVoiceChannelIfMissing(server_snowflake, channel_snowflake) + + if(voice_channel_ok) { + var sql = "" + + if(add_or_subtract) { + sql = "UPDATE voice_channel_active_users SET voice_active_users = voice_active_users + 1 WHERE server_snowflake = ? AND channel_snowflake = ?" + } else { + sql = "UPDATE voice_channel_active_users SET voice_active_users = voice_active_users - 1 WHERE server_snowflake = ? AND channel_snowflake = ?" + } + + return await connection_pool.query(sql, [server_snowflake, channel_snowflake]).then((rows_fields) => { + return true + }) + } else { + return false + } +} + module.exports = { buildPool, unregisterServer, registerMessage, registerServerIfMissing, registerChannelIfMissing, - registerUserIfMissing + registerUserIfMissing, + updateVoiceActiveUsers } \ No newline at end of file