I've been searching throughout Stack Overflow for answers, but I can't find a way to get a message by its id in multiple channels.
Basically, I'm trying to make a reaction role command using Discord.JS v14, but I can't be guaranteed that everyone who uses the bot is going to use it in the same channel the message is in. I've tried using .forEach()
but it doesn't work.
Here's my code:
const {
SlashCommandBuilder,
PermissionFlagsBits,
EmbedBuilder,
} = require("discord.js");
const { enabled, disabled } = require("../../../config.json");
const Settings = require("../../schemas/settings");
const mongoose = require("mongoose");
const Reaction = require("../../schemas/reactionRole");
module.exports = {
name: "reaction",
description: ``,
category: "Utility",
data: new SlashCommandBuilder()
.setName(`reaction`)
.setDescription(`Modify or setup reaction roles in your server!`)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.addSubcommand((subcommand) =>
subcommand
.setName(`setup`)
.setDescription(`Setup reaction roles in your server`)
.addRoleOption((option) =>
option
.setName("role")
.setDescription(`Specify a role you want to use`)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("message-id")
.setDescription(`Enter the message's id`)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName(`emoji`)
.setDescription(`Specify the emoji's id`)
.setRequired(true)
)
),
async execute(interaction, client) {
switch (interaction.options.getSubcommand()) {
case `setup`:
const role = interaction.options.getRole(`role`);
const msg = interaction.options.getString(`message`);
const emoj = interaction.options.getString(`emoji`);
const emoji = client.emojis.cache.get(emoj);
const message = interaction.channel.messages.fetch(msg) //How can I replace this?
const notEmoji = new EmbedBuilder()
.setTitle(`Invalid Emoji Specified`)
.setColor(`Red`)
.setDescription(`The emoji id you specified is not valid`)
.addFields({
name: `How to get emoji id:`,
value: `To get an emoji's id, you first type it as usual:\n:smile:\nTo get it's id, you add a backslash (\\) to it, as such: \:smile:\nThe number is what you use as the id.`,
});
const notMessage = new EmbedBuilder()
.setTitle(`Invalid Message Specified`)
.setColor(`Red`)
.setDescription(`The message id you specified is not valid`)
.addFields({
name: `How to get message id:`,
value: `To get a message's id, you hover over it whilst clicking shift, then a number of icons will appear, click on the ID icon to copy the id of the message`,
});
if (!message) return interaction.reply({ embeds: [notMessage] });
const m = await interaction.channel.messages.fetch(message); //How can I replace this?
m.react(emoji);
await new Reaction({
_id: mongoose.Types.ObjectId(),
guildId: interaction.guild.id,
messageId: message.id,
emojiId: emoji.id,
roleId: role.id,
});
break;
default:
break;
}
},
};