Compare commits

..

5 Commits

4 changed files with 101 additions and 32 deletions

View File

@ -9,11 +9,11 @@ export async function execute(interaction) {
try {
// Create database entry
Message.create({ id });
await Message.create({ id });
// Reply successfully to acknowledge command
await interaction.reply({
content: 'Successfully saved data from message!',
content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`,
ephemeral: true,
});

View File

@ -10,8 +10,8 @@ const createSelfRoles = async (interaction) => {
const id = (await channel.send(text)).id;
// Reply and delete to acknowledge command
interaction.deferReply();
interaction.deleteReply();
await interaction.deferReply();
await interaction.deleteReply();
return id;
};
@ -50,6 +50,27 @@ 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');
@ -61,27 +82,12 @@ const addSelfRoles = async (interaction) => {
// Get user arguments
const role = options.getRole('role');
const emoji = options.getString('emoji');
const emoji = options
.getString('emoji')
.replace(/:.*?:/, ':_:');
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 });
await saveMessageData(id, role, emoji);
step = 'react to';
// React with emoji to message
@ -114,8 +120,8 @@ export const data = new SlashCommandBuilder()
.setDescription('Creates new message in channel.')
.addStringOption(option =>
option
.setRequired(true)
.setName('text')
.setRequired(true)
.setDescription('The text to be displayed in the message.')))
.addSubcommand(subcommand =>
subcommand
@ -169,7 +175,7 @@ export async function execute(interaction) {
if (createNew) {
try {
// Create database entry
Message.create({ id });
await Message.create({ id });
} catch (error) {
console.error(error);

View File

@ -1,11 +1,43 @@
import { Events } from 'discord.js';
import { config } from 'dotenv';
import { Events } from 'discord.js';
import { Message, RoleEmojiPair } from '../database.js';
config();
export const name = Events.MessageReactionAdd;
export async function execute(reaction, user) {
if (user.id === process.env.CLIENT) return;
const uname = user.username;
const rname = reaction._emoji.name;
console.debug(`[DEBUG] User '${uname}' reacted with emoji '${rname}'.`);
// Get message
const msgID = reaction.message.id;
const message = await Message.findOne({
where: {
id: msgID,
}
});
// Ignore if unregistered
if (message === null) return;
// Get emoji
const emoji = reaction.emoji.toString();
const rep = await RoleEmojiPair.findOne({
where: {
message: msgID,
emoji,
}
});
// Deny if unregistered
if (rep === null) {
// Remove reaction and quit
await reaction.remove();
return;
}
// Fetch role from guild
const guild = reaction.message.guild;
const role = await guild.roles.fetch(rep.role);
if (role === null) return;
// Add role to user
await guild.members.addRole({ role, user });
console.info(`[INFO] Added role with id '${role.id}' to user '${user.username}'.`);
}

View File

@ -1,8 +1,39 @@
import { config } from 'dotenv';
import { Events } from 'discord.js';
import { Message, RoleEmojiPair } from '../database.js';
config();
export const name = Events.MessageReactionRemove;
export async function execute(reaction, user) {
const uname = user.username;
const rname = reaction._emoji.name;
console.debug(`[DEBUG] User '${uname}' removed reaction of emoji '${rname}'.`);
if (user.id === process.env.CLIENT) return;
// Get message
const msgID = reaction.message.id;
const message = await Message.findOne({
where: {
id: msgID,
}
});
// Ignore if unregistered
if (message === null) return;
// Get emoji
const emoji = reaction.emoji.toString();
const rep = await RoleEmojiPair.findOne({
where: {
message: msgID,
emoji,
}
});
// Deny if unregistered
if (rep === null) return;
// Fetch role from guild
const guild = reaction.message.guild;
const role = await guild.roles.fetch(rep.role);
if (role === null) return;
// Add role to user
await guild.members.removeRole({ role, user });
console.info(`[INFO] Removed role with id '${role.id}' from user '${user.username}'.`);
}