Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/commands/Fun/poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { SteveCommand } from '@lib/structures/commands/SteveCommand';
import { CommandOptions, KlasaMessage } from 'klasa';
import { Message, MessageReaction } from 'discord.js';
import { ApplyOptions } from '@skyra/decorators';
import { floatPromise } from '@utils/util';

@ApplyOptions<CommandOptions>({
aliases: ['vote'],
description: lang => lang.tget('commandPollDescription'),
extendedHelp: lang => lang.tget('commandPollExtended'),
usage: '<duration:timespan> <name:string> <choices:string> [...]'
})

export default class extends SteveCommand {

public async run(msg: KlasaMessage, [duration, name, ...choices]: [number, string, string]): Promise<Message> {
if (choices.length < 2) return msg.channel.send(msg.language.tget('commandPollTooFew'));
if (choices.length > 10) return msg.channel.send(msg.language.tget('commandPollTooMany'));

// return msg.channel.send(`duration: ${duration}\nname: ${name}\nchoices: ${choices.join(', ')}`);

const emotes = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟'].slice(0, choices.length);

let pollText = `**${name}**\n`;
choices.forEach((choice, option) => {
pollText += `${emotes[option]} ${choice}\n`;
});

const pollMsg = await msg.channel.send(pollText);

emotes.forEach(emote => {
floatPromise(this, pollMsg.react(emote));
});

return pollMsg.awaitReactions(
(reaction: MessageReaction) => emotes.includes(reaction.emoji.name),
{ time: duration }
).then(reactions => {
let maxVotes = 0;
reactions.forEach(reaction => {
if (reaction.users.cache.size > maxVotes) {
maxVotes = reaction.users.cache.size;
}
});

if (maxVotes <= 1) return pollMsg.reply(msg.language.tget('commandPollFail'));

const winners = reactions
.filter(reaction => reaction.users.cache.size >= maxVotes)
.map(reaction => choices[emotes.indexOf(reaction.emoji.name)]);

// Replies here are for when djs 13 switches this function to using inline replies
floatPromise(this, pollMsg.edit(`${pollText}${msg.language.tget('commandPollEnd', winners, maxVotes - 1)}`));
return pollMsg.reply(msg.language.tget('commandPollEnd', winners, maxVotes - 1));
});
}

}
13 changes: 13 additions & 0 deletions src/languages/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,19 @@ export default class extends Language {
extendedHelp: 'The ability to search for a relevant XKCD is coming soon!'
}),
commandXkcdInvalid: 'I was unable to find that XKCD.',
commandPollDescription: `Have ${botName} create a poll for you`,
commandPollExtended: builder.display('poll', {
examples: [
'Is butt legs?|Butt is legs|Butt is butt'
],
extendedHelp: 'This command requires you to have at least two options and no more than ten.'
}),
commandPollTooFew: 'I need more than one choice to create a poll.',
commandPollTooMany: 'I can\'t create a poll with more than ten choices.',
commandPollEnd: (options, votes) => options.length === 1
? `${options} won the poll with ${votes} vote${votes === 1 ? '' : 's'}.`
: `${options.join(' & ')} tied the poll with ${votes} vote${votes === 1 ? '' : 's'} each.`,
commandPollFail: 'It looks like no one voted in the poll.',
/**
* ################################
* # MODERATION SYSTEM #
Expand Down
6 changes: 6 additions & 0 deletions src/lib/types/Languages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ declare module 'klasa' {
commandXkcdDescription: string;
commandXkcdExtended: string;
commandXkcdInvalid: string;
commandPollDescription: string;
commandPollExtended: string;
commandPollTooFew: string;
commandPollTooMany: string;
commandPollEnd: (options: string[], votes: number) => string;
commandPollFail: string;
moderationNoDuration: string;
moderationNoReason: string;
moderationNoSteve: string;
Expand Down