【问题标题】:How to delete a Discord channel using Python?如何使用 Python 删除 Discord 频道?
【发布时间】:2020-04-18 18:27:27
【问题描述】:
@Bot.command(pass_context= True)
async def complete(ctx):
    guild = ctx.message.guild
    id_admin = discord.Role.id=658306078928273408
    overwrites = {
        id_admin: discord.PermissionOverwrite(send_messages=True),
        guild.me: discord.PermissionOverwrite(read_messages=True),

    }
    await guild.delete_text_channel(discord.TextChannel.name)

我没有在 Discord API 中找到正确的属性。
我应该使用什么属性来删除我编写命令的通道?
Error: AttributeError: 'Guild' object has no attribute 'delete_text_channel'

【问题讨论】:

  • 除了之前的错误,你需要在一个TextChannel对象discordpy.readthedocs.io/en/latest/…上调用delete
  • @AlbertoPoljak GuildChannel 的任何子类都有其对应的delete method。这包括VoiceChannelCategoryChannel
  • @Harmon758 和? delete_text_channeldiscord.TextChannel 足以暗示我告诉我他正在处理 TextChannel。但是我明白你的意思,尽管我不确定 cmets 是了解细节的最佳场所。这更像是我的暗示。

标签: python python-3.x discord.py


【解决方案1】:

@Harmon758 很好地说明了应该如何调用删除命令,但对于不熟悉 discord API 的人来说,这是我处理删除通道请求的方式:

@bot.command(name='delete-channel', help='delete a channel with the specified name')
async def delete_channel(ctx, channel_name):
   # check if the channel exists
   existing_channel = discord.utils.get(guild.channels, name=channel_name)
   
   # if the channel exists
   if existing_channel is not None:
      await existing_channel.delete()
   # if the channel does not exist, inform the user
   else:
      await ctx.send(f'No channel named, "{channel_name}", was found')

【讨论】:

    【解决方案2】:

    您可以将GuildChannel.delete 方法与GuildChannel 的任何子类一起使用。
    您可以使用Context.channel 检索发送邮件的TextChannel

    您不应修改 discord.py 类的属性,而应引用这些类的特定对象/实例的属性。

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 2021-09-21
      • 2021-01-12
      • 2020-12-06
      • 1970-01-01
      • 2021-07-20
      • 2021-05-14
      • 2020-09-11
      • 2020-06-23
      相关资源
      最近更新 更多