Some cleanup, and transitioning breadbot.js to use winston for logging
This commit is contained in:
parent
9fc901a2ef
commit
f7e0c4b15a
120
breadbot.js
120
breadbot.js
@ -2,11 +2,31 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { Client, Events, GatewayIntentBits, Collection } = require('discord.js');
|
||||
const { joinVoiceChannel, getVoiceConnection, entersState, VoiceConnectionStatus, EndBehaviorType } = require('@discordjs/voice')
|
||||
const { token, media_voice_folder } = require('./config.json');
|
||||
const { token, media_voice_folder, breadbot_logging_config } = require('./config.json');
|
||||
const winston = require('winston')
|
||||
const winston_mysql = require('winston-mysql')
|
||||
const sqlutil = require('./utilities/sqlutil');
|
||||
const { Console } = require('node:console');
|
||||
const prism = require('prism-media')
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: "silly",
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.simple(),
|
||||
level: breadbot_logging_config["console_log_level"]
|
||||
}),
|
||||
new winston_mysql({
|
||||
level: breadbot_logging_config["sql_log_level"],
|
||||
host: breadbot_logging_config["mysql_host"],
|
||||
user: breadbot_logging_config["mysql_username"],
|
||||
password: breadbot_logging_config["mysql_password"],
|
||||
database: breadbot_logging_config["mysql_db_name"],
|
||||
table: breadbot_logging_config["mysql_table_name"]
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
sqlutil.buildPool()
|
||||
|
||||
const getAllFiles = function(directoryPath, arrayOfFiles) {
|
||||
@ -39,10 +59,12 @@ getAllFiles('.' + path.sep + 'commands', [])
|
||||
|
||||
if ('enabled' in command && command.enabled && 'data' in command && 'execute' in command) {
|
||||
client.commands.set(command.data.name, command);
|
||||
console.log(`[INFO] Loaded command at ${file}`);
|
||||
//console.log(`[INFO] Loaded command at ${file}`);
|
||||
logger.info(`Loaded command at ${file}`)
|
||||
}
|
||||
else {
|
||||
console.log(`[WARNING] The command at ${file} is missing a required "data" or "execute" property or is not enabled.`);
|
||||
//console.log(`[WARNING] The command at ${file} is missing a required "data" or "execute" property or is not enabled.`);
|
||||
logger.warn(`The command at ${file} is missing a required "data" or "execute" property or is not enabled`)
|
||||
}
|
||||
});
|
||||
|
||||
@ -52,7 +74,8 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||
const command = interaction.client.commands.get(interaction.commandName);
|
||||
|
||||
if (!command) {
|
||||
console.error(`No command matching ${interaction.commandName} was found.`);
|
||||
//console.error(`No command matching ${interaction.commandName} was found.`);
|
||||
logger.error(`No command matching ${interaction.commandName} was found`)
|
||||
return;
|
||||
}
|
||||
|
||||
@ -60,49 +83,58 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||
await command.execute(interaction);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
//console.error(error);
|
||||
logger.error(error)
|
||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||
}
|
||||
});
|
||||
|
||||
client.on(Events.GuildCreate, async guild => {
|
||||
if (guild.available) {
|
||||
console.log('Got into a server')
|
||||
console.log(`The server name is ${guild.name}`)
|
||||
console.log(`The server description is ${guild.description}`)
|
||||
console.log(`The server snowflake is ${guild.id}`)
|
||||
//console.log('Got into a server')
|
||||
//console.log(`The server name is ${guild.name}`)
|
||||
//console.log(`The server description is ${guild.description}`)
|
||||
//console.log(`The server snowflake is ${guild.id}`)
|
||||
logger.info(`Got into a server, ${guild.name}, ${guild.id}, ${guild.description}`)
|
||||
|
||||
sqlutil.registerServerIfMissing(guild.id, guild.name, guild.description).then(server_added => {
|
||||
if(server_added) {
|
||||
console.log(`Server Added ${guild.name}`)
|
||||
//console.log(`Server Added ${guild.name}`)
|
||||
logger.info(`Server Added ${guild.name}`)
|
||||
} else {
|
||||
console.log(`Server failed to add ${guild.name}`)
|
||||
//console.log(`Server failed to add ${guild.name}`)
|
||||
logger.error(`Server failed to add ${guild.name}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
|
||||
console.log("Voice State Update Fired")
|
||||
//console.log("Voice State Update Fired")
|
||||
logger.info("Voice State Update Fired")
|
||||
|
||||
if (oldState.channel == null && newState.channel != null) {
|
||||
if (newState.member.id == client.user.id) {
|
||||
return //If the user is breadbot, ignore and exit
|
||||
}
|
||||
|
||||
console.log(`\tChannel Join Detected ${newState.guild.id} - ${newState.channelId} - ${newState.member.id}`)
|
||||
//console.log(`\tChannel Join Detected ${newState.guild.id} - ${newState.channelId} - ${newState.member.id}`)
|
||||
logger.info(`Channel Join Detected ${newState.guild.id} - ${newState.channelId} - ${newState.member.id}`)
|
||||
|
||||
var existingCallID = await sqlutil.inCall(newState.guild.id, newState.channelId)
|
||||
|
||||
console.log(`\tExisting call ID ${existingCallID}`)
|
||||
//console.log(`\tExisting call ID ${existingCallID}`)
|
||||
logger.info(`Existing call ID ${existingCallID}`)
|
||||
|
||||
if (existingCallID == -1) {
|
||||
console.log("\tJoining a call")
|
||||
//console.log("\tJoining a call")
|
||||
logger.info("Joining a call")
|
||||
|
||||
var newCallID = await sqlutil.registerNewCall(newState.guild.id, newState.channelId, new Date())
|
||||
existingCallID = newCallID // To ensure all the stuff that happens after call creation works
|
||||
|
||||
console.log(`\tNext call ID ${newCallID}`)
|
||||
//console.log(`\tNext call ID ${newCallID}`)
|
||||
logger.info(`Next call ID ${newCallID}`)
|
||||
|
||||
// This should always have something to do, as all callIDs should be unique
|
||||
fs.mkdirSync(media_voice_folder + path.sep + newCallID, {recursive: true})
|
||||
@ -141,10 +173,12 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
|
||||
})
|
||||
|
||||
receiver.speaking.on("end", (user_id) => {
|
||||
console.log(`User ${user_id} stopped speaking`)
|
||||
//console.log(`User ${user_id} stopped speaking`)
|
||||
logger.info(`User ${user_id} stopped speaking`)
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn(error)
|
||||
logger.error(error)
|
||||
//console.warn(error)
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,21 +188,25 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
|
||||
var markedUserInCall = await sqlutil.registerUserInCall(existingCallID, newState.member.id)
|
||||
|
||||
if (!markedUserInCall) {
|
||||
console.log(`Something went wrong when marking user in voice call: ${newState.member.id} - ${newState.channelId}`)
|
||||
//console.log(`Something went wrong when marking user in voice call: ${newState.member.id} - ${newState.channelId}`)
|
||||
logger.error(`Something went wrong when marking user in voice call: ${newState.member.id} - ${newState.channelId}`)
|
||||
}
|
||||
} else {
|
||||
console.log(`Something went wrong when registering user for call: ${newState.member.id} - ${newState.member.username}`)
|
||||
//console.log(`Something went wrong when registering user for call: ${newState.member.id} - ${newState.member.username}`)
|
||||
logger.error(`Something went wrong when registering user for call: ${newState.member.id} - ${newState.member.username}`)
|
||||
}
|
||||
} else if (oldState.channel != null && newState.channel == null) {
|
||||
if (oldState.member.id == client.user.id) {
|
||||
return //If the user is breadbot, ignore and exit
|
||||
}
|
||||
|
||||
console.log(`Channel Exit Detected ${oldState.guild.id} - ${oldState.channelId} - ${oldState.member.id}`)
|
||||
//console.log(`Channel Exit Detected ${oldState.guild.id} - ${oldState.channelId} - ${oldState.member.id}`)
|
||||
logger.info(`Channel Exit Detected ${oldState.guild.id} - ${oldState.channelId} - ${oldState.member.id}`)
|
||||
|
||||
var existingCallID = await sqlutil.inCall(oldState.guild.id, oldState.channelId)
|
||||
|
||||
console.log(`Existing call ID: ${existingCallID}`)
|
||||
//console.log(`Existing call ID: ${existingCallID}`)
|
||||
logger.info(`Existing call ID ${existingCallID}`)
|
||||
|
||||
if (existingCallID != -1) {
|
||||
await sqlutil.deregisterUserInCall(existingCallID, oldState.member.id)
|
||||
@ -182,28 +220,34 @@ client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
|
||||
var didUpdateEndTime = await sqlutil.updateCallEndTime(existingCallID, new Date())
|
||||
|
||||
if (!didUpdateEndTime) {
|
||||
console.log(`Failed to mark call id ${existingCallID} as ended with an end date`)
|
||||
//console.log(`Failed to mark call id ${existingCallID} as ended with an end date`)
|
||||
logger.error(`Failed to mark call ID ${existingCallID} as ended with an end date`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("Couldn't find a call ID based on the guild and channel info, was Breadbot in the call?")
|
||||
//console.log("Couldn't find a call ID based on the guild and channel info, was Breadbot in the call?")
|
||||
logger.error("Couldn't find a call ID based on the guild and channel info, was Breadbot in the call?")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
client.on(Events.MessageCreate, async message => {
|
||||
console.log("Message Create Fired")
|
||||
//console.log("Message Create Fired")
|
||||
console.info("Message Create Fired")
|
||||
|
||||
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}`)
|
||||
//console.log(`Channel OK? ${channel_ok}`)
|
||||
//console.log(`User OK? ${user_ok}`)
|
||||
|
||||
logger.info(`Channel Ok? ${channel_ok} User OK? ${user_ok}`)
|
||||
|
||||
if (channel_ok && user_ok) {
|
||||
await sqlutil.registerMessage(message.id, message.channelId, message.author.id, message.content, message.createdAt).then(async message_add => {
|
||||
if(message_add) {
|
||||
console.log("Message Added")
|
||||
//console.log("Message Added")
|
||||
logger.info("Message Added")
|
||||
|
||||
if (message.attachments.size != 0) {
|
||||
const all_attachments = message.attachments.map(attachment => sqlutil.registerAttachmentIfMissing(
|
||||
@ -217,20 +261,25 @@ client.on(Events.MessageCreate, async message => {
|
||||
))
|
||||
|
||||
await Promise.all(all_attachments).catch((error) => {
|
||||
console.log(error)
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
console.log("Failed to log message")
|
||||
//console.log("Failed to log message")
|
||||
logger.error("Failed to log message")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
client.on(Events.MessageUpdate, async (oldMessage, newMessage) => {
|
||||
console.log("Message Update Fired")
|
||||
console.log(`Old Message Snowflake: ${oldMessage.id}`)
|
||||
console.log(`New Message Snowflake: ${newMessage.id}`)
|
||||
//console.log("Message Update Fired")
|
||||
//console.log(`Old Message Snowflake: ${oldMessage.id}`)
|
||||
//console.log(`New Message Snowflake: ${newMessage.id}`)
|
||||
|
||||
logger.info("Message Update Fired")
|
||||
logger.info(`Old Message Snowflake: ${oldMessage.id} New Message Snowflake: ${newMessage.id}`)
|
||||
|
||||
var editTime = newMessage.editedAt
|
||||
|
||||
@ -252,7 +301,7 @@ client.on(Events.MessageUpdate, async (oldMessage, newMessage) => {
|
||||
))
|
||||
|
||||
await Promise.all(all_attachments).catch((error) => {
|
||||
console.log(error)
|
||||
logger.error(error)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -264,7 +313,8 @@ client.on(Events.MessageDelete, async deletedMessage => {
|
||||
})
|
||||
|
||||
client.once(Events.ClientReady, c => {
|
||||
console.log(`Ready! Logged in as ${c.user.tag} - ${c.user.id}`);
|
||||
//console.log(`Ready! Logged in as ${c.user.tag} - ${c.user.id}`);
|
||||
logger.info(`Ready! Logged in as ${c.user.tag} - ${c.user.id}`)
|
||||
});
|
||||
|
||||
client.login(token);
|
@ -55,7 +55,6 @@ async function registerServerIfMissing(server_snowflake, server_name, server_des
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
|
||||
return false
|
||||
@ -64,19 +63,16 @@ async function registerServerIfMissing(server_snowflake, server_name, server_des
|
||||
|
||||
async function registerChannelIfMissing(channel_snowflake, server_snowflake, channel_name) {
|
||||
return connection_pool.query("SELECT * FROM channels WHERE channel_snowflake = ?;", [channel_snowflake]).then(async ([rows, fields]) => {
|
||||
if (rows.length != 0) {
|
||||
//console.log("Channel Already Registered")
|
||||
if (rows.length != 0) {+
|
||||
logger.info("Channel already registered")
|
||||
return true
|
||||
} else {
|
||||
//console.log("Channel Not Registered, registering")
|
||||
logger.info("Channel Not registered, registering")
|
||||
return await connection_pool.query("INSERT INTO channels VALUES (?, ?, ?)", [channel_snowflake, server_snowflake, channel_name]).then(([rows, fields]) => {
|
||||
return true
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -86,7 +82,6 @@ async function updateMessageContentIfPresent(message_snowflake, message_content,
|
||||
return connection_pool.query("SELECT message_snowflake FROM messages WHERE message_snowflake = ?", [message_snowflake]).then(async ([rows, fields]) => {
|
||||
if (rows.length == 0) {
|
||||
logger.info("Message specified doesn't exist, probably created before breadbot was here")
|
||||
//console.log("Message specified doesn't exist, probably created before breadbot was here")
|
||||
return false
|
||||
} else {
|
||||
return await connection_pool.query(
|
||||
@ -99,7 +94,6 @@ async function updateMessageContentIfPresent(message_snowflake, message_content,
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -108,7 +102,6 @@ async function updateMessageContentIfPresent(message_snowflake, message_content,
|
||||
async function markMessageDeletedIfPresent(message_snowflake) {
|
||||
return connection_pool.query("SELECT message_snowflake FROM messages WHERE message_snowflake = ?", [message_snowflake]).then(async ([rows, fields]) => {
|
||||
if (rows.length == 0) {
|
||||
//console.log("Message specified doesn't exists, probably created before breadbot was here")
|
||||
logger.info("Message specified doesn't exists, probably created before breadbot was here")
|
||||
return false
|
||||
} else {
|
||||
@ -119,7 +112,6 @@ async function markMessageDeletedIfPresent(message_snowflake) {
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -128,7 +120,6 @@ async function markMessageDeletedIfPresent(message_snowflake) {
|
||||
async function registerAttachmentIfMissing(attachment_snowflake, message_snowflake, attachment_name, attachment_description, attachment_timestamp, attachment_mime_type, attachment_url) {
|
||||
return connection_pool.query("SELECT attachment_snowflake FROM message_attachments WHERE attachment_snowflake = ?", [attachment_snowflake]).then(async ([rows, fields]) => {
|
||||
if (rows.length != 0) {
|
||||
//console.log("Attachment already exists")
|
||||
logger.info("Attachment alreaedy exists")
|
||||
return true
|
||||
} else {
|
||||
@ -141,7 +132,6 @@ async function registerAttachmentIfMissing(attachment_snowflake, message_snowfla
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -157,7 +147,6 @@ async function registerUserIfMissing(user_snowflake, user_name, user_displayname
|
||||
})
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -167,23 +156,6 @@ async function registerMessage(message_snowflake, channel_snowflake, user_snowfl
|
||||
return connection_pool.query("INSERT INTO messages VALUES (?, ?, ?, ?, ?, 0)", [message_snowflake, channel_snowflake, user_snowflake, message_content, message_timestamp]).then(([rows, fields]) => {
|
||||
return true
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -197,7 +169,6 @@ async function inCall(server_snowflake, channel_snowflake) {
|
||||
return rows[0].call_id
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return -1;
|
||||
})
|
||||
@ -211,7 +182,6 @@ async function registerNewCall(server_snowflake, channel_snowflake, call_start_t
|
||||
return rows.insertId
|
||||
}
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return -1;
|
||||
})
|
||||
@ -221,7 +191,6 @@ async function registerUserInCall(call_id, user_snowflake) {
|
||||
return connection_pool.query("INSERT INTO call_users (call_id, user_snowflake) VALUES (?, ?)", [call_id, user_snowflake]).then(([rows, fields]) => {
|
||||
return true
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -231,7 +200,6 @@ async function deregisterUserInCall(call_id, user_snowflake) {
|
||||
return connection_pool.query("DELETE FROM call_users WHERE call_id = ? AND user_snowflake = ?", [call_id, user_snowflake]).then(([rows, field]) => {
|
||||
return true
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
@ -241,7 +209,6 @@ async function getNumberUsersInCall(call_id) {
|
||||
return connection_pool.query("SELECT COUNT(call_users_id) AS users_in_call FROM call_users WHERE call_id = ?", [call_id]).then(([rows, fields]) => {
|
||||
return rows[0].users_in_call
|
||||
}).catch((error) => {
|
||||
//console.log(error)
|
||||
logger.error(error)
|
||||
return -1
|
||||
})
|
||||
@ -251,7 +218,6 @@ 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)
|
||||
logger.error(error)
|
||||
return false;
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user