1 |
import { CommandInteraction, EmojiIdentifierResolvable, GuildMember, Message, TextChannel, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import MessageEmbed from '../../client/MessageEmbed'; |
5 |
import CommandOptions from '../../types/CommandOptions'; |
6 |
import InteractionOptions from '../../types/InteractionOptions'; |
7 |
import { fetchEmoji } from '../../utils/Emoji'; |
8 |
import Ballot from '../../models/Ballot'; |
9 |
|
10 |
export default class BallotCommand extends BaseCommand { |
11 |
supportsInteractions = true; |
12 |
|
13 |
constructor() { |
14 |
super('ballot', 'automation', []); |
15 |
} |
16 |
|
17 |
async ballotCreate(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
18 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
19 |
await msg.reply({ |
20 |
embeds: [ |
21 |
new MessageEmbed() |
22 |
.setColor('#f14a60') |
23 |
.setDescription(`This command requires at least one argument.`) |
24 |
] |
25 |
}); |
26 |
|
27 |
return; |
28 |
} |
29 |
|
30 |
if (msg instanceof CommandInteraction) |
31 |
await msg.deferReply({ |
32 |
ephemeral: true |
33 |
}); |
34 |
|
35 |
let content: string; |
36 |
let channel: TextChannel = msg.channel as TextChannel; |
37 |
let anonymous = false; |
38 |
|
39 |
if (options.isInteraction) { |
40 |
content = <string> await options.options.getString('content'); |
41 |
|
42 |
if (options.options.getChannel('channel')) |
43 |
channel = <TextChannel> await options.options.getChannel('channel'); |
44 |
|
45 |
if (options.options.getBoolean('anonymous')) |
46 |
anonymous = <boolean> await options.options.getBoolean('anonymous'); |
47 |
} |
48 |
else { |
49 |
if ((msg as Message).mentions.channels.last() && /\<#(\d+)\>/g.test([...options.args].pop()!)) { |
50 |
channel = <TextChannel> (msg as Message).mentions.channels.last(); |
51 |
options.args.pop(); |
52 |
} |
53 |
|
54 |
content = await options.args.join(' ').trim(); |
55 |
} |
56 |
|
57 |
if (!channel.send) { |
58 |
await this.deferReply(msg, { |
59 |
content: 'Invalid text channel.' |
60 |
}); |
61 |
|
62 |
return; |
63 |
} |
64 |
|
65 |
const message = await channel.send({ |
66 |
embeds: [ |
67 |
new MessageEmbed() |
68 |
.setAuthor({ |
69 |
name: anonymous ? 'Staff' : (msg.member?.user as User).tag!, |
70 |
iconURL: anonymous ? msg.guild!.iconURL()! : (msg.member as GuildMember)?.displayAvatarURL() |
71 |
}) |
72 |
.setDescription(content) |
73 |
.setTimestamp() |
74 |
] |
75 |
}); |
76 |
|
77 |
const ballot = new Ballot({ |
78 |
content, |
79 |
author: anonymous ? null : msg.member?.user.id, |
80 |
msg_id: message.id, |
81 |
guild_id: msg.guild!.id, |
82 |
date: new Date(), |
83 |
channel_id: msg.channel!.id |
84 |
}); |
85 |
|
86 |
await ballot.save(); |
87 |
|
88 |
// await client.db.runAsync('INSERT INTO ballots(content, author, msg_id, guild_id, date, channel_id) VALUES(?, ?, ?, ?, ?, ?)', [content, anonymous ? null : msg.member?.user.id, message.id, msg.guild!.id, new Date().toISOString(), msg.channel!.id]); |
89 |
// const ballot = await client.db.getAsync("SELECT * FROM ballots WHERE msg_id = ? AND guild_id = ? ORDER BY id DESC LIMIT 0, 1", [message.id, msg.guild!.id]); |
90 |
|
91 |
await message.react(<EmojiIdentifierResolvable> await fetchEmoji('check')); |
92 |
await message.react(<EmojiIdentifierResolvable> await fetchEmoji('error')); |
93 |
|
94 |
await this.deferReply(msg, { |
95 |
content: `${(await fetchEmoji('check'))!.toString()} Your message has been delivered. The ballot ID is ${ballot.get('id')}.`, |
96 |
}); |
97 |
} |
98 |
|
99 |
async ballotView(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
100 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
101 |
await msg.reply({ |
102 |
embeds: [ |
103 |
new MessageEmbed() |
104 |
.setColor('#f14a60') |
105 |
.setDescription(`This command requires at least one argument.`) |
106 |
] |
107 |
}); |
108 |
|
109 |
return; |
110 |
} |
111 |
|
112 |
try { |
113 |
const id = options.isInteraction ? options.options.getInteger('id') : options.args[0]; |
114 |
//const ballot = await client.db.getAsync("SELECT * FROM ballots WHERE id = ? ORDER BY id DESC LIMIT 0, 1", [id]); |
115 |
const ballot = (await Ballot.findOne({ |
116 |
where: { |
117 |
id |
118 |
} |
119 |
}))?.get(); |
120 |
|
121 |
if (!ballot) |
122 |
throw new Error(); |
123 |
|
124 |
const ballotAuthor = ballot.author ? (await msg.guild!.members.fetch(ballot.author)) : null; |
125 |
|
126 |
const channel = <TextChannel> await msg.guild?.channels.fetch(ballot.channel_id); |
127 |
|
128 |
if (!channel) |
129 |
throw new Error(); |
130 |
|
131 |
const message = await channel.messages.fetch(ballot.msg_id); |
132 |
|
133 |
if (!message) |
134 |
throw new Error(); |
135 |
|
136 |
const upEmote = await fetchEmoji('check'); |
137 |
const downEmote = await fetchEmoji('error'); |
138 |
const upVotes = message.reactions.cache.find(r => r.emoji.id === upEmote!.id)!.count - 1; |
139 |
const downVotes = message.reactions.cache.find(r => r.emoji.id === downEmote!.id)!.count - 1; |
140 |
|
141 |
await msg.reply({ |
142 |
embeds: [ |
143 |
new MessageEmbed() |
144 |
.setAuthor({ |
145 |
name: ballot.author ? ballotAuthor!.user!.tag! : 'Staff', |
146 |
iconURL: ballot.author ? ballotAuthor?.displayAvatarURL()! : msg.guild!.iconURL()! |
147 |
}) |
148 |
.setDescription(ballot.content) |
149 |
.addFields([ |
150 |
{ |
151 |
name: 'Upvotes', |
152 |
value: `${upVotes}` |
153 |
}, |
154 |
{ |
155 |
name: 'Downvotes', |
156 |
value: `${downVotes}` |
157 |
} |
158 |
]) |
159 |
.setTimestamp() |
160 |
] |
161 |
}); |
162 |
} |
163 |
catch (e) { |
164 |
console.log(e); |
165 |
|
166 |
await msg.reply({ |
167 |
embeds: [ |
168 |
new MessageEmbed() |
169 |
.setColor('#f14a60') |
170 |
.setDescription(`Invalid ID or failed to fetch data.`) |
171 |
] |
172 |
}); |
173 |
} |
174 |
} |
175 |
|
176 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
177 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
178 |
await msg.reply({ |
179 |
embeds: [ |
180 |
new MessageEmbed() |
181 |
.setColor('#f14a60') |
182 |
.setDescription(`This command requires at least one subcommand.`) |
183 |
] |
184 |
}); |
185 |
|
186 |
return; |
187 |
} |
188 |
|
189 |
const subcmd = options.isInteraction ? options.options.getSubcommand(true) : options.args[0]; |
190 |
|
191 |
if (!options.isInteraction) |
192 |
await options.args.shift(); |
193 |
|
194 |
if (subcmd === 'create') |
195 |
await this.ballotCreate(client, msg, options); |
196 |
else if (subcmd === 'view') |
197 |
await this.ballotView(client, msg, options); |
198 |
else { |
199 |
await msg.reply({ |
200 |
embeds: [ |
201 |
new MessageEmbed() |
202 |
.setColor('#f14a60') |
203 |
.setDescription(`Invalid subcommand provided.`) |
204 |
] |
205 |
}); |
206 |
} |
207 |
} |
208 |
} |