TravBot-v3/src/commands/fun/poll.ts

59 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-04-12 09:07:04 +00:00
import {MessageEmbed, Message, User} from "discord.js";
import {NamedCommand, RestCommand, poll, CHANNEL_TYPE, SendFunction, Command} from "onion-lasers";
2021-04-12 09:07:04 +00:00
import {pluralise} from "../../lib";
2020-08-17 17:38:15 +00:00
export default new NamedCommand({
2020-12-15 01:44:28 +00:00
description: "Create a poll.",
2021-04-12 09:07:04 +00:00
usage: "(<seconds>) <question>",
2020-12-15 01:44:28 +00:00
run: "Please provide a question.",
2021-04-12 09:07:04 +00:00
channelType: CHANNEL_TYPE.GUILD,
number: new Command({
run: "Please provide a question in addition to the provided duration.",
any: new RestCommand({
description: "Question for the poll.",
async run({send, message, author, args, combined}) {
2021-05-06 13:30:51 +00:00
execPoll(send, message, author, combined, args[0] * 1000);
2021-04-12 09:07:04 +00:00
}
})
}),
any: new RestCommand({
2020-12-15 01:44:28 +00:00
description: "Question for the poll.",
2021-04-12 09:07:04 +00:00
async run({send, message, author, combined}) {
execPoll(send, message, author, combined);
2020-12-15 01:44:28 +00:00
}
})
2020-10-15 09:23:24 +00:00
});
2021-04-12 09:07:04 +00:00
const AGREE = "✅";
const DISAGREE = "⛔";
async function execPoll(send: SendFunction, message: Message, user: User, question: string, duration = 60000) {
const icon =
user.avatarURL({
dynamic: true,
size: 2048
}) || user.defaultAvatarURL;
const msg = await send(
new MessageEmbed()
.setAuthor(`Poll created by ${message.author.username}`, icon)
.setColor(0xffffff)
.setFooter("React to vote.")
.setDescription(question)
);
const results = await poll(msg, [AGREE, DISAGREE], duration);
send(
new MessageEmbed()
.setAuthor(`The results of ${message.author.username}'s poll:`, icon)
.setTitle(question)
.setDescription(
`${AGREE} ${pluralise(
results[AGREE],
"",
"people who agree",
"person who agrees"
)}\n${DISAGREE} ${pluralise(results[DISAGREE], "", "people who disagree", "person who disagrees")}`
)
);
msg.delete();
}