我想发出一个命令,删除通道discord.py

我想发出一个命令,删除通道discord.py,discord.py,Discord.py,这是我到目前为止得到的 这是说“wait”在异步函数之外,我只是对如何解决这个问题感到困惑。我从一个赠品命令中复制了部分代码,因为这是我唯一的问答代码源 @commands.has_permissions(administrator = True) async def cdelete(ctx): embed=discord.Embed(title = Which channel would you like to delete?) await ctx.send(embed=embed)

这是我到目前为止得到的 这是说“wait”在异步函数之外,我只是对如何解决这个问题感到困惑。我从一个赠品命令中复制了部分代码,因为这是我唯一的问答代码源


@commands.has_permissions(administrator = True)
async def cdelete(ctx):
  embed=discord.Embed(title = Which channel would you like to delete?)
await ctx.send(embed=embed)

answer = []

def check(m):

    return m.author == cx.author and m.channel == ctx.channel

    try:
        await client.wait_for('message', timeout=15, check=check)
    except asyncio.TimeoutError:
        await ctx.send("Timeout. Please run the command again")
    else:
        answer.append(message.content)
    try:
        c_id= int(answer[0][2:-1])
        await ctx.channel.delete
        await ctx.send("Channel Deleted")
    except:
        await ctx.send("Thats not a valid channel!")```


这里的问题是
check()
函数。根据文档,您只能在异步函数内部使用wait。要解决此问题,请将
def check(m):
更改为
async def check(m):

如果您只需要一个命令,如果您只需要执行一个具有通道名称的命令,您就可以轻松得多。您可以查看以下内容:

@client.command()
@commands.has_权限(administrator=True)
异步定义cdelete(ctx,通道名称):
“”“按名称或ID删除频道。”
channel_id=int(“”.join(如果i.isdigit(),则在channel_名称中i代表i))#获取通道id
现有_通道=客户端。获取_通道(通道id)#使用定义的方法获取通道id
if existing_channel:#如果存在名为/ID的频道
等待现有的_通道。删除()
否则:#如果该频道不存在
wait ctx.send(f'**未找到名为`{channel_name}`的频道。**'))
我们有一个方法来捕获通道ID,并将其作为有效参数传入。如果ID或通道名称存在,我们将删除该通道

您可以在此处提及频道或传入ID。 用法是:
cdelete#channel/ID

如果要避免在未输入通道/ID/名称时出现长控制台输出,可以在错误处理程序中生成:

@cdelete.error
异步定义cdelete_错误(ctx,错误):
如果isinstance(错误,commands.MissingRequiredArgument):
等待ctx.send(“您需要命名一个频道。”)

检查功能中没有等待,也不能是协同程序,等待将不会等待,因此它将不起作用谢谢我会尝试一下,seedid似乎不起作用,谢谢你的努力我认识你,谢谢你的工作,但是,关于channel_name的一个小错误消息是必需的缺少参数,但感谢您的帮助如果您不传入ID、channel name或channel name,则会显示错误消息,无需担心。(如果您只是运行
cdelete
它将向您显示该消息)那么我是否应该添加cdelete.error?我将编辑我的答案。