From 4e0a225426aae511434a4e571fe235d44d38dfd3 Mon Sep 17 00:00:00 2001 From: Matheus Lemos Date: Fri, 2 Oct 2020 10:49:29 -0300 Subject: [PATCH 1/2] Added poll command Added command to start polls, given the poll title and its options. --- src/commands/util/index.ts | 2 ++ src/commands/util/poll.ts | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/commands/util/poll.ts diff --git a/src/commands/util/index.ts b/src/commands/util/index.ts index c294349..3b72b38 100644 --- a/src/commands/util/index.ts +++ b/src/commands/util/index.ts @@ -3,11 +3,13 @@ import { CommandClient } from 'eris' import evaluate from './evaluate' import kill from './kill' import status from './status' +import poll from './poll' export const init = (bot: CommandClient): void => { evaluate.init(bot) kill.init(bot) status.init(bot) + poll.init(bot) } export default { diff --git a/src/commands/util/poll.ts b/src/commands/util/poll.ts new file mode 100644 index 0000000..4cab115 --- /dev/null +++ b/src/commands/util/poll.ts @@ -0,0 +1,53 @@ +import { CommandClient, Message } from 'eris' + +interface Field { + name: string, + value: any, + inline: boolean +} + +// Starts a new poll +// Poll structure: !poll name of poll; option 1, emoji 1 ; option 2, emoji 2 +export const init = (bot: CommandClient): void => { + bot.registerCommand('poll', async (msg, args) => { + + // Parse arguments and extract poll title,author and options + const opts:string[] = args.join(' ').split(';') + if (opts.length < 3) { + return 'Invalid poll\nCommand: !poll "Poll name"; [optionX, reactionX]'; + } + + const pollName = 'Poll: ' + opts.shift().trim(); + const date = new Date() + const author = 'Created by ' + msg.author + ' in ' + date; + + // Create 'fields' array with poll options and reactions + const fields = [] + for (let i in opts) { + let res = {} + const parsed = opts[i].split(',') + const key = parsed[0].trim(); + const value = parsed[1].trim() + res = { + name: key, + value: value, + inline: false + }; + fields.push(res); + } + + msg.channel.createMessage({ + embed: { + title: pollName, + fields: fields, + footer: { + text: author + } + } + }); + }) +} + +export default { + init +} From 064e116f96fe826392118464d5db3dc009db803f Mon Sep 17 00:00:00 2001 From: Matheus Lemos Date: Sun, 4 Oct 2020 00:37:05 -0300 Subject: [PATCH 2/2] Added automatic reactions to poll Added automatic first reactions to poll made with nookbot --- src/commands/util/poll.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/commands/util/poll.ts b/src/commands/util/poll.ts index 4cab115..db7744b 100644 --- a/src/commands/util/poll.ts +++ b/src/commands/util/poll.ts @@ -22,12 +22,14 @@ export const init = (bot: CommandClient): void => { const author = 'Created by ' + msg.author + ' in ' + date; // Create 'fields' array with poll options and reactions - const fields = [] + const reactions : Array = [] + const fields : Array = [] for (let i in opts) { let res = {} const parsed = opts[i].split(',') const key = parsed[0].trim(); const value = parsed[1].trim() + reactions.push(value); res = { name: key, value: value, @@ -44,8 +46,12 @@ export const init = (bot: CommandClient): void => { text: author } } - }); - }) + }).then(embedMessage => { + for (let i in reactions) { + embedMessage.addReaction(reactions[i]); + } + }) + }); } export default {