From 7c94dd095786dd4482793fe5ae07b9f4eba905b3 Mon Sep 17 00:00:00 2001 From: ujqlg <93595010+ProgramPhoenix@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:02:40 +0200 Subject: [PATCH] Enhancing the vote commands (#115) * added offline groupmembers to vote count * removed console.log added ; * majority now in pro or con needed * failsafe vote command * Fixed crash --------- Co-authored-by: ujqlg <93595010+MartinKrausewitz@users.noreply.github.com> --- index.ts | 3 ++- modules/vote/voteCommand.ts | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index b5c32a0..1dd641d 100644 --- a/index.ts +++ b/index.ts @@ -7,7 +7,8 @@ import {readConfig} from "./lib/configmanager"; const INTENTS = [ IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.Guilds, - IntentsBitField.Flags.GuildMessageReactions + IntentsBitField.Flags.GuildMessageReactions, + IntentsBitField.Flags.GuildMembers ]; const client = new Client({intents: INTENTS}); diff --git a/modules/vote/voteCommand.ts b/modules/vote/voteCommand.ts index aac29fd..bf3d172 100644 --- a/modules/vote/voteCommand.ts +++ b/modules/vote/voteCommand.ts @@ -50,7 +50,12 @@ export default { ] }, handler: async function(interaction: CommandInteraction) { - + try { + await interaction.channel.fetch(); + } catch { + await interaction.reply("Not in this channel! Move to a channel the bot has access to."); + return; + } // --- Variables let usedVotes = 0; let msg = ""; @@ -78,11 +83,12 @@ export default { msg += "This vote ends \n"; } else { // get online member + await maingroup.guild.members.fetch(); const groupmembers = maingroup.members.map(m=>m.user.id); usedVotes = Math.ceil(groupmembers.length/2); // further variables set usedVotes = ((usedVotes == 0) ? 1 : usedVotes); - msg += "This is a majority voting. " + usedVotes + " Votes required!"; + msg += "This is a majority voting. " + usedVotes + " Votes are required for one of the sides!"; } const embed = getEmbedOptions(title, msg, maingroup.id); @@ -126,7 +132,7 @@ export default { } interaction.reply({content:"Voting successful. You are against the topic!", ephemeral:true}); } - if (!timed && pro.size + con.size == usedVotes) { + if (!timed && (pro.size == usedVotes || con.size == usedVotes)) { reply.edit(getEmbedOptions(title, msg, maingroup.id, Math.ceil(Date.now()/1000 + 30))); setTimeout(() => { printVotes(pro, con, reply, title, collector); @@ -162,7 +168,7 @@ async function printVotes(pro: Set, con: Set, reply: Message, ti let downVotes = Array.from(con).map(e => { return "🔴 <@" + e + ">"; }).join("\n"); - + upVotes = (upVotes == "") ? "None" : upVotes; downVotes = (downVotes == "") ? "None" : downVotes;