python - 机器人如何对自己的消息做出反应?
问题描述
我现在正在开发自己的机器人,它最终应该会执行更多功能。但一开始它应该对自己的消息做出反应。但是,这似乎不起作用,因为错误一次又一次地发生。
我不断收到以下错误:
Ignoring exception in command create:
Traceback (most recent call last):
File "D:\Python\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "D:\PythonCode\DiscordBot\method2.py", line 15, in create
msg = await bot.send_message(message.channel,embed=createEmbed)
AttributeError: 'Bot' object has no attribute 'send_message'"
我不知道问题是什么,也无法找到解决方案。
我的代码如下所示:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = 'pp!')
@bot.event
async def on_ready():
print('bot is now active and logged into {0.user}'.format(bot))
@bot.command()
async def create(ctx):
createEmbed = discord.Embed(title='When would you like me to remind you?', description=':regional_indicator_a: Every 10 minutes\n:regional_indicator_b: Every 30 minutes\n:regional_indicator_c: Every hour')
emojis = ['\n{regional indicator a}', '\n{regional indicator b}', '\n{regional indicator c}']
msg = await bot.send_message(message.channel,embed=createEmbed)
await bot.add_reaction(msg, '')
解决方案
正如评论中提到的RufusVSsend_message
: is not part of the class commands.Bot
。相反,我们发送嵌入,await ctx.send()
然后通过定义您发送的消息对其做出反应。
看看下面的代码:
@bot.command()
async def create(ctx):
createEmbed = discord.Embed(title='YourTitle', description='YourDescription') # Embed
msg = await ctx.send(embed=createEmbed) # Define the message that the bot sends
await msg.add_reaction("YourReaction") # Add your reaction
还请再次查看文档。
推荐阅读
- java - 如何在 Spring Boot 中配置自定义身份验证和授权?
- python-3.x - 在继续第一个之前运行第二个 python 脚本
- html - 背景图像未完全显示在平板电脑中
- jquery - 带有 jquery 的变体产品脚本
- python - django中后台任务处理的简单方法
- tensorflow - 如何使用带有批量标准化层的预训练 keras 模型?
- r - R中具有多个电子邮件收件人的网络分析
- javascript - 动态删除localstorage中的li项
- azure-cosmosdb - Cosmos DB ExecuteStoredProcedureAsync 未返回响应
- java - 组件之间的 Java swing 通信