OnPoint
OnPoint

Reputation: 139

I dont know how to use fetch_message correctly

I dont know where to put the Coroutine "fetch_message" I want to edit a message, but it doesnt work the old way

I tried the old way, but it told me it got 2 positional arguments instead of 1...

creator = ctx.message.author.id
await channel.send(f"<@{creator}>", embed=embed)

message_channel = ctx.message.channel
destruction_message = await message_channel .send("Self destruct in 3")
message = await client.fetch_message(destruction_message)
time.sleep(1)
print(message)
print(destruction_message)

await message.edit("Self destruct in 2")
message.edit()
time.sleep(1)
await message.edit("Self destruct in 1")
time.sleep(1)
await message.delete()
message = ctx.message
await client.message.delete(message )

It just tells me "AttributeError: module 'client' has no attribute 'fetch_message'"

This code is part of a embed message, everything including The "Self destruct in 3" gets posted, but the editing fails...

Upvotes: 0

Views: 5977

Answers (2)

C. Mayer
C. Mayer

Reputation: 423

The fetch_message method is an Abstract Base Class, more precice a Messageable.

According to the documentation

An ABC that details the common operations on a model that can send messages.

The following implement this ABC:

• TextChannel
• DMChannel
• GroupChannel
• User
• Member
• Context

This means you can call fetch_message with an object of any of those classes.

In your case you can call it directly on the context await ctx.fetch_message(id).

A more general example starting only with ids would be:

    # taken out of a on_raw_reaction_add() method
    # assuming self.client is the bot

    guild = self.client.get_guild(payload.guild_id)
    channel = guild.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)

However, for your case you should use BrainDeads suggestion

Upvotes: 3

BrainDead
BrainDead

Reputation: 795

I'm not sure why do you want to fetch message when you already got the message object with

message = await nachrichtchannel.send("self destruct in 3")

You can just call edit on that object, you don't have to call the fetch_message

For your edit error you have to pass content as an argument. Seems like you are trying to do a counter so here's an example:

 @commands.command()
 async def counter(self, ctx):
     message = await ctx.channel.send("Timer 3")
     await asyncio.sleep(1)

     await message.edit(content="Timer 2")
     await asyncio.sleep(1)

     await message.edit(content="Timer 1")
     await asyncio.sleep(1)

     await message.delete()

You should use await asyncio.sleep(1) instead of time.sleep(1) to avoid any code hangups.

Upvotes: 2

Related Questions