clean up: refactor by splitting into functions
This commit is contained in:
parent
04b259cf56
commit
cd44a4717a
|
@ -1,6 +1,107 @@
|
||||||
|
import { Op } from 'sequelize';
|
||||||
import { SlashCommandBuilder } from 'discord.js';
|
import { SlashCommandBuilder } from 'discord.js';
|
||||||
import { Message, RoleEmojiPair } from './../../database.js';
|
import { Message, RoleEmojiPair } from './../../database.js';
|
||||||
|
|
||||||
|
const createSelfRoles = async (interaction) => {
|
||||||
|
const { options, channel } = interaction;
|
||||||
|
|
||||||
|
// Create message with text input
|
||||||
|
const text = options.getString('text');
|
||||||
|
const id = (await channel.send(text)).id;
|
||||||
|
|
||||||
|
// Reply and delete to acknowledge command
|
||||||
|
interaction.deferReply();
|
||||||
|
interaction.deleteReply();
|
||||||
|
|
||||||
|
return id;
|
||||||
|
};
|
||||||
|
|
||||||
|
const registerSelfRoles = async (interaction) => {
|
||||||
|
const { options, channel } = interaction;
|
||||||
|
const id = options.getString('id');
|
||||||
|
const response = {
|
||||||
|
success: false,
|
||||||
|
msgID: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get message by id
|
||||||
|
await channel.messages.fetch(id);
|
||||||
|
|
||||||
|
// Reply successfully to acknowledge command
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Successfully fetched message!',
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
response.success = true;
|
||||||
|
response.msgID = id;
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (_) {
|
||||||
|
// Reply failed to acknowledge command
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Failed to fetch message!',
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
};
|
||||||
|
|
||||||
|
const addSelfRoles = async (interaction) => {
|
||||||
|
const { options, channel } = interaction;
|
||||||
|
const id = options.getString('id');
|
||||||
|
|
||||||
|
let step = 'fetch';
|
||||||
|
try {
|
||||||
|
// Get message by id
|
||||||
|
const message = await channel.messages.fetch(id);
|
||||||
|
|
||||||
|
// Get user arguments
|
||||||
|
const role = options.getRole('role');
|
||||||
|
const emoji = options.getString('emoji');
|
||||||
|
|
||||||
|
step = 'save data from';
|
||||||
|
// Try finding existing entry
|
||||||
|
const rep = await RoleEmojiPair.findOne({
|
||||||
|
where: {
|
||||||
|
[Op.or]: [
|
||||||
|
{
|
||||||
|
message: id,
|
||||||
|
role: role.id
|
||||||
|
}, {
|
||||||
|
message: id,
|
||||||
|
emoji
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (rep !== null) throw new Error();
|
||||||
|
|
||||||
|
// Create database entry for pair
|
||||||
|
RoleEmojiPair.create({ message: id, role: role.id, emoji });
|
||||||
|
|
||||||
|
step = 'react to';
|
||||||
|
// React with emoji to message
|
||||||
|
await message.react(emoji);
|
||||||
|
|
||||||
|
// Reply successfully to acknowledge command
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Added new entry for self roles!',
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
|
||||||
|
} catch (_) {
|
||||||
|
// Reply failed to acknowledge command
|
||||||
|
await interaction.reply({
|
||||||
|
content: `Failed to ${step} message!`,
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export const data = new SlashCommandBuilder()
|
export const data = new SlashCommandBuilder()
|
||||||
.setName('self_roles')
|
.setName('self_roles')
|
||||||
.setDescription('Manages reactions for self roles.')
|
.setDescription('Manages reactions for self roles.')
|
||||||
|
@ -42,58 +143,28 @@ export const data = new SlashCommandBuilder()
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
.setDescription('The emoji to be reacted with.')));
|
.setDescription('The emoji to be reacted with.')));
|
||||||
export async function execute(interaction) {
|
export async function execute(interaction) {
|
||||||
const channel = interaction.channel;
|
const { options } = interaction;
|
||||||
|
|
||||||
let createNew = false;
|
let createNew = false, id;
|
||||||
let id = interaction.options.getString('id');
|
switch (options.getSubcommand()) {
|
||||||
switch (interaction.options.getSubcommand()) {
|
|
||||||
case 'create':
|
case 'create':
|
||||||
// Create message with text input
|
id = await createSelfRoles(interaction);
|
||||||
const text = interaction.options.getString('text');
|
|
||||||
id = (await channel.send(text)).id;
|
|
||||||
// Reply and delete to acknowledge command
|
|
||||||
interaction.deferReply();
|
|
||||||
interaction.deleteReply();
|
|
||||||
// Flag to create new database entry
|
// Flag to create new database entry
|
||||||
createNew = true;
|
createNew = true;
|
||||||
break;
|
break;
|
||||||
case 'register':
|
case 'register':
|
||||||
try {
|
const { success, msgID } = await registerSelfRoles(interaction);
|
||||||
// Get message by id
|
id = msgID ?? id;
|
||||||
await channel.messages.fetch(id);
|
|
||||||
// Reply successfully to acknowledge command
|
|
||||||
await interaction.reply({
|
|
||||||
content: 'Successfully fetched message!',
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
// Flag to create new database entry
|
// Flag to create new database entry
|
||||||
createNew = true;
|
createNew = success;
|
||||||
} catch (_) {
|
|
||||||
// Reply failed to acknowledge command
|
|
||||||
await interaction.reply({
|
|
||||||
content: 'Failed to fetch message!',
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'add':
|
case 'add':
|
||||||
const role = interaction.options.getRole('role');
|
await addSelfRoles(interaction);
|
||||||
const emoji = interaction.options.getString('emoji');
|
|
||||||
|
|
||||||
// Reply successfully to acknowledge command
|
|
||||||
await interaction.reply({
|
|
||||||
content: 'Added new entry for self roles!',
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
RoleEmojiPair.create({ message: id, role: role.id, emoji });
|
|
||||||
|
|
||||||
console.debug(`[DEBUG] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createNew) {
|
if (createNew) {
|
||||||
console.debug(`[DEBUG] New self roles on message with ID: '${id}'.`);
|
console.info(`[INFO] New self roles on message with ID: '${id}'.`);
|
||||||
Message.create({ id });
|
Message.create({ id });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user