【发布时间】:2020-11-17 17:44:59
【问题描述】:
我想为我的不和谐机器人添加服务器信息命令,如果有人有它,请提交代码:)。
【问题讨论】:
-
How to ask good question? 你应该看看这个。
标签: python command discord discord.py
我想为我的不和谐机器人添加服务器信息命令,如果有人有它,请提交代码:)。
【问题讨论】:
标签: python command discord discord.py
如果其他人正在寻找一个好的 serverinfo 命令,这就是我写的。
format = "%a, %d %b %Y | %H:%M:%S %ZGMT"
@client.command()
@commands.guild_only()
async def serverinfo(ctx):
embed = discord.Embed(
color = ctx.guild.owner.top_role.color
)
text_channels = len(ctx.guild.text_channels)
voice_channels = len(ctx.guild.voice_channels)
categories = len(ctx.guild.categories)
channels = text_channels + voice_channels
embed.set_thumbnail(url = str(ctx.guild.icon_url))
embed.add_field(name = f"Information About **{ctx.guild.name}**: ", value = f":white_small_square: ID: **{ctx.guild.id}** \n:white_small_square: Owner: **{ctx.guild.owner}** \n:white_small_square: Location: **{ctx.guild.region}** \n:white_small_square: Creation: **{ctx.guild.created_at.strftime(format)}** \n:white_small_square: Members: **{ctx.guild.member_count}** \n:white_small_square: Channels: **{channels}** Channels; **{text_channels}** Text, **{voice_channels}** Voice, **{categories}** Categories \n:white_small_square: Verification: **{str(ctx.guild.verification_level).upper()}** \n:white_small_square: Features: {', '.join(f'**{x}**' for x in ctx.guild.features)} \n:white_small_square: Splash: {ctx.guild.splash}")
await ctx.send(embed=embed)
该命令将显示服务器 ID、所有者、位置、创建日期、成员、频道、验证、功能和启动画面。
【讨论】:
我是这样写的
@commands.has_any_role('Owner', 'Head Dev', 'Head Admin', 'Admins', 'Moderator', 'Community Helper', 'Team Leader', 'Head Team Member')
async def serverinfo(self, ctx):
role_count = len(ctx.guild.roles)
list_of_bots = [bot.mention for bot in ctx.guild.members if bot.bot]
staff_roles = ["Owner", "Head Dev", "Dev", "Head Admin", "Admins", "Moderators", "Community Helpers", "Members"]
embed2 = discord.Embed(timestamp=ctx.message.created_at, color=ctx.author.color)
embed2.add_field(name='Name', value=f"{ctx.guild.name}", inline=False)
embed2.add_field(name='Owner', value=f"Mekasu, Kastien", inline=False)
embed2.add_field(name='Verification Level', value=str(ctx.guild.verification_level), inline=False)
embed2.add_field(name='Highest role', value=ctx.guild.roles[-2], inline=False)
embed2.add_field(name='Contributers:', value="None")
for r in staff_roles:
role = discord.utils.get(ctx.guild.roles, name=r)
if role:
members = '\n'.join([member.name for member in role.members]) or "None"
embed2.add_field(name=role.name, value=members)
embed2.add_field(name='Number of roles', value=str(role_count), inline=False)
embed2.add_field(name='Number Of Members', value=ctx.guild.member_count, inline=False)
embed2.add_field(name='Bots:', value=(', '.join(list_of_bots)))
embed2.add_field(name='Created At', value=ctx.guild.created_at.__format__('%A, %d. %B %Y @ %H:%M:%S'), inline=False)
embed2.set_thumbnail(url=ctx.guild.icon_url)
embed2.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
embed2.set_footer(text=self.bot.user.name, icon_url=self.bot.user.avatar_url)
channel = self.bot.get_channel(staff_commands)
await channel.send(embed=embed2)
【讨论】:
我使用了这段代码,它应该可以满足您的需求。虽然我更喜欢更详细的问题,但这样我可以以更好的格式回答。
guild 变量可以描述服务器或使用命令的地方。
然后你只需添加你的东西,比如服务器 id 等等。
我认为您也可以在列表中添加角色,但现在,这是我使用的代码。
@client.command()
async def serverinfo(ctx):
name = str(ctx.guild.name)
description = str(ctx.guild.description)
owner = str(ctx.guild.owner)
id = str(ctx.guild.id)
region = str(ctx.guild.region)
memberCount = str(ctx.guild.member_count)
icon = str(ctx.guild.icon_url)
embed = discord.Embed(
title=name + " Server Information",
description=description,
color=discord.Color.blue()
)
embed.set_thumbnail(url=icon)
embed.add_field(name="Owner", value=owner, inline=True)
embed.add_field(name="Server ID", value=id, inline=True)
embed.add_field(name="Region", value=region, inline=True)
embed.add_field(name="Member Count", value=memberCount, inline=True)
await ctx.send(embed=embed)
附言我是 python 和 discord.py 的初学者,如果这没有帮助,我很抱歉。
【讨论】:
https://discordpy.readthedocs.io/en/latest/api.html#guild 您可以通过以下方式获取公会(不和谐服务器):
client.get_guild(guild_ID)
然后您可以使用 guild.name、guild.icon、... 获取所有不同的信息。
【讨论】: