Do better logging lol

This commit is contained in:
Christopher Cookman 2023-04-26 11:38:58 -06:00
parent 7fafccd8fb
commit 14213bff37
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -212,21 +212,30 @@ const rest = new REST({
version: '10' version: '10'
}).setToken(config.discord.token); }).setToken(config.discord.token);
var logChannel; var logChannel;
dcClient.on('ready', () => { var sendLog;
console.log(`${colors.cyan("[INFO]")} Logged in as ${dcClient.user.tag}!`);
logChannel = dcClient.channels.cache.get(config.discord.logId); dcClient.on('ready', async () => {
await dcClient.channels.fetch(config.discord.logId).then((channel) => {
sendLog = (message) => {
channel.send(`\`\`\`ansi\n${message}\`\`\``);
console.log(message);
};
sendLog(`${colors.cyan("[INFO]")} Logged in as ${dcClient.user.tag}!`);
// Set up application commands // Set up application commands
const commands = require('./commands.json'); const commands = require('./commands.json');
(async () => { (async () => {
try { try {
console.log(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`); sendLog(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`);
await rest.put( await rest.put(
Routes.applicationGuildCommands(dcClient.user.id, config.discord.guildId), { Routes.applicationGuildCommands(dcClient.user.id, config.discord.guildId), {
body: commands body: commands
} }
); );
console.log(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`); sendLog(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`);
} catch (error) { } catch (error) {
console.error(`${colors.red("[ERROR]")} ${error}`); console.error(`${colors.red("[ERROR]")} ${error}`);
} }
@ -242,7 +251,7 @@ dcClient.on('ready', () => {
status: "online" status: "online"
}); });
}).catch((error) => { }).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`); sendLog(`${colors.red("[ERROR]")} ${error}`);
}); });
// Run every 5 minutes // Run every 5 minutes
@ -256,7 +265,7 @@ dcClient.on('ready', () => {
status: "online" status: "online"
}); });
}).catch((error) => { }).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`); sendLog(`${colors.red("[ERROR]")} ${error}`);
}); });
}, 300000); }, 300000);
@ -276,18 +285,11 @@ dcClient.on('ready', () => {
// They're in the server, do nothing // They're in the server, do nothing
}).catch((error) => { }).catch((error) => {
// They're not in the server, delete the extension // They're not in the server, delete the extension
console.log(`${colors.cyan("[INFO]")} ${extension.user.extension} is not in the server, deleting it`); sendLog(`${colors.cyan("[INFO]")} ${extension.user.extension} is not in the server, deleting it`);
deleteExtension(extension.user.extension).then((result) => { deleteExtension(extension.user.extension).then((result) => {
console.log(`${colors.cyan("[INFO]")} Deleted extension ${extension.user.extension} because the user is no longer in the server`); sendLog(`${colors.cyan("[INFO]")} Deleted extension ${extension.user.extension} because the user is no longer in the server`);
logChannel.send({
embeds: [{
title: "Extension Deleted",
description: `${member} (${member.id}) left the server, so their extension (${extension.user.extension}) was deleted`,
color: 0xff0000
}]
});
}).catch((error) => { }).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`); sendLog(`${colors.red("[ERROR]")} ${error}`);
}); });
}); });
@ -398,27 +400,22 @@ dcClient.on('ready', () => {
}) })
}); });
});
dcClient.on("guildMemberRemove", (member) => { dcClient.on("guildMemberRemove", (member) => {
// Delete the extension if the user leaves the server // Delete the extension if the user leaves the server
console.log(`${colors.cyan("[INFO]")} User ${member.id} left the server`) sendLog(`${colors.cyan("[INFO]")} User ${member.id} left the server`)
lookupExtension(member.id, "uid").then((result) => { lookupExtension(member.id, "uid").then((result) => {
if (result.status == "exists") { if (result.status == "exists") {
console.log(`${colors.cyan("[INFO]")} User ${member.id} has extension ${result.result.fetchVoiceMail.extension}, deleting it`) sendLog(`${colors.cyan("[INFO]")} User ${member.id} has extension ${result.result.fetchVoiceMail.extension}, deleting it`)
deleteExtension(result.result.fetchVoiceMail.extension).then((result) => { deleteExtension(result.result.fetchVoiceMail.extension).then((result) => {
console.log(`${colors.cyan("[INFO]")} Deleted extension ${result.result.fetchVoiceMail.extension} because the user left the server`); sendLog(`${colors.cyan("[INFO]")} Deleted extension ${result.result.fetchVoiceMail.extension} because the user left the server`);
logChannel.send({
embeds: [{
title: "Extension Deleted",
description: `${member} (${member.id}) left the server, so their extension (${extension.user.extension}) was deleted`,
color: 0xff0000
}]
});
}).catch((error) => { }).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`); sendLog(`${colors.red("[ERROR]")} ${error}`);
}); });
} }
}).catch((error) => { }).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`); sendLog(`${colors.red("[ERROR]")} ${error}`);
}); });
}); });
@ -468,13 +465,7 @@ dcClient.on('interactionCreate', async interaction => {
] ]
}] }]
}) })
logChannel.send({ sendLog(`${colors.cyan("[INFO]")} Created extension ${ext} for user ${uid}`);
embeds: [{
title: "Extension Created",
description: `${interaction.user} (${interaction.user.id}) created extension ${ext}`,
color: 0x00ff00
}]
});
// Add the role to the user on Discord based on the ID in the config file // Add the role to the user on Discord based on the ID in the config file
let role = interaction.guild.roles.cache.find(role => role.id === config.discord.roleId); let role = interaction.guild.roles.cache.find(role => role.id === config.discord.roleId);
interaction.member.roles.add(role); interaction.member.roles.add(role);
@ -516,7 +507,7 @@ dcClient.on('interactionCreate', async interaction => {
} }
}).catch((error) => { }).catch((error) => {
// The user doesn't have an extension, create one // The user doesn't have an extension, create one
console.log(`${colors.red("[ERROR]")} ${error}`) sendLog(`${colors.red("[ERROR]")} ${error}`)
interaction.editReply({ interaction.editReply({
content: "You don't have an extension!", content: "You don't have an extension!",
ephemeral: true ephemeral: true
@ -571,13 +562,7 @@ dcClient.on('interactionCreate', async interaction => {
content: "Extension Deleted!", content: "Extension Deleted!",
ephemeral: true ephemeral: true
}); });
logChannel.send({ sendLog(`${colors.green("[INFO]")} ${interaction.user.tag} (${interaction.user.id}) deleted extension ${result.result.fetchExtension.user.extension}`)
embeds: [{
title: "Extension Deleted",
description: `${member} (${member.id}) chose to delete their extension, ${result.result.fetchExtension.user.extension}`,
color: 0xff0000
}]
});
// Remove the role from the user on Discord based on the ID in the config file // Remove the role from the user on Discord based on the ID in the config file
let role = interaction.guild.roles.cache.find(role => role.id === config.discord.roleId); let role = interaction.guild.roles.cache.find(role => role.id === config.discord.roleId);
interaction.member.roles.remove(role); interaction.member.roles.remove(role);