generated from Baipyrus/DiscordJS-Template
shared add self role pair function
This commit is contained in:
parent
86091f5eee
commit
61b8a9a2ec
@ -1,65 +1,11 @@
|
||||
import { TextInputBuilder, TextInputStyle } from 'discord.js';
|
||||
import { RoleEmojiPair } from '../../database.js';
|
||||
import {
|
||||
ModalBuilder,
|
||||
ActionRowBuilder,
|
||||
ApplicationCommandType,
|
||||
ContextMenuCommandBuilder
|
||||
} from 'discord.js';
|
||||
import { ModalBuilder } from 'discord.js';
|
||||
|
||||
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, msgID, emoji, role) => {
|
||||
const { channel } = interaction;
|
||||
|
||||
let step = 'fetch';
|
||||
try {
|
||||
// Get message by id
|
||||
const message = await channel.messages.fetch(msgID);
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
import { addSelfRoles } from '../../shared.js';
|
||||
|
||||
export const data = new ContextMenuCommandBuilder()
|
||||
.setDMPermission(false)
|
||||
@ -83,7 +29,7 @@ export async function modalSubmit(interaction) {
|
||||
return;
|
||||
}
|
||||
|
||||
await addSelfRoles(interaction, message, emoji, role);
|
||||
await addSelfRoles(interaction, message, role, emoji);
|
||||
}
|
||||
export async function execute(interaction) {
|
||||
const modal = new ModalBuilder()
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Op } from 'sequelize';
|
||||
import { SlashCommandBuilder } from 'discord.js';
|
||||
import { Message, RoleEmojiPair } from './../../database.js';
|
||||
import { addSelfRoles } from '../../shared.js';
|
||||
|
||||
const createSelfRoles = async (interaction) => {
|
||||
const { options, channel } = interaction;
|
||||
@ -50,67 +51,6 @@ const registerSelfRoles = async (interaction) => {
|
||||
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()
|
||||
.setName('self_roles')
|
||||
.setDMPermission(false)
|
||||
@ -163,13 +103,18 @@ export async function execute(interaction) {
|
||||
createNew = true;
|
||||
break;
|
||||
case 'register':
|
||||
const { success, msgID } = await registerSelfRoles(interaction);
|
||||
id = msgID ?? id;
|
||||
const response = await registerSelfRoles(interaction);
|
||||
id = response.msgID ?? id;
|
||||
// Flag to create new database entry
|
||||
createNew = success;
|
||||
createNew = response.success;
|
||||
break;
|
||||
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;
|
||||
}
|
||||
|
||||
|
56
shared.js
Normal file
56
shared.js
Normal file
@ -0,0 +1,56 @@
|
||||
import { Op } from "sequelize";
|
||||
import { RoleEmojiPair } from "./database.js";
|
||||
|
||||
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 });
|
||||
};
|
||||
|
||||
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