1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 20:30:58 -05:00
Daggerbot-TS/src/commands/poll.ts

66 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-09-04 10:21:04 -04:00
import Discord from 'discord.js';
import TClient from '../client.js';
import {writeFileSync, existsSync, mkdirSync} from 'node:fs';
export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
if (client.config.mainServer.id === interaction.guildId) {
if (!interaction.member.roles.cache.has(client.config.mainServer.roles.mpmod) && !interaction.member.roles.cache.has(client.config.mainServer.roles.bottech)) return client.youNeedRole(interaction, 'mpmod');
}
const channelId = '1084864116776251463'; // #mp-announcements
({
start: async()=>{
const map_names = interaction.options.getString('map_names', true).split('|');
if (map_names.length > 10) return interaction.reply('You can only have up to 10 maps in a poll!');
const msg = await (interaction.guild.channels.cache.get(channelId) as Discord.TextChannel).send({embeds: [
new client.embed()
.setColor(client.config.embedColor)
.setTitle('Voting for next map!')
.setDescription(map_names.map((map,i)=>`${i+1}. **${map}**`).join('\n'))
.setFooter({text: `Poll started by ${interaction.user.tag}`, iconURL: interaction.member.displayAvatarURL({extension: 'webp', size: 1024})})
]});
await interaction.reply(`Successfully created a poll in <#${channelId}>`)
const numbers = ['1⃣','2⃣','3⃣','4⃣','5⃣','6⃣','7⃣','8⃣','9⃣','🔟'];
for (let i = 0; i < map_names.length; i++) await msg.react(numbers[i])
},
end: async()=>{
const msg = await (interaction.guild.channels.cache.get(channelId) as Discord.TextChannel).messages.fetch(interaction.options.getString('message_id', true));
if (!msg) return interaction.reply('Message not found, please make sure you have the correct message ID.');
if (msg.embeds[0].title !== 'Voting for next map!') return interaction.reply('This message is not a poll!');
if (msg.embeds[0].footer?.text?.startsWith('Poll ended by')) return interaction.reply('This poll has already ended!');
if (msg.reactions.cache.size < 2) return interaction.reply('This poll has not been voted on yet!');
if (!existsSync('src/database/polls')) mkdirSync('src/database/polls');
writeFileSync(`src/database/polls/pollResults-${msg.id}.json`, JSON.stringify({
map_names: msg.embeds[0].description.split('\n').map(x=>x.slice(3)),
votes: msg.reactions.cache.map(x=>x.count)
}));
(client.channels.cache.get('516344221452599306') as Discord.TextChannel).send({files: [`src/database/polls/pollResults-${msg.id}.json`]});
msg.edit({embeds: [new client.embed().setColor(client.config.embedColor).setTitle('Voting has ended!').setDescription('The next map will be '+msg.embeds[0].description.split('\n')[msg.reactions.cache.map(x=>x.count).indexOf(Math.max(...msg.reactions.cache.map(x=>x.count)))].slice(3)).setFooter({text: `Poll ended by ${interaction.user.tag}`, iconURL: interaction.member.displayAvatarURL({extension: 'webp', size: 1024})})]});
await interaction.reply(`Successfully ended the [poll](<https://discord.com/channels/${interaction.guildId}/${channelId}/${msg.id}>) in <#${channelId}>`)
}
})[interaction.options.getSubcommand()]();
},
data: new Discord.SlashCommandBuilder()
.setName('poll')
.setDescription('Poll system for FSMP server')
.addSubcommand(x=>x
.setName('start')
.setDescription('Start a poll')
.addStringOption(x=>x
.setName('map_names')
.setDescription('Map names separated by |\'s, up to 10 maps')
.setRequired(true)))
.addSubcommand(x=>x
.setName('end')
.setDescription('End a poll')
.addStringOption(x=>x
.setName('message_id')
.setDescription('Message ID of the poll')
.setRequired(true)))
}