Creating Polls
In Seyfert, the ability to create polls
is just around the corner!
This section will show the basic creation of polls, events and examples.
This is an extended section of the configuration file.
1module.exports = config.bot({2 //... other options3 intents: ['GuildMessagePolls', 'DirectMessagePolls'],4});
Receiving Events
Currently there are 2 events for polls:
messagePollVoteAdd
andmessagePollVoteRemove
.
1export default createEvent({2 data: { name: 'messagePollVoteAdd' },3 run: (data) => {4 //You can do whatever you want with the data5 console.log(`The user: ${data.userId} added a vote to the poll: ${data.messageId}`);6 },7});
1export default createEvent({2 data: { name: 'messagePollVoteRemove' },3 run: (data) => {4 //You can do whatever you want with the data5 console.log(`The user: ${data.userId} removed a vote to the poll: ${data.messageId}`);6 },7});
Ending Event
The way to check when a poll is ended is using the
messageUpdate
event.
Here is a quick example that will edit the message when a poll is completed:
1export default createEvent({2 data: { name: 'messageUpdate' },3 //This is [oldMessage, newMessage]4 //But in this example we only need newMessage5 run: ([, newMessage]) => {6 if (newMessage.poll?.results?.isFinalized) {7 console.log(`The poll with the id: ${newMessage.id} is ended`)8 }9 },10});
Commands
Quick examples of how to create a poll and how to end it.
It’s suppose you have the following directory structure:
- src
Directorycommands
Directorypoll
- start.command.ts
- end.command.ts
- parent.ts
- index.ts
- seyfert.config.js
- package.json
- tsconfig.json
1import { AutoLoad, Declare, Command } from 'seyfert';2
3@Declare({4 name: 'poll',5 description: 'Poll command!',6})7@AutoLoad()8export default class PollCommand extends Command {}
1import {2 type CommandContext,3 Declare,4 type OKFunction,5 Options,6 PollBuilder,7 SubCommand,8 createStringOption,9 createNumberOption,10} from 'seyfert';11import { MessageFlags } from 'seyfert/lib/types';12
13const options = {14 question: createStringOption({15 description: 'Poll question.',16 required: true,17 }),18 answers: createStringOption({19 description: 'Poll answers separated by commas.',20 required: true,21 //This can be improved, but is a quick example22 value: ({ value }, ok: OKFunction<string[] | null>) => {23 //This is to have more options in the poll24 //Example: ganyu, supremacy25 const answers = value.split(', ');26 if (!answers.length) return ok(null);27
28 return ok(answers);29 },30 }),31 //The poll duration in hours.32 //Example: 1, 2, 5...33 hours: createNumberOption({34 description: 'The duration of the poll.'35 }),36};37
38@Declare({39 name: 'start',40 description: 'Create a new poll.',41})42@Options(options)43export default class StartPoll extends SubCommand {44 async run(ctx: CommandContext<typeof options>) {45 const { options, channel } = ctx;46 const { question, answers } = options;47
48 if (!channel?.isTextGuild()) return;49 if (!answers)50 return ctx.editOrReply({51 flags: MessageFlags.Ephemeral,52 content: 'Answers needs to be separed by commas!',53 });54
55 const hours = options.hours ?? 1;56
57 //We have a new Builder!58 //PollBuilder will make easier for us to create Polls.59 const newPoll = new PollBuilder()60 .allowMultiselect(true)61 .setQuestion({ text: question })62 .setAnswers(63 //The poll answers64 //With a maximum of 10 answers65 answers.map((text) => ({66 text,67 //You can add emojis too!68 emoji: '🐐'69 }))70 )71 .setDuration(hours);72
73 await channel.messages.write({74 content: 'New poll started.',75 poll: newPoll76 });77
78 await ctx.editOrReply({79 flags: MessageFlags.Ephemeral,80 content: `Poll started in the channel: ${channel.toString()}`,81 });82 }83}
1import {2 type CommandContext,3 Declare,4 Options,5 SubCommand,6 createStringOption7} from 'seyfert';8import { MessageFlags } from 'seyfert/lib/types';9
10const options = {11 message: createStringOption({12 description: 'The message id of the poll',13 required: true,14 }),15};16
17@Declare({18 name: 'end',19 description: 'End a poll.',20})21@Options(options)22export default class EndPoll extends SubCommand {23 async run(ctx: CommandContext<typeof options>) {24 const { options, client } = ctx;25 const { message: messageId } = options;26
27 await client.messages.endPoll(ctx.channelId, messageId);28 await ctx.editOrReply({29 flags: MessageFlags.Ephemeral,30 content: 'Poll ended.',31 });32 }33}