【问题标题】:How do i delete a channel in discord.py having its id?如何删除 discord.py 中具有其 ID 的频道?
【发布时间】:2022-04-05 22:24:34
【问题描述】:

如何删除 discord.py 中具有其 ID 的频道? 频道是指语音、文本和类别频道。 我尝试使用Guild.channel.delete(channel_id),但它既不工作也不抛出错误

@client.command()
async def removechannel(ctx, channel_id):
    removeChannel(channel_id)

removeChannel 只会删除带有 ID 的频道)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    如果您将参数类型设置为TextChannel,您可以在命令中提及它,而不必编写 ID,尽管 ID 也可以使用 - !removechannel #general
    TextChannel 对象有一个 delete() 方法,您可以像这样使用它:

    @client.command()
    async def removechannel(ctx, channel: discord.TextChannel):
        await channel.delete()
        await ctx.send("Successfully deleted the channel!")
    

    如果需要,您也可以通过 ID 使其工作(对于语音通道,因为您不能提及它们):

    @client.command()
    async def removechannel(ctx, channel_id: int):
        channel = client.get_channel(channel_id)
        await channel.delete()
        await ctx.send("Successfully deleted the channel!")
    

    参考资料:

    【讨论】:

    • 我如何删除类别频道?
    • Client.get_channel() 方法涵盖了这一点 - 如果您只输入类别的 ID,那么它将起作用。 (使用第二个例子——第一个纯粹用于文本通道)
    • 对我来说,它不适用于类别频道 - (已解决)
    猜你喜欢
    • 2018-09-10
    • 2021-12-21
    • 2021-10-04
    • 2021-01-25
    • 2021-11-08
    • 2018-12-08
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多