Fetch old reactions to an old message - discord.js

I've written a discord.js bot with version 12.0.2 and some features have been running for a while but are now disfunctionning and it seems to be related to empty cache, but I can't manage to figure out how to solve it.
Here is the scenario :
I have my bot weekly posting a message on a server and guild members have a week to react to this message. At the end of the week, I want to analyze the reactions to the bot to decide what to do (I can't have the bot running 24/7 to await for reactions).
Here is my problem :
Once I've fetched this specific message, what I did until now was msg.reactions.resolve('✅').users.fetch().then(somestuff), but since a few weeks it stopped functionning and now throws (node:2336) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'users' of null.
I've been through some debugging steps and found out that msg.reactions.cache is empty, hence here I think the problem is, but I cannot find how to fill it.
Here is what msg.reactions contains:
ReactionManager {
cacheType: [Function: Collection],
cache: Collection [Map] {},
message: Message {
details_about_the_message_that_confirm_it_is_the_desired_one_that_have_been_reacted_to
}
}
Would anybody have ideas about how to solve this please ? Any help would be much appreciated, thank you !

I've finally found a solution, or maybe I'd better call it a workaround, hence I'm sharing it here to close this post.
First of all, I have to say that msg.reactions.resolve('✅').users.fetch().then(somestuff) does its work and is not part of the problem, as the problem was lying in the message fetching process. Fetching a bunch of messages or fetching a specific one (i.e. by ID) leads to different results.
Fetching a specific message by ID let you access the ReactionManager's cache, while fetching several messages seems not to populate the cache.

Related

Is there a way to refresh the channel cache in Discord.Net

I am currently writing a Discord bot.
If it doesn't exist, it is supposed to create a thread in a given channel and write some messages there.
My current way of checking is
private SocketThreadChannel ThreadIfExists(string id, SocketGuildChannel channel)
{
return Context.Guild.ThreadChannels.Where(t => t.ParentChannel == channel)
.FirstOrDefault(t => t.Name.Contains(id));
}
where id is a unique id given to each thread.
Most of the time, it works fine; if the thread doesn't exist, the function returns null, and I can create the thread myself later, and if it does, it just gets returned to me.
My problem is that if I manually delete one of said threads in discord later, the bot has them saved in cache, and it will still try to write in an unexistent thread.
The only function I could find to refresh the cache is Context.Client.PurgeChannelCache();, but it throws Object reference not set to an instance of an object, even if I check the client for null.
Restarting the bot does fix it, but that's clearly not viable after it gets released.
Is there something else I could try?
For now, I have just made a workaround with a try/catch, where I check if sending the message throws an Unknown Channel error and force the creation once again.
I am pretty sure that this is one of the worst ways of doing it, but it does work, I guess

Error: There was a problem getting the Conversation associated with this room

I'm trying to implement this project: https://github.com/twilio/twilio-video-app-react
I got error "There was a problem getting the Conversation associated with this room." when the ChatProvider did mount. Seem like chatClient can't find the conversation with room.sid but I can't figure out how to create conversation when it doesn't exist and add participants when conversation existed. Does anyone know this issue? Thanks!
Update: the server code: https://gist.github.com/qngnud/c9910afada625a9a4eceb3ad3a67d3b7
Twilio developer evangelist here.
It appears that you are using similar code to the original app in your server, however you have missed a crucial keyword.
The part of the code where you try to fetch a conversation by the room.sid is surrounded by a try/catch, however it is called in promise form, so any failures will be picked up by a chained .catch() function.
In the original code, the calls are made using the await keyword which turns promises into code that appears synchronous and, most importantly, in which errors can be caught by a try/catch block.
So, you should either move your catch blocks to .catch() promise chains, or declare the whole function async and use await before the asynchronous function calls.
I fixed this issue by adding member in the channel before joining the twilio room.
To add member in the channel, I used the api provided by twilio => Click here.
After adding the member in the channel, ChatClient will find the conversation with roomSid.
And I used chatClient.getConversationBySid('CHXXXXXXXXXXXXXXXXXXXXXX') instead chatClient.getConversationByUniqueName(room.sid).
Happy Coding!

Discord.py reacting to most recent message

I need help reacting to the most recent message. This can be in a specified channel or just in the server. The main thing I need help with is getting the message id or info about the most recent message, the reacting part I can do.
Please let me know if there is a solution, as everything I have looked up hasnt produced any results.
Thanks!
There are quite a few methods to get the last message in a channel. Assuming you already have a specific channel in which you want to react. Use
last_message = channel.last_message #channel must be a discord.Channel
The docs specify that this could sometimes get the wrong message, hence we use
messages = await channel.history(limit=1).flatten()[0]
References:
last_message
history
Tip:
Try searching for the relevant class in the docs instead of googling

How to check if a command user has a certain role

I have been trying to make a quick bot for a friend, however I have come across something I cant fix. Whenever I try to access the member from the message it returns null. I am trying to find the users roles so I can check if they have a certain one.
The root of this problem that member is null inside the message. Therefor I cannot read from a null value.
client.on('message', message => {
This is how I am declaring message, and here is a console log of message. https://pastebin.com/vrdg9Wvu
So please can someone help me find a way to compare the command users roles.
Which version of dicord.js are you using?
I don't know where this may come from, maybe if the user who sent the message has left the guild while sending, otherwise I don't know what's wrong. If it's still not working, consider using message.guild.members.cache.get(message.author.id).then(user => {}), that may work.

How to automate topic posting and to clear the chat before hand

I am trying to use discord.js to post a topic at 3 different intervals: every day, every week and every month.
I will have a different file with the topics for each day. I also want each chat room for the separate topics to clear before posting the new topic.
I am using the code below to automate the interval at which it posts but I need the clear function and also maybe a more streamlined way to post it, or a way to post at a specific time.
bot.on('message', function(message) {
// Now, you can use the message variable inside
if (message.content === "$Next") {
var interval = setInterval(function() {
// use the message's channel (TextChannel) to send a new message
message.channel.send("Test Topic")
.catch(console.error); // add error handling here
}, 1 * 10);
}
});
I need to automate these functions and to clear the chat before it posts the new topic. My current code works for automatically posting at the given interval. I need help clearing the channels chat and automatically posting a topic from a saved file.
Use the node-schedule package to help with the defined intervals or specific time.
As for pulling the topic from a file, please take a look at Node's native fs module.

Resources