├── demo.png ├── .vscode └── settings.json ├── package.json ├── LICENSE ├── .gitignore ├── README.md ├── .eslintrc.json └── index.js /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saanuregh/discord.js-poll-embed/HEAD/demo.png -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "eslint.enable": true, 3 | "eslint.alwaysShowStatus": true, 4 | "prettier.singleQuote": true, 5 | "prettier.useTabs": true, 6 | "npm.enableScriptExplorer": true, 7 | "formatContextMenu.closeAfterSave": true, 8 | "prettier.printWidth": 120, 9 | "prettier.tabWidth": 4, 10 | "prettier.eslintIntegration": true, 11 | "spellright.language": [ 12 | "en" 13 | ], 14 | "spellright.documentTypes": [ 15 | "markdown", 16 | "latex", 17 | "plaintext" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "discord.js-poll-embed", 3 | "version": "1.0.2", 4 | "description": "A simple utility to create polls with just embeds and emoji reactions.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/saanuregh/discord.js-poll-embed.git" 12 | }, 13 | "keywords": [ 14 | "discord", 15 | "discord.js", 16 | "poll" 17 | ], 18 | "author": "saanuregh", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/saanuregh/discord.js-poll-embed/issues" 22 | }, 23 | "homepage": "https://github.com/saanuregh/discord.js-poll-embed#readme" 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Saanu Reghunadh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
7 | 8 | 9 | # discord.js-poll-embed 10 | A simple utility to create polls with just embeds and emoji reactions. No poll API required, just Discord.js reactions. By default supports following functionalities, 11 | * By default supports 10 poll options (can be increased). 12 | * Timed poll. 13 | * Force closing poll. 14 | * Custom emojis for voting (Required for bypassing default options limit) 15 | 16 | Supports only discord.js@^12.0.0 (master). 17 | # Installation 18 | * `npm install discord.js-poll-embed` 19 | 20 | # Usage 21 | __Basic Bot Example__ 22 | ```js 23 | // Import the discord.js-pagination package 24 | const pollEmbed = require('discord.js-poll-embed'); 25 | 26 | // Call the pollEmbed method, first three arguments are required 27 | // title is the poll title 28 | // options is an array of strings, which contains the poll options 29 | // timeout is the time in seconds for which users can vote for. 0 makes it infinite and default value is 30 seconds 30 | // emojiList is the list of emojis used for voting. Defaults to 10 simple digit emojis. Which also limits the no of options you can give by default to 10. While using custom emojis be careful that discord doesnt support some emojis. 31 | // forceEndPollEmoji is the emoji which can be voted by the poll author to force close voting. Default value is a green check box. 32 | pollEmbed(msg, title, options, timeout, emojiList, forceEndPollEmoji); 33 | // There you go, now you have poll embeds 34 | ``` 35 | # Preview 36 |  37 | Here is a preview. 38 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint:recommended", 3 | "env": { 4 | "node": true, 5 | "es6": true 6 | }, 7 | "parserOptions": { 8 | "ecmaVersion": 2017 9 | }, 10 | "rules": { 11 | "brace-style": [ 12 | "error", 13 | "1tbs", 14 | { 15 | "allowSingleLine": true 16 | } 17 | ], 18 | "comma-dangle": [ 19 | "error", 20 | "never" 21 | ], 22 | "comma-spacing": "error", 23 | "comma-style": "error", 24 | "curly": [ 25 | "error", 26 | "multi-line", 27 | "consistent" 28 | ], 29 | "dot-location": [ 30 | "error", 31 | "property" 32 | ], 33 | "handle-callback-err": "off", 34 | "indent": [ 35 | "error", 36 | "tab", 37 | { 38 | "SwitchCase": 1 39 | } 40 | ], 41 | "max-nested-callbacks": [ 42 | "error", 43 | { 44 | "max": 4 45 | } 46 | ], 47 | "max-statements-per-line": [ 48 | "error", 49 | { 50 | "max": 2 51 | } 52 | ], 53 | "no-console": "off", 54 | "no-empty-function": "error", 55 | "no-floating-decimal": "error", 56 | "no-inline-comments": "error", 57 | "no-lonely-if": "error", 58 | "no-multi-spaces": "error", 59 | "no-multiple-empty-lines": [ 60 | "error", 61 | { 62 | "max": 2, 63 | "maxEOF": 1, 64 | "maxBOF": 0 65 | } 66 | ], 67 | "no-shadow": [ 68 | "error", 69 | { 70 | "allow": [ 71 | "err", 72 | "resolve", 73 | "reject" 74 | ] 75 | } 76 | ], 77 | "no-trailing-spaces": [ 78 | "error" 79 | ], 80 | "no-var": "error", 81 | "object-curly-spacing": [ 82 | "error", 83 | "always" 84 | ], 85 | "prefer-const": "error", 86 | "quotes": [ 87 | "error", 88 | "single" 89 | ], 90 | "semi": [ 91 | "error", 92 | "always" 93 | ], 94 | "space-before-blocks": "error", 95 | "space-before-function-paren": [ 96 | "error", 97 | { 98 | "anonymous": "never", 99 | "named": "never", 100 | "asyncArrow": "always" 101 | } 102 | ], 103 | "space-in-parens": "error", 104 | "space-infix-ops": "error", 105 | "space-unary-ops": "error", 106 | "spaced-comment": "error", 107 | "yoda": "error" 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { MessageEmbed } = require('discord.js'); 2 | 3 | const defEmojiList = [ 4 | '\u0031\u20E3', 5 | '\u0032\u20E3', 6 | '\u0033\u20E3', 7 | '\u0034\u20E3', 8 | '\u0035\u20E3', 9 | '\u0036\u20E3', 10 | '\u0037\u20E3', 11 | '\u0038\u20E3', 12 | '\u0039\u20E3', 13 | '\uD83D\uDD1F' 14 | ]; 15 | 16 | const pollEmbed = async (msg, title, options, timeout = 30, emojiList = defEmojiList.slice(), forceEndPollEmoji = '\u2705') => { 17 | if (!msg && !msg.channel) return msg.reply('Channel is inaccessible.'); 18 | if (!title) return msg.reply('Poll title is not given.'); 19 | if (!options) return msg.reply('Poll options are not given.'); 20 | if (options.length < 2) return msg.reply('Please provide more than one choice.'); 21 | if (options.length > emojiList.length) return msg.reply(`Please provide ${emojiList.length} or less choices.`); 22 | 23 | let text = `*To vote, react using the correspoding emoji.\nThe voting will end in **${timeout} seconds**.\nPoll creater can end the poll **forcefully** by reacting to ${forceEndPollEmoji} emoji.*\n\n`; 24 | const emojiInfo = {}; 25 | for (const option of options) { 26 | const emoji = emojiList.splice(0, 1); 27 | emojiInfo[emoji] = { option: option, votes: 0 }; 28 | text += `${emoji} : \`${option}\`\n\n`; 29 | } 30 | const usedEmojis = Object.keys(emojiInfo); 31 | usedEmojis.push(forceEndPollEmoji); 32 | 33 | const poll = await msg.channel.send(embedBuilder(title, msg.author.tag).setDescription(text)); 34 | for (const emoji of usedEmojis) await poll.react(emoji); 35 | 36 | const reactionCollector = poll.createReactionCollector( 37 | (reaction, user) => usedEmojis.includes(reaction.emoji.name) && !user.bot, 38 | timeout === 0 ? {} : { time: timeout * 1000 } 39 | ); 40 | const voterInfo = new Map(); 41 | reactionCollector.on('collect', (reaction, user) => { 42 | if (usedEmojis.includes(reaction.emoji.name)) { 43 | if (reaction.emoji.name === forceEndPollEmoji && msg.author.id === user.id) return reactionCollector.stop(); 44 | if (!voterInfo.has(user.id)) voterInfo.set(user.id, { emoji: reaction.emoji.name }); 45 | const votedEmoji = voterInfo.get(user.id).emoji; 46 | if (votedEmoji !== reaction.emoji.name) { 47 | const lastVote = poll.reactions.get(votedEmoji); 48 | lastVote.count -= 1; 49 | lastVote.users.remove(user.id); 50 | emojiInfo[votedEmoji].votes -= 1; 51 | voterInfo.set(user.id, { emoji: reaction.emoji.name }); 52 | } 53 | emojiInfo[reaction.emoji.name].votes += 1; 54 | } 55 | }); 56 | 57 | reactionCollector.on('dispose', (reaction, user) => { 58 | if (usedEmojis.includes(reaction.emoji.name)) { 59 | voterInfo.delete(user.id); 60 | emojiInfo[reaction.emoji.name].votes -= 1; 61 | } 62 | }); 63 | 64 | reactionCollector.on('end', () => { 65 | text = '*Ding! Ding! Ding! Time\'s up!\n Results are in,*\n\n'; 66 | for (const emoji in emojiInfo) text += `\`${emojiInfo[emoji].option}\` - \`${emojiInfo[emoji].votes}\`\n\n`; 67 | poll.delete(); 68 | msg.channel.send(embedBuilder(title, msg.author.tag).setDescription(text)); 69 | }); 70 | }; 71 | 72 | const embedBuilder = (title, author) => { 73 | return new MessageEmbed() 74 | .setTitle(`Poll - ${title}`) 75 | .setFooter(`Poll created by ${author}`); 76 | }; 77 | 78 | module.exports = pollEmbed; 79 | --------------------------------------------------------------------------------