2022-01-16 17:37:07 +00:00
|
|
|
|
import {MessageEmbed, Message, User, MessageActionRow, MessageButton} from "discord.js";
|
2021-04-13 12:38:52 +00:00
|
|
|
|
import {NamedCommand, RestCommand, poll, CHANNEL_TYPE, SendFunction, Command} from "onion-lasers";
|
2022-01-16 17:37:07 +00:00
|
|
|
|
import {SlashCommandBuilder} from "@discordjs/builders";
|
|
|
|
|
import {CommandInteraction} from "discord.js";
|
|
|
|
|
import {pluralise, parseDuration} from "../../lib";
|
|
|
|
|
|
|
|
|
|
export const header = new SlashCommandBuilder()
|
|
|
|
|
.setDescription("Create a poll.")
|
|
|
|
|
.addStringOption((option) => option.setName("question").setDescription("Question for the poll").setRequired(true))
|
|
|
|
|
.addIntegerOption((option) =>
|
|
|
|
|
option.setName("duration").setDescription("Duration of the poll in seconds.").setRequired(false)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export async function handler(interaction: CommandInteraction) {
|
|
|
|
|
const {options} = interaction;
|
|
|
|
|
const question = options.getString("question", true);
|
|
|
|
|
var duration = options.getInteger("duration", false);
|
|
|
|
|
duration = parseDuration(duration + "s"); //override the duration variable with miliseconds one
|
|
|
|
|
execSlashPoll(interaction, question, duration || 60000);
|
|
|
|
|
}
|
2020-08-17 17:38:15 +00:00
|
|
|
|
|
2021-04-05 04:35:12 +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
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}),
|
2021-04-10 17:07:55 +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 = "⛔";
|
|
|
|
|
|
2022-01-16 17:37:07 +00:00
|
|
|
|
async function execSlashPoll(interaction: CommandInteraction, question: string, duration = 60000) {
|
|
|
|
|
const responseButtons = new MessageActionRow().addComponents(
|
|
|
|
|
new MessageButton().setCustomId("agree").setLabel(AGREE).setStyle("SUCCESS"),
|
|
|
|
|
new MessageButton().setCustomId("disagree").setLabel(DISAGREE).setStyle("DANGER")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const icon =
|
|
|
|
|
interaction.user.avatarURL({
|
|
|
|
|
dynamic: true,
|
|
|
|
|
size: 2048
|
|
|
|
|
}) || interaction.user.defaultAvatarURL;
|
|
|
|
|
const embed = new MessageEmbed()
|
|
|
|
|
.setAuthor(`Poll created by ${interaction.user.username}`, icon)
|
|
|
|
|
.setColor(0xffffff)
|
|
|
|
|
.setFooter("Click the buttons to vote.")
|
|
|
|
|
.setDescription(question)
|
|
|
|
|
.addField(`${AGREE} -`, `${pluralise(0, "", "people have voted")}\n`)
|
|
|
|
|
.addField(`${DISAGREE} -`, `${pluralise(0, "", "people have voted")}\n`);
|
|
|
|
|
const msg = await interaction.reply({
|
|
|
|
|
embeds: [embed],
|
|
|
|
|
components: [responseButtons]
|
|
|
|
|
});
|
|
|
|
|
var idsArray: string[] = [];
|
|
|
|
|
const collector = interaction.channel?.createMessageComponentCollector({time: duration});
|
|
|
|
|
collector?.on("collect", async (i) => {
|
|
|
|
|
if (i.customId === "agree") {
|
|
|
|
|
if (idsArray.includes(i.user.id)) {
|
|
|
|
|
i.reply({content: "You have already voted!", ephemeral: true});
|
|
|
|
|
} else {
|
|
|
|
|
idsArray.push(i.user.id);
|
|
|
|
|
var agree = +1;
|
|
|
|
|
embed.fields[0].value = `${pluralise(agree, "", "people who agree", "person who agrees")}\n`;
|
|
|
|
|
interaction.editReply({embeds: [embed]});
|
|
|
|
|
i.reply({content: "You picked ✅!", ephemeral: true});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i.customId === "disagree") {
|
|
|
|
|
if (idsArray.includes(i.user.id)) {
|
|
|
|
|
i.reply({content: "You have already voted!", ephemeral: true});
|
|
|
|
|
} else {
|
|
|
|
|
idsArray.push(i.user.id);
|
|
|
|
|
var disagree = +1;
|
|
|
|
|
embed.fields[1].value = `${pluralise(disagree, "", "people who disagree", "person who disagrees")}\n`;
|
|
|
|
|
interaction.editReply({embeds: [embed]});
|
|
|
|
|
i.reply({content: "You picked ⛔!", ephemeral: true});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
//This solution looks messy but works really well and stops from stuff like vote fraud happening.
|
|
|
|
|
//I'm not sure if it's the best solution but if you have a better idea then please let me know.
|
|
|
|
|
|
|
|
|
|
collector?.on("end", async (collected) => {
|
|
|
|
|
embed.setTitle(`The results of ${interaction.user.username}'s poll:`);
|
|
|
|
|
interaction.editReply({embeds: [embed]});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 09:07:04 +00:00
|
|
|
|
async function execPoll(send: SendFunction, message: Message, user: User, question: string, duration = 60000) {
|
|
|
|
|
const icon =
|
|
|
|
|
user.avatarURL({
|
|
|
|
|
dynamic: true,
|
|
|
|
|
size: 2048
|
|
|
|
|
}) || user.defaultAvatarURL;
|
2021-10-29 12:52:46 +00:00
|
|
|
|
const msg = await send({
|
|
|
|
|
embeds: [
|
|
|
|
|
new MessageEmbed()
|
|
|
|
|
.setAuthor(`Poll created by ${message.author.username}`, icon)
|
|
|
|
|
.setColor(0xffffff)
|
|
|
|
|
.setFooter("React to vote.")
|
|
|
|
|
.setDescription(question)
|
|
|
|
|
]
|
|
|
|
|
});
|
2021-04-12 09:07:04 +00:00
|
|
|
|
const results = await poll(msg, [AGREE, DISAGREE], duration);
|
2021-10-29 12:52:46 +00:00
|
|
|
|
send({
|
|
|
|
|
embeds: [
|
|
|
|
|
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"
|
|
|
|
|
)}`
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
});
|
2021-04-12 09:07:04 +00:00
|
|
|
|
msg.delete();
|
|
|
|
|
}
|