I know that I can get a message object using await ctx.fetch_message(mesId). Although If I send a message, and then start the bot's session (Restart the Client). The script cannot see the message. Is there any way to get rid of this problem?
Also, it's worth mentioning that I use discord.Bot type user not discord.Client
First of all there is no ctx.fetch_message ref
It should be ctx.channel.fetch_message Keep in mind you can get another channel by using await bot.get_channel(ID) and then fetch the message.
Your code should look like this:
# using another channel
channel = await bot.get_channel(123456)
message = await channel.fetch_message(123456)
# or using ctx
message = await ctx.channel.fetch_message(123456)
Docs
Related
In Discord.py I am currently coding a bot that basically acts as a system for one particular server, and isn't intended to be used as a public bot. But I still wanted to add commands in that makes it simple for administrators to configure the bot inside of Discord, all was well, until I ran into the issue of trying to check if a channel ID is actually correct or not when executing the command, but unfortunately I kept getting error after error after error.
#bot.command()
async def channel(ctx, type, id):
global channel_report
global channel_approve
if id != discord.TextChannel.id:
await ctx.send("Command terminated: bad id.")
return
if type == "report":
#code here
elif type == "approve":
#code here
What have I been missing? I have tried to approach this many different ways, even with methods such as get_message , but got nowhere, and as a newish programmer, but especially new to the Discord API in particular, I'm lost. Thanks for all of those who are dedicating their time to help me, computer coding is just one of them interests.
I'm assuming what you mean by "checking if a channel ID is correct" is that you want to make sure the given ID represents an actual channel that the bot can access. You can use bot.get_channel() to get a channel object from an ID. If the channel doesn't exist, it returns null. So you can just check if it is null.
channel = bot.get_channel(000000000000000000) # try to get a channel which doesn't exist
if channel is None: # will return true as invalid channel returns null
await ctx.send("Command terminated: bad id.")
return
Is there a way to let the bot send a message and get the ID of the message, so you can delete it a few seconds later?
ps. discord.py
The only possible way I found was, to just delete the last sent message in the channel.
there is two Q here:
yes there is a way to get the id for example:
message = await ctx.send("hi")
message_id = message.id
to delete a message a few seconds later you can use delete_after
for example:
await ctx.send("hi",delete_after = 10)
The channel.send() method has a kwarg exactly for this purpose, delete_after. You set it's value when sending a message and it will automatically delete the message after that time (in seconds). Here's the docs for the channel.send coroutine(/function) https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.send
How can a python bot read every message that is sent in a certain channel or read every message that was sent in one using a command like $save 421345187663708161. Thanks in advance, havent been able to find the answer anywhere.
I made this in an on_message Function which scans the message content for "$save" on the beginning, then gets the channel per the given ID.
#client.event
async def on_message(message):
if message.content.startswith("$save"):
splittedcontent = message.content.split()
channel = client.get_channel(splittedcontent[1])
all_messages = channel.history()
What your job now is, is to understand this code, maybe inform about the things that are going on here (like channel.history) and implement this to your code (preferably also with some try/except cases).
I have made a discord bot that has the saves the id of a specific msg on a JSON file and when someone reacts to the msg he sends a message back.
My problem is when I do something which is going to turn the bot off for a period of time (restart, update, change host and more), the bot "forgets" all the messages that were made before he was activated again.
Is there a way to make the bot remember all the messages that happened before he was turned off?
(I tried channel.history loop and the bot did find his previous made message (I tested if the id is the same)
but after I tried to react to the message again, the bot still didn't recognize that a reaction has been made because the message was made before he was turned on.
side note:
There is a work around you can do that instead of trying to remember the message id, you need to remember the channel id that the msg has been sent in as well.
So after you have found your previous message, you can delete it and make a new message.
The thing is, I wanna know if there is a rather easier way to do it or if there is a way to "remember" previous messages.
So what I would do is I would save the messages into an SQL database and on start up recover all the messages.
Like so:
# Start up (run this code first)
try:
with open(path, "x") as file: pass
except: pass
global conn, c
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS Messages (
messages string NOT NULL
)""")
# Gets message history
c.execute("SELECT * FROM Messages")
history = c.fetchall()
This will not work unless you save the messages when you receive them, so add these lines once you receive a message you want to be able to get later
# Saves message
c.execute("INSERT INTO messages VALUES (?)", (message,))
conn.commit()
I'm trying to response to user on subscribe. By example, in a chatroom when an user connect to subscription, the subscription responses him with data (like a welcome message), but only to same user who just connect (no broadcast).
How can I do that? :(
Update: We resolve to use channels. DjangoChannelsGraphqlWs does not allow direct back messages.
Take a look at this DjangoChannelsGraphQL example. Link points to the part which is there to avoid "user self-notifications" (avoid user being notified about his own actions). You can use the same trick to send notification only to the user who made the action, e.g. who just subscribed.
Modified publish handler could look like the following:
def publish(self, info, chatroom=None):
new_msg_chatroom = self["chatroom"]
new_msg_text = self["text"]
new_msg_sender = self["sender"]
new_msg_is_greetings = self["is_greetings"]
# Send greetings message only to the user who caused it.
if new_msg_is_greetings:
if (
not info.context.user.is_authenticated
or new_msg_sender != info.context.user.username
):
return OnNewChatMessage.SKIP
return OnNewChatMessage(
chatroom=chatroom, text=new_msg_text, sender=new_msg_sender
)
I did not test the code above, so there could be issues, but I think it illustrates the idea quite well.