Discord - 使用 python 显示特定类别的频道名称

时间:2021-04-19 10:18:16

标签: python discord

如果有人向机器人发送消息,例如:如果 xyz 向机器人发送 !vip 消息,那么他会回复 all来自“VIP”类别的文本频道名称?我找不到这样的主题,只是计算特定类别中有多少文本频道等等......

我确实设置了基本的机器人(在 stackoverflow 上也找到了),如果您将 !hello 发送给机器人并且它正在工作,但我不知道如何做我的回复上面提到了。

import discord

TOKEN = 'yourtoken'

client = discord.Client()

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        msg = 'Hey, {0.author.mention}'.format(message)
        await message.channel.send(msg)

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
client.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

如评论中所述,您必须为此使用命令。为此,您可以使用 CategoryChannel.channels 和或 guild.categories

这会给出有关整个类别的信息,如下所示: [<TextChannel id=ChannelID name='Allgemein' position=0 nsfw=False news=False category_id=CategoryID>]

要仅显示名称,请使用以下内容:

text_channel_list = [] # "Create" a list

@client.command()
async def comm(ctx, *, category: CategoryChannel):
    for channel in category.text_channels: # Get text channel
        text_channel_list.append(channel.name) # Append them to the list
    await ctx.send(f"\n".join(text_channel_list)) # Send the channels in one message

为了只接收一条消息,我们必须将 await 语句放在 for 循环函数之外。

有关详细信息,请阅读docs