Search code examples
pythondiscorddiscord.py

Nothing is being stored in my "username" variable


Attached below is my code, I've tried adding print statements to troubleshoot but nothing is coming through. This program is supposed to run as a discord bot by the command !roblox (username). It then takes the username and check's it's rank in a group, friends, account age, all through Roblox's API.

import discord
import requests

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!roblox'):
        # Get the username from the user input
        username = message.content.split('!roblox ')[1]
        print(username) 
        # Make a request to the Roblox API to get the user's data
        url = f"https://users.roblox.com/v1/users/search?keyword=    {username}&limit=10"
        response = requests.get(url)
        data = response.json()

        # Check if the user exists
        if "data" in data and data["data"]:
            user_id = data["data"][0]["id"]

            # Get the user's account age
            url = f"https://users.roblox.com/v1/users/{user_id}"
            response = requests.get(url)
            data = response.json()
            created_at = data["created"]

             # Get the user's friend count
             url = f"https://friends.roblox.com/v1/users/{user_id}/friends/count"
             response = requests.get(url)
             data = response.json()
             friend_count = data["count"]

             # Get the user's role name in the group "United States Army"
             url = f"https://groups.roblox.com/v2/users/{user_id}/groups/roles"
             response = requests.get(url)
             data = response.json()

             role_name = None
             for group in data["data"]:
                 if group["group"]["id"] == 3108077:
                     role_name = group["role"]["name"]
                    break

             # Build the response message
            message = f"**Username:** {username}\n"
            message += f"**Account Age:** {created_at}\n"
            message += f"**Friend Count:** {friend_count}\n"
            message += f"**Role in United States Army:** {role_name}" if role_name else ""

        else:
            message = f"Could not find user {username}."

        # Send the response message
        await message.channel.send(message)

client.run('TOKEN')

I'm not sure why the username variable doesn't have a value. Please help!


Solution

  • Split splits a string into parts based on the string you put in as the argument. For example, "hi there".split(" ") is splitting "hi there" up by " ", so that will result in ["hi", "there"]. To get the username, use this:

    username = message.content.split(' ')[1]
    

    Also, your bot needs the message content intent, enable that in the developer portal and also with this:

    intents.message_content = True