Discord.py 如何使用机器人将发送的消息?

import discord

from discord.ext import commands

async def anyfunc(ctx):

    await ctx.send(f"{ctx.message.author.mention}, "Hi!")

    bot_message_id = ((bot's_last_message)).id

在 (()) 中放入什么以使机器人记住它将发送的消息的 ID?有没有办法将下一条(例如用户的)消息(尚未发送)的 id 作为变量?


蓝山帝景
浏览 120回答 2
2回答

MMTTMM

ctx.send()返回discord.Message它发送的消息的实例,因此您可以将其存储在变量中。message = await ctx.send(f"{ctx.message.author.mention}, Hi!")# do what you want with the message, in your case getting it's idbot_message_id = message.id至于你的第二个问题,如果你想获得用户对该消息的响应(因此特定用户将发送的下一条消息),你可以使用内置函数wait_for。def check(message):    return message.author.id == the_id_of_that_usermessage = await bot.wait_for("message", check=check)这样,机器人实际上就会发出wait for一条消息,使您的check函数返回True,或者在本例中是由该用户发送的消息。

繁华开满天机

我不太明白你的问题,但我会尽力回答。根据API 参考,您可以使用 获取某人的最后一条消息channel.history().get()。这是一个例子:import discordfrom discord.ext import commandsasync def anyfunc(ctx):    await ctx.send(f"{ctx.message.author.mention}, Hi!")    bot_message_id = await ctx.channel.history().get(author__name="Bot's name").id这可能会起作用,但如果出现错误,也许您可以尝试author__id=bot's id代替author__name.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python