Home>

I'm developing a bot with Discord.py. I want to understand how to write a function that will itself determine the name of the server on which the bot is located.

  • Answer # 1

    In class Guild, there is an attribute that returns the name of the server.

    Example1:

    from discord.ext import commands
    bot= commands.Bot()
    # Suppose we have a command *name*
    @bot.command()
    async def name(ctx: commands.Context):
        # Get the name of the server
        guild_name= ctx.guild.name
        # Do something next
        ...
    

    Example2:

    from discord import
    Guild
    from discord.ext import commands
    bot= commands.Bot()
    def get_guild_name(guild_id: int):
        # "Getting" the server
        guild: Guild= bot.get_guild(guild_id)
        # Server name
        guild_name= guild.name
        return guild_name
    
  • Answer # 2

    In class Guild, there is an attribute that returns the name of the server.

    Example1:

    from discord.ext import commands
    bot= commands.Bot()
    # Suppose we have a command *name*
    @bot.command()
    async def name(ctx: commands.Context):
        # Get the name of the server
        guild_name= ctx.guild.name
        # Do something next
        ...
    

    Example2:

    from discord import
    Guild
    from discord.ext import commands
    bot= commands.Bot()
    def get_guild_name(guild_id: int):
        # "Getting" the server
        guild: Guild= bot.get_guild(guild_id)
        # Server name
        guild_name= guild.name
        return guild_name