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'
}).setToken(config.discord.token);
var logChannel;
dcClient.on('ready', () => {
console.log(`${colors.cyan("[INFO]")} Logged in as ${dcClient.user.tag}!`);
logChannel = dcClient.channels.cache.get(config.discord.logId);
var sendLog;
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
const commands = require('./commands.json');
(async () => {
try {
console.log(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`);
sendLog(`${colors.cyan("[INFO]")} Started refreshing application (/) commands.`);
await rest.put(
Routes.applicationGuildCommands(dcClient.user.id, config.discord.guildId), {
body: commands
}
);
console.log(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`);
sendLog(`${colors.cyan("[INFO]")} Successfully reloaded application (/) commands.`);
} catch (error) {
console.error(`${colors.red("[ERROR]")} ${error}`);
}
@ -242,7 +251,7 @@ dcClient.on('ready', () => {
status: "online"
});
}).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`);
sendLog(`${colors.red("[ERROR]")} ${error}`);
});
// Run every 5 minutes
@ -256,7 +265,7 @@ dcClient.on('ready', () => {
status: "online"
});
}).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`);
sendLog(`${colors.red("[ERROR]")} ${error}`);
});
}, 300000);
@ -267,7 +276,7 @@ dcClient.on('ready', () => {
let extensions = result.fetchAllExtensions.extension;
extensions.forEach((extension) => {
lookupExtension(extension.user.extension, "ext").then((result) => {
if(result.result.fetchVoiceMail.email == null) {
if (result.result.fetchVoiceMail.email == null) {
// Extension is not part of the bot, do nothing
return;
};
@ -276,18 +285,11 @@ dcClient.on('ready', () => {
// They're in the server, do nothing
}).catch((error) => {
// 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) => {
console.log(`${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
}]
});
sendLog(`${colors.cyan("[INFO]")} Deleted extension ${extension.user.extension} because the user is no longer in the server`);
}).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`);
sendLog(`${colors.red("[ERROR]")} ${error}`);
});
});
@ -396,29 +398,24 @@ dcClient.on('ready', () => {
})
}
})
});
});
dcClient.on("guildMemberRemove", (member) => {
// 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) => {
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) => {
console.log(`${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
}]
});
sendLog(`${colors.cyan("[INFO]")} Deleted extension ${result.result.fetchVoiceMail.extension} because the user left the server`);
}).catch((error) => {
console.log(`${colors.red("[ERROR]")} ${error}`);
sendLog(`${colors.red("[ERROR]")} ${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({
embeds: [{
title: "Extension Created",
description: `${interaction.user} (${interaction.user.id}) created extension ${ext}`,
color: 0x00ff00
}]
});
sendLog(`${colors.cyan("[INFO]")} Created extension ${ext} for user ${uid}`);
// 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);
interaction.member.roles.add(role);
@ -516,7 +507,7 @@ dcClient.on('interactionCreate', async interaction => {
}
}).catch((error) => {
// The user doesn't have an extension, create one
console.log(`${colors.red("[ERROR]")} ${error}`)
sendLog(`${colors.red("[ERROR]")} ${error}`)
interaction.editReply({
content: "You don't have an extension!",
ephemeral: true
@ -571,13 +562,7 @@ dcClient.on('interactionCreate', async interaction => {
content: "Extension Deleted!",
ephemeral: true
});
logChannel.send({
embeds: [{
title: "Extension Deleted",
description: `${member} (${member.id}) chose to delete their extension, ${result.result.fetchExtension.user.extension}`,
color: 0xff0000
}]
});
sendLog(`${colors.green("[INFO]")} ${interaction.user.tag} (${interaction.user.id}) deleted extension ${result.result.fetchExtension.user.extension}`)
// 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);
interaction.member.roles.remove(role);