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 { try {
// Create database entry // Create database entry
await Message.create({ id }); Message.create({ id });
// Reply successfully to acknowledge command // Reply successfully to acknowledge command
await interaction.reply({ 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, ephemeral: true,
}); });

View File

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

View File

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