discord bot python 删除频道出错

discord bot python delete channel gives an error

我试图创建一个删除针对核弹机器人的核弹频道的命令, 这是我的代码:

async def inferno(ctx,problem):
    if problem == ("channelnuke"):
        channel == ("Nuke-channel")
        await ctx.send("okay solving problem")
        await ctx.send("Guard on, every nuke channel will be instant deleted")
        while True:
            
            await channel.delete()

但是报错discord has no attribute delete

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: module 'discord.channel' has no attribute 'delete'

有谁知道应该怎么做吗?

async def inferno(ctx, problem):
    if problem == "channelnuke":
        channel == ctx.channel
        await channel.delete()
        await ctx.send("okay solving problem")
        await ctx.send("Guard on, every nuke channel will be instant deleted")

遗憾的是,由于我的声誉不足,我无法对之前的答案发表评论。所以这是上一个答案的更新代码,现在应该可以使用了。

@client.command()
async def inferno(ctx, problem):
    if problem == "channelnuke":
        channel = ctx.channel   # the previous answer had put (==) instead of (=)
        await ctx.send("okay solving problem")
        await ctx.send("Guard on, every nuke channel will be instant deleted")
        await channel.delete()

如果你想添加频道,你可以通过将频道作为可选参数来实现:

@client.command()
async def inferno(ctx, problem, channel : commands.TextChannelConverter = None): # we will put the channel into an optional arguments by doing = None
    if channel == None: # it will find the channel if you didn't specify the channel
        channel = ctx.channel
    await ctx.send("okay solving problem")
    if problem == "channelnuke":
        await channel.delete()

TextChannelConverter 将查看频道,您可以使用 id、频道名称或提及频道,它应该可以工作,下面将提供更多信息。

TextChannelConverter Docs

稍后谢谢我:D