Compare commits

..

No commits in common. "531b07e30443c969e2d8ce65957a339647d96dd5" and "d7409ed0f72db39c077ff059a757e3425362cf51" have entirely different histories.

4 changed files with 32 additions and 101 deletions

View File

@ -9,11 +9,11 @@ export async function execute(interaction) {
try {
// Create database entry
await Message.create({ id });
Message.create({ id });
// Reply successfully to acknowledge command
await interaction.reply({
content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`,
content: 'Successfully saved data from message!',
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
await interaction.deferReply();
await interaction.deleteReply();
interaction.deferReply();
interaction.deleteReply();
return id;
};
@ -50,27 +50,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');
@ -82,12 +61,27 @@ const addSelfRoles = async (interaction) => {
// Get user arguments
const role = options.getRole('role');
const emoji = options
.getString('emoji')
.replace(/:.*?:/, ':_:');
const emoji = options.getString('emoji');
step = 'save data from';
await saveMessageData(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();
// Create database entry for pair
RoleEmojiPair.create({ message: id, role: role.id, emoji });
step = 'react to';
// React with emoji to message
@ -120,8 +114,8 @@ export const data = new SlashCommandBuilder()
.setDescription('Creates new message in channel.')
.addStringOption(option =>
option
.setName('text')
.setRequired(true)
.setName('text')
.setDescription('The text to be displayed in the message.')))
.addSubcommand(subcommand =>
subcommand
@ -175,7 +169,7 @@ export async function execute(interaction) {
if (createNew) {
try {
// Create database entry
await Message.create({ id });
Message.create({ id });
} catch (error) {
console.error(error);

View File

@ -1,43 +1,11 @@
import { config } from 'dotenv';
import { Events } from 'discord.js';
import { Message, RoleEmojiPair } from '../database.js';
import { config } from 'dotenv';
config();
export const name = Events.MessageReactionAdd;
export async function execute(reaction, user) {
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) {
// 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}'.`);
const uname = user.username;
const rname = reaction._emoji.name;
console.debug(`[DEBUG] User '${uname}' reacted with emoji '${rname}'.`);
}

View File

@ -1,39 +1,8 @@
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) {
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}'.`);
const uname = user.username;
const rname = reaction._emoji.name;
console.debug(`[DEBUG] User '${uname}' removed reaction of emoji '${rname}'.`);
}