python - Discord bot上的功能已定义错误
问题描述
通过添加新命令从第一个 Discord 机器人构建。在创建第二个命令时,我之前在代码中收到错误“函数已定义”,即使它位于不同的标题下。
@client.command()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
My first command above, worked fine.
@client.command()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!roll'):
msg = 'So, {0.author.mention} you rolled for a {}'.format(random.randint(1,20))
await client.send_message(message.channel, msg)
第二个有一个错误,说之前已经在第 10 行定义了异步(在第一个函数中)
解决方案
通过使用def
,您正在定义一个函数。一个函数必须是唯一的,并且不能用相同的名称定义两次。
解决办法可以
@client.command()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
elif message.content.startswith('!roll'):
msg = 'So, {0.author.mention} you rolled for a {}'.format(random.randint(1,20))
await client.send_message(message.channel, msg)
推荐阅读
- c++ - 如何在非轴对齐框(任意旋转的框)内找到点的相对偏移量
- javascript - 返回函数在我的代码中不起作用导致无限循环
- javascript - 从雅虎财经的交互式图表中抓取数据的有效方法
- python - pyqt5如何修复VBox中的图像大小?
- php - 按钮调用控制器功能/方法
- c++ - GL_TEXTURE_2D_MULTISAMPLE 未在此范围内声明 OpenGL 4.6
- windows - 如何在 Windows 上使用 Fiddler 监控 docker push 生成的 HTTP 流量?
- excel - excel日期格式不起作用mm / dd / yyyy到yyyy-mm-dd
- winapi - 为什么 MediaFoundation 的 MediaEngine 可能会在第二次调用播放时播放视频,而不是第一次?
- node.js - 在运行 HTTPS 的 IIS 上运行 Nodejs 和 React 时遇到问题