Fix employeeCard crash when fed non-existent entry.
This commit is contained in:
parent
819ca2fd52
commit
eee43d2976
138
src/index.js
138
src/index.js
@ -1,5 +1,5 @@
|
||||
import dotenv from 'dotenv';
|
||||
import {Client, IntentsBitField, EmbedBuilder} from 'discord.js';
|
||||
import {Client, IntentsBitField, EmbedBuilder, MessageManager} from 'discord.js';
|
||||
import sqlite3 from 'sqlite3';
|
||||
|
||||
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();
|
||||
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
IntentsBitField.Flags.Guilds,
|
||||
@ -19,6 +20,7 @@ const client = new Client({
|
||||
]
|
||||
});
|
||||
|
||||
client
|
||||
|
||||
client.on('ready', (c) => {
|
||||
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');
|
||||
if (interaction.commandName === 'croak') {
|
||||
interaction.reply('Croak!');
|
||||
}
|
||||
};
|
||||
|
||||
if (interaction.commandName == 'companyid') {
|
||||
|
||||
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) {
|
||||
const addRemove = interaction.options.get('addremove').value;
|
||||
@ -59,6 +66,7 @@ client.on('interactionCreate', async(interaction) => {
|
||||
}
|
||||
if (hireFirePromote == 'fire') {
|
||||
DeleteEmployee(employeeName);
|
||||
interaction.reply(`Fired ${employeeName}`)
|
||||
}
|
||||
} else if (interaction.commandName === 'employee' && !isManager) {
|
||||
interaction.reply("You aren't a manager! Get back to work.")
|
||||
@ -101,16 +109,24 @@ client.on('interactionCreate', async(interaction) => {
|
||||
};
|
||||
};
|
||||
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) {
|
||||
interaction.reply("You aren't a manager! Get back to work.")
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
function CreateEmployee(Name) {
|
||||
db.run(`INSERT INTO Employees (name) VALUES (\'${Name}\')`);
|
||||
db.run(`INSERT INTO Employees (name, coppergear) VALUES (\'${Name}\', 0)`);
|
||||
};
|
||||
|
||||
function DeleteEmployee(Name) {
|
||||
@ -122,95 +138,112 @@ function PromoteEmployee(Name, rank) {
|
||||
};
|
||||
|
||||
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
|
||||
db.run(`UPDATE Employees SET ${Type} = ${Type} + 1 WHERE name = \'${Employee}\' `)
|
||||
};
|
||||
function RemoveGear(Type, Employee) {
|
||||
if (Type != "coppergear" && Type != "silvergear" && Type != "goldgear" && Type != "purplegear") return
|
||||
db.run(`UPDATE Employees SET ${Type} = ${Type} - 1 WHERE name = \'${Employee}\'`)
|
||||
}
|
||||
};
|
||||
function RenameEmployee(oldName, newName) {
|
||||
db.run(`UPDATE Employees SET name = \'${newName}\' WHERE name = \'${oldName}\'`);
|
||||
}
|
||||
};
|
||||
|
||||
function EmployeeCardEmbed(employeeName) {
|
||||
return new Promise((resolve) => {
|
||||
db.get(`SELECT name, rank, coppergear, silvergear, purplegear, copperswog, silverswog, goldswog, purpleswog FROM Employees WHERE name = \'${employeeName}\'`, (err, character) => {
|
||||
if (err != null) {console.log(err.message)};
|
||||
|
||||
let copperGearString = " ";
|
||||
console.log(character.coppergear);
|
||||
for (let i = 0; i < character.coppergear; i++) {
|
||||
copperGearString += "<:coppergear:1308166341622235179>";
|
||||
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)
|
||||
resolve("Something went wrong when fetching employee info");
|
||||
};
|
||||
if(character === undefined) {
|
||||
console.log("No data returned")
|
||||
}
|
||||
|
||||
let silverGearString = " ";
|
||||
console.log(character.silvergear);
|
||||
for (let i = 0; i < character.silvergear; i++) {
|
||||
silverGearString += ":gear:";
|
||||
};
|
||||
if (character !== undefined){
|
||||
let copperGearString = " ";
|
||||
console.log(character.coppergear);
|
||||
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) {
|
||||
return new Promise((resolve) => {
|
||||
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) {
|
||||
console.log(err.message);
|
||||
};
|
||||
|
||||
let copperGearString = "";
|
||||
console.log(character.coppergear);
|
||||
//console.log(character.coppergear);
|
||||
if (character.coppergear >0) {copperGearString += "\n## "}
|
||||
for (let i = 0; i < character.coppergear; i++) {
|
||||
copperGearString += "<:coppergear:1308166341622235179>";
|
||||
};
|
||||
|
||||
let silverGearString = "";
|
||||
console.log(character.silvergear);
|
||||
//console.log(character.silvergear);
|
||||
if (character.silvergear > 0) {silverGearString += "\n## "}
|
||||
for (let i = 0; i < character.silvergear; i++) {
|
||||
silverGearString += ":gear:";
|
||||
};
|
||||
|
||||
let goldGearString = "";
|
||||
console.log(character.goldgear);
|
||||
//console.log(character.goldgear);
|
||||
if(character.goldgear > 0) {goldGearString += "\n## "};
|
||||
for(let i = 0; i < character.goldgear; i++) {
|
||||
goldGearString += "<:goldgear:1308166365550481499>";
|
||||
};
|
||||
let purplegearString ="";
|
||||
console.log(character.purplegear);
|
||||
//console.log(character.purplegear);
|
||||
if(character.purplegear > 0) {purplegearString += "\n## "};
|
||||
for(let i = 0; i< character.purplegear; i++) {
|
||||
purplegearString += "<:purplegear:1308166392864051250>";
|
||||
@ -219,11 +252,6 @@ function BuildScoreboard(rank) {
|
||||
messageContent += `**${character.name}** ${goldGearString} ${purplegearString} ${silverGearString} ${copperGearString} \n`;
|
||||
}, () => {resolve(messageContent); });
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
function intToGearString(coppergearInt, silvergearInt, goldgearInt, copperswogInt, silverswogInt, goldswogInt) {
|
||||
|
||||
}
|
||||
|
||||
client.login(process.env.DISCORD_TOKEN);
|
@ -96,6 +96,7 @@ const commands = [
|
||||
name: 'new-name',
|
||||
description: 'The new name of the employee',
|
||||
type: ApplicationCommandOptionType.String,
|
||||
required: true,
|
||||
}
|
||||
]
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user