forked from Galactic/galactic-bot
Refactor poll command
This commit is contained in:
parent
bc467e4dd9
commit
c5c304d5d9
@ -56,12 +56,19 @@ class PollCommand extends SlashCommand
|
||||
|
||||
async execute (invoker: InvokerWrapper<true>, { choices, channel, duration, multichoice, message }: CommandParams)
|
||||
{
|
||||
const { subcommand, author } = invoker;
|
||||
const guild = invoker.guild!;
|
||||
const member = invoker.member!;
|
||||
const { subcommand } = invoker;
|
||||
|
||||
if (subcommand!.name === 'create')
|
||||
return this.#createPoll(invoker, { choices, channel, duration, multichoice });
|
||||
else if (subcommand!.name === 'delete')
|
||||
return this.#deletePoll(invoker, { message });
|
||||
else if (subcommand!.name === 'end')
|
||||
return this.#endPoll(invoker, { message });
|
||||
}
|
||||
|
||||
async #createPoll (invoker: InvokerWrapper<true>, { choices, channel, duration, multichoice }: CommandParams)
|
||||
{
|
||||
const { guild, member, author } = invoker;
|
||||
const targetChannel = (channel?.asChannel || invoker.channel) as GuildTextBasedChannel;
|
||||
if (!targetChannel?.isTextBased())
|
||||
throw new CommandError(invoker, { index: 'ERR_INVALID_CHANNEL_TYPE' });
|
||||
@ -118,29 +125,7 @@ class PollCommand extends SlashCommand
|
||||
await this.client.polls.create(poll, guild.id);
|
||||
await invoker.editReply({ emoji: 'success', index: 'COMMAND_POLL_START', params: { channel: targetChannel.id } });
|
||||
}
|
||||
else if (subcommand!.name === 'delete')
|
||||
{
|
||||
const poll = await this.client.polls.delete(message!.asString);
|
||||
if (!poll)
|
||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||
const pollChannel = await guild.resolveChannel<TextChannel>(poll.payload.channel);
|
||||
if (!pollChannel)
|
||||
return { index: 'COMMAND_POLL_MISSING_CHANNEL', emoji: 'failure' };
|
||||
const msg = await pollChannel.messages.fetch(poll.payload.message).catch(() => null);
|
||||
if (msg)
|
||||
await msg.delete();
|
||||
return { index: 'COMMAND_POLL_DELETED', emoji: 'success' };
|
||||
}
|
||||
else if (subcommand!.name === 'end')
|
||||
{
|
||||
const poll = await this.client.polls.find(message!.asString);
|
||||
if (!poll)
|
||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||
await this.client.polls.end(poll.payload);
|
||||
// await guild._poll(poll.data as PollData & CallbackData);
|
||||
return { index: 'COMMAND_POLL_ENDED', emoji: 'success' };
|
||||
}
|
||||
}
|
||||
|
||||
async #queryQuestions (invoker: InvokerWrapper<true>, choices: number, targetchannel: GuildTextBasedChannel)
|
||||
{
|
||||
const { guild, member } = invoker;
|
||||
@ -166,6 +151,31 @@ class PollCommand extends SlashCommand
|
||||
}
|
||||
return questions;
|
||||
}
|
||||
|
||||
async #deletePoll (invoker: InvokerWrapper<true>, { message }: CommandParams)
|
||||
{
|
||||
const { guild } = invoker;
|
||||
const poll = await this.client.polls.delete(message!.asString);
|
||||
if (!poll)
|
||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||
const pollChannel = await guild.resolveChannel<TextChannel>(poll.payload.channel);
|
||||
if (!pollChannel)
|
||||
return { index: 'COMMAND_POLL_MISSING_CHANNEL', emoji: 'failure' };
|
||||
const msg = await pollChannel.messages.fetch(poll.payload.message).catch(() => null);
|
||||
if (msg)
|
||||
await msg.delete();
|
||||
return { index: 'COMMAND_POLL_DELETED', emoji: 'success' };
|
||||
}
|
||||
|
||||
async #endPoll (_invoker: InvokerWrapper<true>, { message }: CommandParams)
|
||||
{
|
||||
const poll = await this.client.polls.find(message!.asString);
|
||||
if (!poll)
|
||||
return { index: 'COMMAND_POLL_404', emoji: 'failure' };
|
||||
await this.client.polls.end(poll._id, poll.payload);
|
||||
// await guild._poll(poll.data as PollData & CallbackData);
|
||||
return { index: 'COMMAND_POLL_ENDED', emoji: 'success' };
|
||||
}
|
||||
}
|
||||
|
||||
export default PollCommand;
|
@ -47,12 +47,12 @@ class PollManager implements CallbackClient
|
||||
return poll;
|
||||
}
|
||||
|
||||
async handleCallback (_id: string, payload: PollData): Promise<void>
|
||||
async handleCallback (id: string, payload: PollData): Promise<void>
|
||||
{
|
||||
await this.end(payload);
|
||||
await this.end(id, payload);
|
||||
}
|
||||
|
||||
async end ({ user, channel, startedIn, message, multiChoice }: PollData)
|
||||
async end (id: string, { user, channel, startedIn, message, multiChoice }: PollData)
|
||||
{
|
||||
const startChannel = await this.#client.resolveChannel(startedIn);
|
||||
const pollChannel = await this.#client.resolveChannel(channel);
|
||||
@ -89,6 +89,7 @@ class PollManager implements CallbackClient
|
||||
if (startChannel && startChannel.isTextBased())
|
||||
await startChannel.send(guild.format('COMMAND_POLL_NOTIFY_STARTER', { user, channel }));
|
||||
}
|
||||
await this.callbacks.removeCallback(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user