Some final updates for today, trying to keep track of how many people are in a particular voice channel
This commit is contained in:
parent
a729545e01
commit
6effe2983e
24
breadbot.js
24
breadbot.js
@ -3,6 +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');
|
||||||
|
|
||||||
sqlutil.buildPool('breadbot_test')
|
sqlutil.buildPool('breadbot_test')
|
||||||
|
|
||||||
@ -82,9 +83,25 @@ 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) {
|
||||||
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) {
|
} 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 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)
|
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) {
|
if (channel_ok && user_ok) {
|
||||||
sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(message_add => {
|
sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(message_add => {
|
||||||
if(message_add) {
|
if(message_add) {
|
||||||
|
@ -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 = {
|
module.exports = {
|
||||||
buildPool,
|
buildPool,
|
||||||
unregisterServer,
|
unregisterServer,
|
||||||
registerMessage,
|
registerMessage,
|
||||||
registerServerIfMissing,
|
registerServerIfMissing,
|
||||||
registerChannelIfMissing,
|
registerChannelIfMissing,
|
||||||
registerUserIfMissing
|
registerUserIfMissing,
|
||||||
|
updateVoiceActiveUsers
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user