【问题标题】:Discord.py what is the code for announcement channels?Discord.py 公告频道的代码是什么?
【发布时间】:2021-06-06 22:34:00
【问题描述】:

我正在创建一个不和谐机器人,我想创建一个 serverinfo 命令,并且我希望其中一个文本显示服务器中的频道数量。

我遇到的问题是我希望公告通道也被考虑在内,但公告通道是文本通道,所以这意味着它们被添加到文本通道的总数中。我也不知道公告频道的代码是什么,我翻遍了discord.py文档

我正在使用 Python 3.9.5,并且我相信我正在使用最新版本的 discord 模块,因为当我从命令提示符处再次安装它时,它说“要求已经满足:c 中的 discord.py: \users\plant\appdata\local\programs\python\python39\lib\site-packages (1.7.2)"

我想要来自 tatsu bot 的类似的东西

我的频道代码

categories = ctx.guild.categories
text_channels = ctx.guild.text_channels
#announcements_channels = ctx.guild.announcements_channels
embed.add_field(name = f"Channels[{len(categories)+len(text_channels)}]",
                value = f"Category: {len(categories)}\nText: {len(text_channels)}\nNews: ",
                inline = False)

我的 serverinfo 命令代码

@client.command(name = "serverinfo", description = "Shows info about the current server.", aliases=["server"])
async def serverinfo(ctx): 
    embed = discord.Embed(title = f"{ctx.guild.name}", description = ".", colour = white)   
    roleCount = len(ctx.guild.roles)
    bots = [bot.mention for bot in ctx.guild.members if bot.bot]

    embed.set_thumbnail(url = str(ctx.guild.icon_url))
    
    embed.add_field(name = f"ID: **{ctx.guild.id}**", value = "⠀", inline = False)
    embed.add_field(name = "Verification Level", value = str(ctx.guild.verification_level).capitalize(), inline = False)
    embed.add_field(name = "Region", value =  str(ctx.guild.region).capitalize(), inline = False)
    categories = ctx.guild.categories
    text_channels = ctx.guild.text_channels
    #announcements_channels = ctx.guild.announcements_channels
    embed.add_field(name = f"Channels[{len(categories)+len(text_channels)}]",
                    value = f"Category: {len(categories)}\nText: {len(text_channels)}\nNews: ",
                    inline = False)
    embed.add_field(name = "Server Owner", value = str(ctx.guild.owner.mention), inline = False)
    embed.add_field(name = "Created On", value = ctx.guild.created_at.strftime("%a, %#d %B %Y, %I:%M %p UTC"), inline = False)
    embed.add_field(name = f"Roles[{len(ctx.guild.roles)}]", value = "⠀", inline = False)

    embed.set_footer(icon_url = str(ctx.author.avatar_url), text = f"Requested by {ctx.author.name}")

    await ctx.send(embed = embed)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您必须使用for 循环遍历每个频道,并查看它是否是新闻频道。如果是,请将其添加到列表中。如果不是,则表示它是一个普通的文本频道,您可以将其添加到您的文本频道列表中。

    news_channels = 0
    text_channels = 0
    for channel in ctx.guild.text_channels:
      if channel.is_news():
        news_channels += 1
      else:
        text_channels += 1
    

    那么你可以这样做:

    embed.add_field(name = "News channels", value = news_channels)
    

    您可以在文档here 中找到它。

    【讨论】:

    • 非常感谢,您的代码有一点错误,所以我只需要执行 ctx.guild.text_channels 而不是 ctx.guild.channels。
    • @Atrixium 啊抱歉我错过了
    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 2011-10-24
    • 2021-10-02
    • 2021-02-18
    • 2018-06-14
    • 1970-01-01
    • 2022-01-07
    • 2021-03-13
    相关资源
    最近更新 更多