python - (discord.py) 齿轮中的函数
问题描述
我一直在尝试在 python 中开发一个不和谐的机器人,我想制作一堆不会出现在帮助中的“隐藏”命令。我想这样做,以便每当有人激活隐藏命令时,机器人都会向他们发送 pm。我试图制作一个功能来做到这一点,但到目前为止它不起作用。这是cog文件中的代码:
import discord
from discord.ext import commands
class Hidden(commands.Cog):
def __init__(self, client):
self.client = client
def hidden_message(ctx):
ctx.author.send('You have found one of several hidden commands! :shushing_face:\nCan you find them all? :thinking:')
@commands.command()
async def example(self, ctx):
await ctx.send('yes')
hidden_message(ctx)
def setup(client):
client.add_cog(Hidden(client))
运行示例命令时,机器人正常响应,但未调用该函数。控制台中没有错误消息。我对python还是很陌生,所以有人可以告诉我我做错了什么吗?
解决方案
你需要await
在调用异步函数时使用ctx.author.send
,所以你包装它的函数也需要是异步的
async def hidden_message(self, ctx):
await ctx.author.send('You have found one of several hidden commands! :shushing_face:\nCan you find them all? :thinking:')
进而
@commands.command()
async def example(self, ctx):
await ctx.send('yes')
await self.hidden_message(ctx)
最后,要使命令从默认帮助命令中隐藏,您可以这样做
@commands.command(hidden=True)
推荐阅读
- powershell - PowerShell 中 PromptForChoice 的奇怪行为
- time-complexity - T(n)的复杂度= 8T[(n+3)/5]+T[((n+3)/5)-3]+196.n/5+336?
- bootstrap-4 - 引导程序中的margin-left 5px?
- java - 尝试使用我从 firestore 获得的数据在 android studio 的 calendarwidget 中添加一个事件并将它们显示在 recyclerview 上
- reactjs - 更新 npm 后 npm run build 失败(EACCES:权限被拒绝)
- red - 如何解压缩/放气 zlib 数据 [rfc1951]?
- json-server - JSON-Server Heroku 子目录
- go - 进行批量调用时http请求中的Golang上下文
- java - 如何让一个类在 Java 中识别另一个类
- flutter - 在 ListTile 上自定义前导、尾随和添加阴影