我最近使用discord.py编程,经过几次搜索后,我仍然找不到实现此命令的方法(这也使我在请求来自服务器的信息的其他命令时遇到问题),因此,如果需要您可以。 在这种情况下,我想创建一个向我提供服务器信息的命令。
Traceback (most recent call last):
File "C:\Users\PC GIUSEPPE\PycharmProjects\untitled\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\PC GIUSEPPE\PycharmProjects\untitled\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\PC GIUSEPPE\PycharmProjects\untitled\venv\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'server' is not defined
代码:
@client.command(aliases=["serverinfo","Server_info","Serverinfo","SERVERINFO","si","Si","SI"])
@commands.has_any_role('Moderatori', 'Triumvirato', 'Co-Triumvirato', 'Senatori', '690956686147453048')
async def ServerInfo(ctx):
author = ctx.author.name
name_server = server.name
create_server = server.create
owner_server = server.owner.name
embed = discord.Embed(
title="Informazioni del server",
description=f'Tutte le informazioni generali del nostro server {name_server}',
color=0x003399
)
embed.set_thumbnail(url='')
embed.set_footer(text=f'Richiesto da: {author}')
embed.add_field(
name='Server creato il:',
value='f{create_server}',
)
embed.add_field(
name='Owner Attuale del server:',
value='f{create_server}',
)
embed.add_field(
name='Server creato il:',
value=f'{create_server}',
)
embed.add_field(
name='Server creato il:',
value=f'{owner_server}',
)
答案 0 :(得分:1)
您没有在代码中定义server
,这就是您遇到此错误的原因。
Server
是该机器人所在的Discord服务器的旧discord.py参考。现在,您必须像这样使用Guild
:
@client.command()
async def ServerInfo(ctx):
author = ctx.author.name
guild = ctx.guild
name = guild.name
create_server = server.created_at
owner_server = server.owner.name
(...)
您还可以创建这些信息的字典:
@client.command()
async def ServerInfo(ctx):
author = ctx.author.name
guild = ctx.guild
infos = {
'name': guild.name
'owner': guild.owner.name
'created_at': guild.created_at
}
(...)
如果您真的不知道discord.py(在v1.0之前)和discord.py@rewrite(在v1.0之后)之间的区别,请使用summary link。