generated from Baipyrus/DiscordJS-Template
Compare commits
5 Commits
cb56dded3b
...
fdedb50bc5
Author | SHA1 | Date | |
---|---|---|---|
fdedb50bc5 | |||
61b8a9a2ec | |||
86091f5eee | |||
095ad012ff | |||
4bdd04a881 |
@ -1,30 +1,65 @@
|
|||||||
import { Message } from '../../database.js';
|
import { TextInputBuilder, TextInputStyle } from 'discord.js';
|
||||||
import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js';
|
import {
|
||||||
|
ModalBuilder,
|
||||||
|
ActionRowBuilder,
|
||||||
|
ApplicationCommandType,
|
||||||
|
ContextMenuCommandBuilder
|
||||||
|
} from 'discord.js';
|
||||||
|
import { addSelfRoles } from '../../shared.js';
|
||||||
|
|
||||||
export const data = new ContextMenuCommandBuilder()
|
export const data = new ContextMenuCommandBuilder()
|
||||||
.setDMPermission(false)
|
.setDMPermission(false)
|
||||||
.setName('Register self roles')
|
.setName('Add role emoji pair')
|
||||||
.setType(ApplicationCommandType.Message);
|
.setType(ApplicationCommandType.Message);
|
||||||
|
export async function modalSubmit(interaction) {
|
||||||
|
const { fields, guild } = interaction;
|
||||||
|
// Get text inputs from modal
|
||||||
|
const message = fields.getTextInputValue('message');
|
||||||
|
const roleID = fields.getTextInputValue('role');
|
||||||
|
const emoji = fields.getTextInputValue('emoji');
|
||||||
|
|
||||||
|
// Fetch role from guild
|
||||||
|
const role = await guild.roles.fetch(roleID);
|
||||||
|
// Role not found
|
||||||
|
if (role === null) {
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Could not fetch role! Please contact server staff.',
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await addSelfRoles(interaction, message, role, emoji);
|
||||||
|
}
|
||||||
export async function execute(interaction) {
|
export async function execute(interaction) {
|
||||||
|
const modal = new ModalBuilder()
|
||||||
|
.setCustomId('Add role emoji pair-pair')
|
||||||
|
.setTitle('Role Emoji Pair');
|
||||||
|
|
||||||
const id = interaction.targetMessage.id;
|
const id = interaction.targetMessage.id;
|
||||||
|
const message = new ActionRowBuilder().addComponents(
|
||||||
|
new TextInputBuilder()
|
||||||
|
.setLabel('The message ID this command is run on.')
|
||||||
|
.setStyle(TextInputStyle.Short)
|
||||||
|
.setCustomId('message')
|
||||||
|
.setRequired(true)
|
||||||
|
.setValue(id));
|
||||||
|
|
||||||
try {
|
const role = new ActionRowBuilder().addComponents(
|
||||||
// Create database entry
|
new TextInputBuilder()
|
||||||
await Message.create({ id });
|
.setLabel('Enter exactly one role ID.')
|
||||||
|
.setStyle(TextInputStyle.Short)
|
||||||
|
.setCustomId('role')
|
||||||
|
.setRequired(true));
|
||||||
|
|
||||||
// Reply successfully to acknowledge command
|
const emoji = new ActionRowBuilder().addComponents(
|
||||||
await interaction.reply({
|
new TextInputBuilder()
|
||||||
content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`,
|
.setLabel('Enter exactly one emoji.')
|
||||||
ephemeral: true,
|
.setStyle(TextInputStyle.Short)
|
||||||
});
|
.setCustomId('emoji')
|
||||||
|
.setRequired(true));
|
||||||
|
|
||||||
console.info(`[INFO] New self roles on message with ID: '${id}'.`);
|
modal.addComponents(message, role, emoji);
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
await interaction.showModal(modal);
|
||||||
// Reply failed to acknowledge command
|
|
||||||
await interaction.reply({
|
|
||||||
content: 'Failed to save data from message!',
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Op } from 'sequelize';
|
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';
|
||||||
|
import { addSelfRoles } from '../../shared.js';
|
||||||
|
|
||||||
const createSelfRoles = async (interaction) => {
|
const createSelfRoles = async (interaction) => {
|
||||||
const { options, channel } = interaction;
|
const { options, channel } = interaction;
|
||||||
@ -50,67 +51,6 @@ const registerSelfRoles = async (interaction) => {
|
|||||||
return response;
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveMessageData = async (id, role, emoji) => {
|
|
||||||
// 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(`Failed to fetch RoleEmojiPair entry with data {message:${id},role:${role.id},emoji:${emoji}}!`);
|
|
||||||
|
|
||||||
// Create database entry for pair
|
|
||||||
await RoleEmojiPair.create({ message: id, role: role.id, emoji });
|
|
||||||
};
|
|
||||||
|
|
||||||
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')
|
|
||||||
.replace(/:.*?:/, ':_:');
|
|
||||||
|
|
||||||
step = 'save data from';
|
|
||||||
await saveMessageData(id, role, 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 (error) {
|
|
||||||
console.error(error);
|
|
||||||
|
|
||||||
// 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')
|
||||||
.setDMPermission(false)
|
.setDMPermission(false)
|
||||||
@ -163,13 +103,18 @@ export async function execute(interaction) {
|
|||||||
createNew = true;
|
createNew = true;
|
||||||
break;
|
break;
|
||||||
case 'register':
|
case 'register':
|
||||||
const { success, msgID } = await registerSelfRoles(interaction);
|
const response = await registerSelfRoles(interaction);
|
||||||
id = msgID ?? id;
|
id = response.msgID ?? id;
|
||||||
// Flag to create new database entry
|
// Flag to create new database entry
|
||||||
createNew = success;
|
createNew = response.success;
|
||||||
break;
|
break;
|
||||||
case 'add':
|
case 'add':
|
||||||
await addSelfRoles(interaction);
|
// Get command options
|
||||||
|
const msgID = options.getString('id');
|
||||||
|
const role = options.getRole('role');
|
||||||
|
const emoji = options.getString('emoji');
|
||||||
|
// Try adding self role pair
|
||||||
|
await addSelfRoles(interaction, msgID, role, emoji);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,9 +21,9 @@ const executeCommand = async (interaction, command) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const genericExecute = async (interaction, command, name, description) => {
|
const genericExecute = async (interaction, command, name, description, cmdName) => {
|
||||||
try {
|
try {
|
||||||
console.info(`[INFO] Command ${interaction.commandName} ${description ?? `used "${name}"`}.`);
|
console.info(`[INFO] Command ${(cmdName ?? interaction.commandName) ?? 'anonymous'} ${description ?? `used "${name}"`}.`);
|
||||||
await command[name](interaction);
|
await command[name](interaction);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -49,7 +49,7 @@ export async function execute(interaction) {
|
|||||||
const name = interaction.customId.split('-')[0];
|
const name = interaction.customId.split('-')[0];
|
||||||
command = interaction.client.commands.get(name);
|
command = interaction.client.commands.get(name);
|
||||||
|
|
||||||
await genericExecute(interaction, command, 'modalSubmit', 'submitted a modal');
|
await genericExecute(interaction, command, 'modalSubmit', 'submitted a modal', name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
60
shared.js
Normal file
60
shared.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import { Op } from "sequelize";
|
||||||
|
import { Message, RoleEmojiPair } from "./database.js";
|
||||||
|
|
||||||
|
const saveMessageData = async (id, role, emoji) => {
|
||||||
|
// Try finding message
|
||||||
|
const msg = await Message.findOne({ where: { id } });
|
||||||
|
if (msg === null) throw new Error(`No message with ID '${id}' could be found!`);
|
||||||
|
|
||||||
|
// 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(`Existing RoleEmojiPair entry with (partial) data {message:${id},role:${role.id},emoji:${emoji}}!`);
|
||||||
|
|
||||||
|
// Create database entry for pair
|
||||||
|
await RoleEmojiPair.create({ message: id, role: role.id, emoji });
|
||||||
|
};
|
||||||
|
|
||||||
|
export const addSelfRoles = async (interaction, msgID, role, emoji) => {
|
||||||
|
const { channel } = interaction;
|
||||||
|
|
||||||
|
let step = 'fetch';
|
||||||
|
try {
|
||||||
|
// Get message by id
|
||||||
|
const message = await channel.messages.fetch(msgID);
|
||||||
|
|
||||||
|
step = 'save data from';
|
||||||
|
await saveMessageData(msgID, role, 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 (error) {
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
// Reply failed to acknowledge command
|
||||||
|
await interaction.reply({
|
||||||
|
content: `Failed to ${step} message!`,
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user