discord.py I can't run server-info command

Viewed 835

I recently program with discord.py and after several searches I still can't find a way to implement this command (it also leads me to have problems with other commands that request information from the server) and therefore I need help if you can. In this case I want to create a command that gives me the server information.

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

Code:

@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}',
    )
1 Answers

You didn't defined server in your code, that's why you have this error.
Server was the old discord.py reference for the discord server the bot is in. Now, you'll have to use Guild like so:

@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
    (...)

You could also create a dictionnary of those informations:

@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
    }
    (...)

In case you don't really know the difference between discord.py (before v1.0) and discord.py@rewrite (after v1.0), here's a summary link.

Related