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 {
|
||||
// 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,
|
||||
});
|
||||
|
||||
|
@ -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,20 +50,7 @@ const registerSelfRoles = async (interaction) => {
|
||||
return response;
|
||||
};
|
||||
|
||||
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');
|
||||
|
||||
step = 'save data from';
|
||||
const saveMessageData = async (id, role, emoji) => {
|
||||
// Try finding existing entry
|
||||
const rep = await RoleEmojiPair.findOne({
|
||||
where: {
|
||||
@ -78,10 +65,29 @@ const addSelfRoles = async (interaction) => {
|
||||
]
|
||||
}
|
||||
});
|
||||
if (rep !== null) throw new Error();
|
||||
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
|
||||
RoleEmojiPair.create({ message: id, role: role.id, emoji });
|
||||
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
|
||||
@ -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);
|
||||
|
||||
|
@ -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}'.`);
|
||||
}
|
||||
|
@ -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}'.`);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user