我如何使用discord.py来获取当前服务器内部所有类别和频道的列表?

时间:2020-08-04 02:47:35

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

我想要一个输出,该输出将在机器人所在的服务器内部输出所有(类别名称和ID +通道名称和ID)。

代码可能看起来像这样:

    # catetories
    @client.command()
    async def list_catetories(ctx):
      for category in discord.categories:
        print('id: ' + category.id + ', name: ' + category.name)

    # channels
    @client.command()
    async def channels(ctx):
      for channel in discord.channels:
        print('id: ' + channel.id + ', name: ' + channel.name)

解决方案

# categories
@client.command()
async def cat(ctx):
    for category in ctx.message.guild.categories:
        print(category.name)

# text channels
@client.command()
async def txtChannel(ctx):
    for text_channel in ctx.message.guild.text_channels:
        print(text_channel.name)

#voice channels
@client.command()
async def vc(ctx):
    for voice_channel in ctx.message.guild.voice_channels:
        print(voice_channel.name)

#all channels + categories
@client.command()
async def channels(ctx):
    for channel in ctx.message.guild.channels:
        print(channel.name)

1 个答案:

答案 0 :(得分:0)

有多种渠道类型,因此以下代码将显示all个渠道。如果要按类型进行过滤,则应检查其类型

@client.command()
async def channels(ctx):
  for channel in ctx.guild.channels:
    print(f"Name: {channel.id} Id: {channel.id}")