Discordjs - lastMessageId always null event after fetching - javascript

Introduction :
I'm pretty new at creating discord BOT and I'm currently trying to make like a ticket bot.
In order to do that, I need to check if the user that add the reaction is allow to do it or not.
Code :
I already checked this (successfully) when the user is using a command thanks to this (isAllow is a custom function that works when passing member object in params) :
if (Common.isAllow(message.member)) { /* code ... */ }
But I also want to check for the roles of the user that is reacting to a message, in order to create a new channel if the user is allowed to do it.
The problem is that when I get the user object, the lastMessageId is null, so I cannot get member object and check its roles.
The code that is checking for reaction is this one :
client.on("messageReactionAdd", async (reaction, user) => {
// When a reaction is received, check if the structure is partial
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
switch (reaction.message.channel.id) {
// Check if the message has been sent into the lawyer contact channel
case Config.CHANNELS.LAWYER_CONTACT:
case Config.CHANNELS.CONFIG:
checkForLawyerChannelReaction(reaction, user);
break;
default:
break;
}
});
If the reaction has been made in the good channel (LAWYER_CONTACT) or in the config channel (CONFIG), it will call this function, which is the one trying to check for permission :
function checkForLawyerChannelReaction(reaction, user) {
console.log('checkForLawyerChanelReaction');
console.log(user);
if (Common.isAllow( /* member object */ ) { /* code ... */ }
The response I get for console.log(user) is :
Result
I already tried to fetch user data like this, and it doesn't change a thing :
console.log(await client.users.fetch('123456789123456789'));
(ID has been changed, of course)
Note that the BOT has joined (again) yesterday with administrator permissions, and message has been sent by the user since yesterday.
Does somebody has a fix for this or information that I may not have yet ?
Thank you for your time.

You can get the GuildMember object by passing the user as an argument into the Guild.member(user) method.
const guildMember = reaction.message.guild.member(user);

Related

Reaction event discord.js

I'm trying to make a starboard code with my bot, and everything else is working good. But I'm trying to make it to where the bot ignores reactions from the author of the actual message.
This is my current code:
client.on('messageReactionAdd', (reaction_orig, message, user) => {
if (message.author.id === reaction_orig.users.id) return
manageBoard(reaction_orig)
})
It returns the following error:
if (message.author.id === reaction_orig.users.id) return;
^
TypeError: Cannot read property 'id' of undefined
The problem is that messageReactionAdd takes two parameters; the message reaction as the first one, and the user that applied the emoji as the second one. When you write reaction_orig, message, user, reaction_orig is the reaction (which is correct), but message is the user who reacted as it's the second parameter. The user variable will be undefined.
Another issue is that reaction_orig.users returns a ReactionUserManager that doesn't have an id property. Luckily, the user is already passed down to your callback so you can use its ID.
Also, reaction_orig has a message property, the original message that this reaction refers to so you can get its authors' ID from it.
You can change your code to this to work:
client.on('messageReactionAdd', (reaction_orig, user) => {
if (reaction_orig.message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
manageBoard(reaction_orig);
});
However, the code above only works on cached messages, ones posted after the bot is connected. Reacting on older messages won't fire the messageReactionAdd event. If you also want to listen to reactions on old messages you need to enable partial structures for MESSAGE, CHANNEL and REACTION when instantiating your client, like this:
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
You can check if the message is cached by e.g. checking if its author property is not null. If it's null, you can fetch the message. Now, you have both the message author and the user who reacted, so you can compare their IDs:
// make sure it's an async function
client.on('messageReactionAdd', async (reaction_orig, user) => {
// fetch the message if it's not cached
const message = !reaction_orig.message.author
? await reaction_orig.message.fetch()
: reaction_orig.message;
if (message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
// the reaction is coming from a different user
manageBoard(reaction_orig);
});
Try doing this :
client.on('messageReactionAdd', (reaction, user) => {
if (!reaction.message.author.id === user.id){
//Do whatever you like with it
console.log(reaction.name)
}
});
Note: The message must be cached. For that you'll need to do this
Client.channels.cache.get("ChannelID").messages.fetch("MessageID");
I'm guessing you're using discord.js v12

Problem defining typingStart to a specific channel

I am trying to get my discord bot to send this message only if the user starts typing in the defined channel, and not other text channels. I don't get any errors, the bot just doesn't send a message. What am I doing wrong here? Can typingStart be defined to a specific channel?
const Join2_channel = "972135774921247676"
bot.on("typingStart", (message , channel) => {
if (channel.id === Join2_channel) {
message.send('Type !join');
}
});
The typingStart event takes two parameters; channel (the channel the user started typing in) and user (that started typing), in this order.
In your current code you're checking if the user.id is 972135774921247676 and as that's a channel's snowflake, it won't match. You need to update your callback function:
bot.on("typingStart", (channel, user) => {
if (channel.id === Join2_channel) {
channel.send('Type !join');
}
});

How do i make a discord bot send a message when a specific user is mentioned?

I'm fairly new to javascript and have been using Discord.js to make one or two Discord Bots. I'm working on a feature that sends a message when I'm pinged in my discord server. I've tried a few things and none have worked.
I have this so far, it detects when any user is pinged, not just me.
client.on('message', (message) => {
if (message.mentions.members.first()) {
message.channel.send('Do not ping this user.');
}
});
You can compare User IDs. How to get User IDs.
if (message.mentions.users.first().id === 'Your ID') // if the person mentioned was you
return message.channel.send('Do not mention this user');
Furthermore, as the name suggests, Collection.first() will fetch the first element of a collection. This means that the if statement will only return true if the first mention was you. For example:
User: 'Hello #you' // detected
User: 'Hello #notYou and #you' // not detected
To circumvent this, you can use Collection.has():
// will return true if *any* of the mentions were you
if (message.mentions.users.has('Your ID'))
return message.channel.send('Do not mention this user');

How to fix "Problem with Reactions (Restart bot)"

I've a problem with a reaction reply System.
I want that when a user adds a reaction, that replies to a message, except that when the bot reboots it is no longer detected by the bot.
Do you know how to fix this problem?
Here is my current code :
bot.on("messageReactionAdd", function(messageReaction, user){
if(messageReaction.message.content === "Message"){
if(user.bot){return}
messageReaction.message.reply("It works.")
}
})
bot.on("message", function(message){
if(message.content.startsWith(prefix + "test")){
message.delete()
message.member.createDM().then(m => m.send("Message").then(m => m.react("✅")))
}
}
on the latest version of discord.js, you can use the Partial Events to accomplish this.
According to the doc (https://discord.js.org/#/docs/main/master/topics/partials):
Partials allow you to receive events that contain uncached instances, providing structures that contain very minimal data. For example, if you were to receive a messageDelete event with an uncached message, normally Discord.js would discard the event. With partials, you're able to receive the event, with a Message object that contains just an ID.
What you need to do is
const Discord = require('discord.js');
// add "partials" in the bot options to enable partial events
const client = new Discord.Client({"partials": ['CHANNEL', 'MESSAGE']});
[...]
client.on("messageReactionAdd", async function(messageReaction, user){
// fetch message data if we got a partial event
if (messageReaction.message.partial) await messageReaction.message.fetch();
if(messageReaction.message.content === "Message"){
if(user.bot){return}
messageReaction.message.reply("It works.")
}
})
Doing this, you must be careful on your client.on("message", ...) calls, to avoid accessing data or method that are not available.
there is a boolean message.partial that you can use to discard partial message when you don't need them.

Referencing a DM channel

I'm trying to create a command that allows users to create their password following prompts through DM. I'm able to send a message to the user, but not able to read a message sent back with a MessageCollector because I cannot find a way to reference the DM channel.
I have tried using another instance of bot.on("message", message) here, but that creates a leak in the system causing the second instance to never disappear.
I also can't let users use a command say !CreatePassword *** because this function is linked with many others in a strict order.
Maybe I'm doing something fundamentally wrong, or approaching the problem in a bad way, but I need a way to reference a DM channel.
This is the best iteration of my code so far.
function createAccount(receivedMessage, embedMessage)
{
const chan = new Discord.DMChannel(bot, receivedMessage.author);
const msgCollector = new Discord.MessageCollector(chan , m => m.author.id == receivedMessage.author.id);
msgCollector.on("collect", (message) =>
{
// Other Code
msgCollector.stop();
// Removing an embed message on the server, which doesn't have a problem.
embedMessage.delete();
})
}
I can show the rest of my code if necessary.
Thank you for your time. I've lost an entire night of sleep over this.
I would do it like this (I'll assume that receivedMessage is the message that triggered the command, correct me if I'm wrong)
async function createAccount(receivedMessage, embedMessage) {
// first send a message to the user, so that you're sure that the DM channel exists.
let firstMsg = await receivedMessage.author.send("Type your password here");
let filter = () => true; // you don't need it, since it's a DM.
let collected = await firstMsg.channel.awaitMessages(filter, {
maxMatches: 1, // you only need one message
time: 60000 // the time you want it to run for
}).catch(console.log);
if (collected && collected.size > 0) {
let password = collected.first().content.split(' ')[0]; // grab the password
collected.forEach(msg => msg.delete()); // delete every collected message (and so the password)
await firstMsg.edit("Password saved!"); // edit the first message you sent
} else await firstMsg.edit("Command timed out :("); // no message has been received
firstMsg.delete(30000); // delete it after 30 seconds
}

Categories