Fix employeeCard crash when fed non-existent entry.

This commit is contained in:
Alexandertp 2024-11-20 16:04:47 +01:00
parent 819ca2fd52
commit eee43d2976
2 changed files with 87 additions and 58 deletions

View File

@ -1,5 +1,5 @@
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import {Client, IntentsBitField, EmbedBuilder} from 'discord.js'; import {Client, IntentsBitField, EmbedBuilder, MessageManager} from 'discord.js';
import sqlite3 from 'sqlite3'; import sqlite3 from 'sqlite3';
const db = new sqlite3.Database("./db/employeescoreboard.db", sqlite3.OPEN_READWRITE,(err) => { const db = new sqlite3.Database("./db/employeescoreboard.db", sqlite3.OPEN_READWRITE,(err) => {
@ -10,6 +10,7 @@ const db = new sqlite3.Database("./db/employeescoreboard.db", sqlite3.OPEN_READW
dotenv.config(); dotenv.config();
const client = new Client({ const client = new Client({
intents: [ intents: [
IntentsBitField.Flags.Guilds, IntentsBitField.Flags.Guilds,
@ -19,6 +20,7 @@ const client = new Client({
] ]
}); });
client
client.on('ready', (c) => { client.on('ready', (c) => {
console.log('The Swog is prepared.'); console.log('The Swog is prepared.');
@ -29,13 +31,18 @@ client.on('interactionCreate', async(interaction) => {
let isManager = await interaction.member.roles.cache.some(role => role.name === 'Manager'); let isManager = await interaction.member.roles.cache.some(role => role.name === 'Manager');
if (interaction.commandName === 'croak') { if (interaction.commandName === 'croak') {
interaction.reply('Croak!'); interaction.reply('Croak!');
} };
if (interaction.commandName == 'companyid') { if (interaction.commandName == 'companyid') {
let employeeCard = await EmployeeCardEmbed(interaction.options.get('employee').value); let employeeCard = await EmployeeCardEmbed(interaction.options.get('employee').value);
interaction.reply({embeds: [employeeCard]}); if (typeof employeeCard === 'string') {
} interaction.reply(employeeCard);
}
else {
interaction.reply({embeds: [employeeCard]});
}
};
if (interaction.commandName === 'gear' && isManager) { if (interaction.commandName === 'gear' && isManager) {
const addRemove = interaction.options.get('addremove').value; const addRemove = interaction.options.get('addremove').value;
@ -59,6 +66,7 @@ client.on('interactionCreate', async(interaction) => {
} }
if (hireFirePromote == 'fire') { if (hireFirePromote == 'fire') {
DeleteEmployee(employeeName); DeleteEmployee(employeeName);
interaction.reply(`Fired ${employeeName}`)
} }
} else if (interaction.commandName === 'employee' && !isManager) { } else if (interaction.commandName === 'employee' && !isManager) {
interaction.reply("You aren't a manager! Get back to work.") interaction.reply("You aren't a manager! Get back to work.")
@ -101,16 +109,24 @@ client.on('interactionCreate', async(interaction) => {
}; };
}; };
if (updateOrDisplay == 'update') { if (updateOrDisplay == 'update') {
const messagesInGearchannel = await gearchannel.messages.fetch()
const gearMessage = await messagesInGearchannel.find(msg => msg.author.id === '1306647733490290809')
if (gearMessage === undefined) {
interaction.reply("Could not find a message to update, did you remember to display it first?");
}
else {
gearMessage.edit(scoreBoardMessage);
interaction.reply("Updated scoreboard.")
}
} }
} else if (interaction.commandName === 'scoreboard' && !isManager) { } else if (interaction.commandName === 'scoreboard' && !isManager) {
interaction.reply("You aren't a manager! Get back to work.") interaction.reply("You aren't a manager! Get back to work.")
}; };
}); });
function CreateEmployee(Name) { function CreateEmployee(Name) {
db.run(`INSERT INTO Employees (name) VALUES (\'${Name}\')`); db.run(`INSERT INTO Employees (name, coppergear) VALUES (\'${Name}\', 0)`);
}; };
function DeleteEmployee(Name) { function DeleteEmployee(Name) {
@ -122,95 +138,112 @@ function PromoteEmployee(Name, rank) {
}; };
function AddGear(Type,Employee) { function AddGear(Type,Employee) {
if (Type == "copper" || Type == "silver" || Type == "gold" || Type == "purple"){
Type += "gear";
}
if (Type != "coppergear" && Type != "silvergear" && Type != "goldgear" && Type != "purplegear") return if (Type != "coppergear" && Type != "silvergear" && Type != "goldgear" && Type != "purplegear") return
db.run(`UPDATE Employees SET ${Type} = ${Type} + 1 WHERE name = \'${Employee}\' `) db.run(`UPDATE Employees SET ${Type} = ${Type} + 1 WHERE name = \'${Employee}\' `)
}; };
function RemoveGear(Type, Employee) { function RemoveGear(Type, Employee) {
if (Type != "coppergear" && Type != "silvergear" && Type != "goldgear" && Type != "purplegear") return if (Type != "coppergear" && Type != "silvergear" && Type != "goldgear" && Type != "purplegear") return
db.run(`UPDATE Employees SET ${Type} = ${Type} - 1 WHERE name = \'${Employee}\'`) db.run(`UPDATE Employees SET ${Type} = ${Type} - 1 WHERE name = \'${Employee}\'`)
} };
function RenameEmployee(oldName, newName) { function RenameEmployee(oldName, newName) {
db.run(`UPDATE Employees SET name = \'${newName}\' WHERE name = \'${oldName}\'`); db.run(`UPDATE Employees SET name = \'${newName}\' WHERE name = \'${oldName}\'`);
} };
function EmployeeCardEmbed(employeeName) { function EmployeeCardEmbed(employeeName) {
return new Promise((resolve) => { return new Promise((resolve) => {
db.get(`SELECT name, rank, coppergear, silvergear, purplegear, copperswog, silverswog, goldswog, purpleswog FROM Employees WHERE name = \'${employeeName}\'`, (err, character) => { db.get(`SELECT name, rank, coppergear, silvergear, goldgear, purplegear, copperswog, silverswog, goldswog, purpleswog FROM Employees WHERE name = ?`,[employeeName], (err, character) => {
if (err != null) {console.log(err.message)}; if (err != null) {
console.log(err.message)
let copperGearString = " "; resolve("Something went wrong when fetching employee info");
console.log(character.coppergear);
for (let i = 0; i < character.coppergear; i++) {
copperGearString += "<:coppergear:1308166341622235179>";
}; };
if(character === undefined) {
console.log("No data returned")
}
let silverGearString = " "; if (character !== undefined){
console.log(character.silvergear); let copperGearString = " ";
for (let i = 0; i < character.silvergear; i++) { console.log(character.coppergear);
silverGearString += ":gear:"; for (let i = 0; i < character.coppergear; i++) {
}; copperGearString += "<:coppergear:1308166341622235179>";
};
let silverGearString = " ";
console.log(character.silvergear);
for (let i = 0; i < character.silvergear; i++) {
silverGearString += ":gear:";
};
let goldGearString = " ";
console.log(character.goldgear);
for(let i = 0; i < character.goldgear; i++) {
goldGearString += "<:goldgear:1308166365550481499>";
};
let purplegearString =" ";
console.log(character.purplegear);
for(let i = 0; i< character.purplegear; i++) {
purplegearString += "<:purplegear:1308166392864051250>";
};
const employeeCard = new EmbedBuilder()
.setColor(0x388E3C)
.setTitle(employeeName)
.setDescription(character.rank)
.addFields(
{name:`Copper`, value: copperGearString, inline: true},
{name:'Silver', value: silverGearString, inline: true},
{name:'Purple', value: purplegearString, inline: true},
)
.addFields(
{name:'Gold', value: goldGearString, inline: true},
)
.setFooter({text: 'ID Issued by Gearbeast Inc. Do not copy.'});
resolve(employeeCard);
}
else {
resolve("Something went wrong when fetching employee information");
}
let goldGearString = " ";
console.log(character.goldgear);
for(let i = 0; i < character.goldgear; i++) {
goldGearString += "<:goldgear:1308166365550481499>";
};
let purplegearString =" ";
console.log(character.purplegear);
for(let i = 0; i< character.purplegear; i++) {
purplegearString += "<:purplegear:1308166392864051250>";
};
const employeeCard = new EmbedBuilder()
.setColor(0x388E3C)
.setTitle(employeeName)
.setDescription(character.rank)
.addFields(
{name:`Copper`, value: copperGearString, inline: true},
{name:'Silver', value: silverGearString, inline: true},
{name:'Purple', value: purplegearString, inline: true},
)
.addFields(
{name:'Gold', value: goldGearString, inline: true},
)
.setFooter({text: 'ID Issued by Gearbeast Inc. Do not copy.'});
resolve(employeeCard);
}); });
}) })
} };
function BuildScoreboard(rank) { function BuildScoreboard(rank) {
return new Promise((resolve) => { return new Promise((resolve) => {
let messageContent = `# ${rank} \n`; let messageContent = `# ${rank} \n`;
db.each(`SELECT name, rank, coppergear, silvergear, goldgear, purplegear FROM Employees WHERE rank = \'${rank}\'`, (err, character) => { db.each(`SELECT name, rank, coppergear, silvergear, goldgear, purplegear FROM Employees WHERE rank = ?`, [rank], (err, character) => {
if (err != null) { if (err != null) {
console.log(err.message); console.log(err.message);
}; };
let copperGearString = ""; let copperGearString = "";
console.log(character.coppergear); //console.log(character.coppergear);
if (character.coppergear >0) {copperGearString += "\n## "} if (character.coppergear >0) {copperGearString += "\n## "}
for (let i = 0; i < character.coppergear; i++) { for (let i = 0; i < character.coppergear; i++) {
copperGearString += "<:coppergear:1308166341622235179>"; copperGearString += "<:coppergear:1308166341622235179>";
}; };
let silverGearString = ""; let silverGearString = "";
console.log(character.silvergear); //console.log(character.silvergear);
if (character.silvergear > 0) {silverGearString += "\n## "} if (character.silvergear > 0) {silverGearString += "\n## "}
for (let i = 0; i < character.silvergear; i++) { for (let i = 0; i < character.silvergear; i++) {
silverGearString += ":gear:"; silverGearString += ":gear:";
}; };
let goldGearString = ""; let goldGearString = "";
console.log(character.goldgear); //console.log(character.goldgear);
if(character.goldgear > 0) {goldGearString += "\n## "}; if(character.goldgear > 0) {goldGearString += "\n## "};
for(let i = 0; i < character.goldgear; i++) { for(let i = 0; i < character.goldgear; i++) {
goldGearString += "<:goldgear:1308166365550481499>"; goldGearString += "<:goldgear:1308166365550481499>";
}; };
let purplegearString =""; let purplegearString ="";
console.log(character.purplegear); //console.log(character.purplegear);
if(character.purplegear > 0) {purplegearString += "\n## "}; if(character.purplegear > 0) {purplegearString += "\n## "};
for(let i = 0; i< character.purplegear; i++) { for(let i = 0; i< character.purplegear; i++) {
purplegearString += "<:purplegear:1308166392864051250>"; purplegearString += "<:purplegear:1308166392864051250>";
@ -219,11 +252,6 @@ function BuildScoreboard(rank) {
messageContent += `**${character.name}** ${goldGearString} ${purplegearString} ${silverGearString} ${copperGearString} \n`; messageContent += `**${character.name}** ${goldGearString} ${purplegearString} ${silverGearString} ${copperGearString} \n`;
}, () => {resolve(messageContent); }); }, () => {resolve(messageContent); });
}); });
}; };
function intToGearString(coppergearInt, silvergearInt, goldgearInt, copperswogInt, silverswogInt, goldswogInt) {
}
client.login(process.env.DISCORD_TOKEN); client.login(process.env.DISCORD_TOKEN);

View File

@ -96,6 +96,7 @@ const commands = [
name: 'new-name', name: 'new-name',
description: 'The new name of the employee', description: 'The new name of the employee',
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
required: true,
} }
] ]
}, },