首页 > 解决方案 > (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还是很陌生,所以有人可以告诉我我做错了什么吗?

标签: pythondiscord.py

解决方案


你需要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)

推荐阅读