import discord
from discord.ext import commands, tasks
from twitch import TwitchClient
from pprint import pformat

# Initialize Twitch client with your client ID
client = TwitchClient(client_id='<twitch token>')

# Create a Discord bot instance with a command prefix
bot = commands.Bot(command_prefix='$')

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')

@bot.command()
async def info(ctx, username):
    response = await ctx.send('Querying Twitch...')
    try:
        # Translate username to user ID
        users = client.users.translate_usernames_to_ids(username)
        for user in users:
            userid = user.id
        twitchinfo = client.users.get_by_id(userid)
        await check_live_status(ctx, userid, twitchinfo, response)
    except Exception as e:
        await response.edit(content='Invalid username')

@tasks.loop(seconds=10)
async def check_live_status(ctx, userid, twitchinfo, response):
    # Check if the user is currently live
    status = client.streams.get_stream_by_user(userid)
    if status is None:
        livestat = f'{twitchinfo.display_name} is not live'
    else:
        livestat = f'{twitchinfo.display_name} is {status.stream_type}'
    responsemsg = pformat(twitchinfo) + '\n' + livestat
    await response.edit(content=responsemsg)

# Start the bot with your Discord token
bot.run('<discord token>')