Skip to content

Commit

Permalink
Enhancing the vote commands (#115)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ProgramPhoenix and ProgramPhoenix authored Oct 27, 2023
1 parent d74d9a6 commit 7c94dd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
14 changes: 10 additions & 4 deletions modules/vote/voteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -78,11 +83,12 @@ export default {
msg += "This vote ends <t:" + Math.ceil(Date.now()/1000 + time) + ":R> \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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -162,7 +168,7 @@ async function printVotes(pro: Set<string>, con: Set<string>, reply: Message, ti
let downVotes = Array.from(con).map(e => {
return "🔴 <@" + e + ">";
}).join("\n");

upVotes = (upVotes == "") ? "None" : upVotes;
downVotes = (downVotes == "") ? "None" : downVotes;

Expand Down

0 comments on commit 7c94dd0

Please sign in to comment.