diff --git a/src/index.js b/src/index.js index aeb2b8e..fc6672d 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ client.on('ready', (c) => { console.log('The Swog is prepared.'); }) //Chat commands are handled here -client.on('interactionCreate', (interaction) => { +client.on('interactionCreate', async(interaction) => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === 'croak') { @@ -37,6 +37,7 @@ client.on('interactionCreate', (interaction) => { const employee = interaction.options.get('employee').value; if ( addRemove == 'add') { AddGear(gearType, employee) + interaction.reply(`Added a gear to ${employee}`) }; } @@ -63,18 +64,17 @@ client.on('interactionCreate', (interaction) => { if (interaction.commandName === 'scoreboard') { const updateOrDisplay = interaction.options.get('update-display').value; if (updateOrDisplay == 'display') { - db.each("SELECT name, rank FROM Employees", (err, character) => { - if (err != null) { - console.log(err.message); - } - - console.log("Character Name: " + character.name + " rank: " + character.rank); - }); + const gearchannel = client.channels.cache.get('1306669146804715693'); - const scoreBoardMessage = BuildScoreboard(); + let scoreBoardMessage = await BuildScoreboard(); console.log("Scoreboard goes here: " + scoreBoardMessage); - //gearchannel.send(scoreBoardMessage); - interaction.reply("Displaying Scoreboard"); + if (scoreBoardMessage != "") { + gearchannel.send(scoreBoardMessage); + interaction.reply("Displaying Scoreboard"); + } + else { + console.log("Something went wrong when building the scoreboard."); + }; }; }; @@ -88,31 +88,39 @@ function DeleteEmployee(Name) { }; function PromoteEmployee(Name, rank) { db.run(`UPDATE Employees SET rank = \'${rank}\' WHERE name = \'${Name}\'`) -} +}; function AddGear(Type,Employee) { - + if (Type != "coppergear" | "silvergear" | "goldgear") return + db.run(`UPDATE Employees SET ${Type} = ${Type} + 1 WHERE name = \'${Employee}\' `) }; function BuildScoreboard() { - let messageContent = "" - db.each('SELECT name, rank, coppergear, silvergear, goldgear, purplegear FROM Employees', (err, character) => { - if (err != null) { - console.log(err.message); - } + return new Promise((resolve) => { + let messageContent = ""; + db.each('SELECT name, rank, coppergear, silvergear, goldgear, purplegear FROM Employees', (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:" + }; + + let silverGearString = ""; + console.log(character.silvergear); + for (let i = 0; i < character.silvergear; i++) { + silvergearstring += ":gear:" + } - let coppergearstring = ""; - for (let i = 0; i < character.coppergear; i++) { - coppergearstring += ":coppergear:" - } - - let silvergearstring = ""; - - messageContent += `**${character.name}** \n ## ${coppergearstring} \n`; - - + let goldGearString = ""; + messageContent += `**${character.name}** \n ##${copperGearString} \n`; + + }, () => {resolve(messageContent); }); }); - return messageContent; -} + +}; client.login(process.env.DISCORD_TOKEN); \ No newline at end of file