I am using discord.py 2.1.0 and I want to get last message in channel
i tried this
@tree.command(guild = discord.Object(id=941748573937209344), name = 'kontrol', description='kontrol')
async def check(ctx):
channels = ctx.guild.text_channels
for channel in channels:
if channel.name.startswith('test-'):
last_message = [msg async for msg in channel.history(limit=1)]
print(last_message)
output :
[<Message id=1060085926157619221 channel=<TextChannel id=1060078850417111112 name='test-supraaaaaa' position=19 nsfw=False news=False category_id=941749835328012328> type=<MessageType.default: 0> author=<User id=323516141777715202 name='supraaaaaa' discriminator='0966' bot=False> flags=<MessageFlags value=0>>]
but there is no date last message created at and it gives this error when getting its id :
@tree.command(guild = discord.Object(id=941748573937209344), name = 'kontrol', description='kontrol') #guild specific slash command
async def check(ctx):
channels = ctx.guild.text_channels
for channel in channels:
if channel.name.startswith('övgü-'):
last_message = [msg async for msg in channel.history(limit=1)]
print(last_message.id)
AttributeError: 'list' object has no attribute 'id'
CodePudding user response:
last_message
is a list. A list does not have the id
property.
What you are trying to do is to access the element in the list. There is only one, in the example you provided, so you should do:
print(last_message[0].id)
It means: In the list named last_message
, access the first element (the Message
object in your case) and from this object, access the id
property in order to print
it to STDIN
.
To get the author's id, it is the same logic:
print(last_message[0].author.id)
Becasue last_message.author
gives you the User
object, which has the id
property.
Concerning the date, it does not appear in the example you provided. I am not familiar with Discord's API, so you'll have to wait for another answer or look at the doc' by yourself.