python - Discord.py Bot - `if` 命令和`async def` 有什么区别?
问题描述
我见过许多开发人员以两种方式为 Discord 编写 Python 机器人。
其中一些使用:
if message.content.startswith("command"):
await message.channel.send("text")
而其他人(大多数)使用这种方法:
@client.command()
async def command(ctx):
response = "Text"
await ctx.send(response)
它们两者之间有什么区别,哪种方法更好/更有效率?
解决方案
使用@client.command()
andasync def
意味着这些是 a 内的可调用函数coroutine
,而if
我认为 需要嵌套在某种循环中。
当您client.RUN(TOKEN)
启动机器人时,Discord 模块会启动一个asyncio coroutine
. 它使用async
并await
允许异步任务管理,请参阅有关协程的 Python 文档和有关 wait_for 事件的 Discord.py API 参考
推荐阅读
- xml - 在定义的字符串之后查找特定字符串
- python - 如何堆叠for循环的每次迭代的输出?
- windows - 在不重新启动 Windows 的情况下更新 KMDF 驱动程序
- php - 在 php 中收到 FormData 时导致错误
- raspberry-pi - 如何在树莓派上卸载motioneye?
- firebase - Crashlytics - Firebase 日志中的 Stacktrace 不可读
- react-native - Expo Audio保持ActiveInBackground不工作
- c++ - AVX2 SIMD Instrinsics 16 位到 8 位,反之亦然
- python - 这个答案的python 3版本是什么?
- java - 如何让程序等到计时器线程完成后再继续?