Discord.py reacting to most recent message - discord

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

Related

Nextcord: I want to mention the sender of the slash command I made. How would I go about that?

I'm trying to make this command notify my admins of a bot test and need it to mention the user who called the command. How would I go about that? I don't fully understand how to get that information with slash commands.
#client.slash_command(name= "test", description="(For Deputies or Leader only) Checks the operational state of the client.", guild_ids=[806043206030589952])
#has_any_role(leader_id, deputy_id)
async def test(interaction:Interaction):
bot_log = channel_up(940125176723554394)
await bot_log.send(f'<#&806045254834847776>,{} has started diagnostics for the bot. Please ignore any possible disturbances for the next minute or so.')
Thanks in advance for the advice, it's the first discord bot I've ever created.
EDIT:
In documentation, I found the solution. I have to use interaction.user.mention to get it to mention the user who sent the command. Or at least in theory, I'm dealing with a different issue now. Hopefully this helps people who also were as confused as me out.
You can use the interaction.user.name function with an # at the beginning.
The code should look something like this:
await interaction.response.send_message("#"+str(interaction.user.name))

Discord.py adding reaction to webhook.send message

I am able to send messages to my discord channel via webhook very easily, however, when trying to add reactions to the messages it's very difficult, considering my programming is still trivial.
The code line I have right now is:
data_count=1
webhook.send(content=discord.Reaction(message="test", data=data_count, emoji="👍"), file=discord.File("american_eagle_excited.gif"), embed=discord.Embed(title="Sample Embed", description="This is the description"))
Everything, when I break down the parameters I can get to work besides the discord.Reaction class. I feel like I am missing something very easily and after trying to read through the class requirements I had to finally make my way to StackOverflow.
To add a reaction you need the discord.Message instance which is returned by the webhook.send method, you then add the reaction with message.add_reaction
message = webhook.send("test", wait=True, ...) # `wait=True` is really important, if we don't set this the bot will not wait for the message to be returned
await message.add_reaction("👍")

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!

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.

Fetch old reactions to an old message

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.

Resources