generated from Baipyrus/DiscordJS-Template
Compare commits
5 Commits
d7409ed0f7
...
531b07e304
Author | SHA1 | Date | |
---|---|---|---|
531b07e304 | |||
42293efb68 | |||
3fa0bd9c1f | |||
1e716d5f5e | |||
afa958b557 |
@ -9,11 +9,11 @@ export async function execute(interaction) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Create database entry
|
// Create database entry
|
||||||
Message.create({ id });
|
await 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!',
|
content: `Successfully saved data from message! Add roles to it with reference ID '${id}'.`,
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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
|
||||||
interaction.deferReply();
|
await interaction.deferReply();
|
||||||
interaction.deleteReply();
|
await interaction.deleteReply();
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
@ -50,6 +50,27 @@ 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');
|
||||||
@ -61,27 +82,12 @@ const addSelfRoles = async (interaction) => {
|
|||||||
|
|
||||||
// Get user arguments
|
// Get user arguments
|
||||||
const role = options.getRole('role');
|
const role = options.getRole('role');
|
||||||
const emoji = options.getString('emoji');
|
const emoji = options
|
||||||
|
.getString('emoji')
|
||||||
|
.replace(/:.*?:/, ':_:');
|
||||||
|
|
||||||
step = 'save data from';
|
step = 'save data from';
|
||||||
// Try finding existing entry
|
await saveMessageData(id, role, emoji);
|
||||||
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
|
||||||
@ -114,8 +120,8 @@ export const data = new SlashCommandBuilder()
|
|||||||
.setDescription('Creates new message in channel.')
|
.setDescription('Creates new message in channel.')
|
||||||
.addStringOption(option =>
|
.addStringOption(option =>
|
||||||
option
|
option
|
||||||
.setRequired(true)
|
|
||||||
.setName('text')
|
.setName('text')
|
||||||
|
.setRequired(true)
|
||||||
.setDescription('The text to be displayed in the message.')))
|
.setDescription('The text to be displayed in the message.')))
|
||||||
.addSubcommand(subcommand =>
|
.addSubcommand(subcommand =>
|
||||||
subcommand
|
subcommand
|
||||||
@ -169,7 +175,7 @@ export async function execute(interaction) {
|
|||||||
if (createNew) {
|
if (createNew) {
|
||||||
try {
|
try {
|
||||||
// Create database entry
|
// Create database entry
|
||||||
Message.create({ id });
|
await Message.create({ id });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
|
@ -1,11 +1,43 @@
|
|||||||
import { Events } from 'discord.js';
|
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
|
import { Events } from 'discord.js';
|
||||||
|
import { Message, RoleEmojiPair } from '../database.js';
|
||||||
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;
|
|
||||||
const rname = reaction._emoji.name;
|
// Get message
|
||||||
console.debug(`[DEBUG] User '${uname}' reacted with 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) {
|
||||||
|
// 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}'.`);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,39 @@
|
|||||||
|
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) {
|
||||||
const uname = user.username;
|
if (user.id === process.env.CLIENT) return;
|
||||||
const rname = reaction._emoji.name;
|
|
||||||
console.debug(`[DEBUG] User '${uname}' removed reaction of 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) 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}'.`);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user