繁体   English   中英

Discord.py 公告频道的代码是什么?

[英]Discord.py what is the code for announcement channels?

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

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

I am using Python 3.9.5 and I believe that I am using the latest version of the discord module as when I went to install it again from the command prompt as it said "Requirement already satisfied: discord.py in c:\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)

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

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)

您可以在此处的文档中找到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM