0

I am trying to get the bot information via the token in Discord.py, but I do not know if it is possible. I am trying to get the bot name, how many servers, and the client ID in the console.

I have tried looking for other explinations, but they did not work or were for discord.js

Carson
  • 1

1 Answers1

-1

Yes. Please read the docs thoroughly before asking related questions here. You can get information about the bot using bot.application_info(), or bot.application (which I can't get working).

I don't know what you mean by "bot name"; here's how to get them "in the console":

print(f"I'm in {len(bot.guilds)} server{'s' if len(bot.guilds) != 1 else ''}.")
print(f"bot.user.name (display name without number tag): {bot.user.name}")
print(f"bot.user.display_name (display name without number tag): {bot.user.display_name}")
print(f"bot.user.mention (unique @ mention string): {bot.user.mention}")
print(f"bot.user (username with number tag): {bot.user} OR {bot.user.display_name}#{bot.user.discriminator}")

app_info = await bot.application_info()

#print(app_info == bot.application) (would return True)

print(f"bot.application_id: {bot.application_id}")
print(f"<AppInfo>.id: {app_info.id}")
print(f"<AppInfo>.name (display name without number tag): {app_info.name}")

As you'll see, bot.user.name, bot.user.display_name and <AppInfo>.name are the same. bot.user returns the full username, with the number tag (bot.user.discriminator). Choose which name you want to use :D

bot.application_id and <AppInfo>.id are also same. bot.user.mention is also the same but returns a string in the <@id> format, which, when sent, pings the user (in this case, the bot itself), unlike the other two which return just the integer ID.

I've commented out a line, as I can't get bot.application to work, as no matter what I try, my version's Bot and Client classes don't have that attribute.

The docs about bot.application; use it if you can figure out how to:

The client’s application info.

This is retrieved on login() and is not updated afterwards. This allows populating the application_id without requiring a gateway connection.

This is None if accessed before login() is called.

See also

The application_info() API call

New in version 2.0.

Type:

Optional[ AppInfo ]

Here's a full example of where and how you can use the code:

from discord.ext import commands
import discord.utils

intents = discord.Intents.all()

bot = commands.Bot(command_prefix="", description="", intents=intents)


@bot.event
async def on_ready():
    print("I'm online.") #logged in; can get application info
    print(f"I'm in {len(bot.guilds)} server{'s' if len(bot.guilds) != 1 else ''}.")
    print(f"bot.user.name (display name without number tag): {bot.user.name}")
    print(f"bot.user.display_name (display name without number tag): {bot.user.display_name}")
    print(f"bot.user.mention (unique @ mention string): {bot.user.mention}")
    print(f"bot.user (username with number tag): {bot.user} OR {bot.user.display_name}#{bot.user.discriminator}")
    
    app_info = await bot.application_info()
    
    #print(app_info == bot.application) #would return True
    
    print(f"bot.application_id: {bot.application_id}")
    print(f"<AppInfo>.id: {app_info.id}")
    print(f"<AppInfo>.name (display name without number tag): {app_info.name}")
    
bot.run("<token>")

I've put the code inside on_ready(), as you can get the application info there. I've used the discord.ext.commands.Bot class instead of the discord.Client one, as the former is a superset of the latter and has more features, so you'd want to use it.

docs for discord.Client

docs for discord.ext.commands.Bot

The Amateur Coder
  • 789
  • 3
  • 11
  • 33