No chat to show

This project's files are all here to explore.

Files
.gitignore
bot.py
config.json
README.md
requirements.txt
Code
5 236
bot.py
import discord
import os
import json
import asyncio
from dotenv import load_dotenv
import logging

load_dotenv()

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

client = discord.Client()

with open('config.json', 'r') as f:
    config = json.load(f)

monitoring_active = False
monitored_channel_id = None


@client.event
async def on_ready():
    logger.info(f'Queue Sniper ready! Logged in as {client.user.name}')
    logger.info(f'Monitoring for: {', '.join(config["tiers_to_monitor"])}')
    logger.info('Bot is active and monitoring for mctiers queue messages!')


@client.event
async def on_message(message):
    global monitoring_active, monitored_channel_id
    
    if message.author.id != client.user.id:
        if message.author.bot and message.author.name.lower() == 'mctiers':
            if monitoring_active:
                content_lower = message.content.lower()
                
                for tier in config['tiers_to_monitor']:
                    if tier.lower() in content_lower and 'queue' in content_lower:
                        await handle_queue_button(message, tier)
                        break
        return
    
    content = message.content
    
    if content == '!start':
        if monitoring_active:
            await message.channel.send("โŒ Already monitoring! Use `!stop` to stop monitoring first.")
            return
        
        monitoring_active = True
        monitored_channel_id = message.channel.id
        
        embed = discord.Embed(
            title="โœ… Queue Sniper Activated",
            description=f"Now monitoring for: **{', '.join(config['tiers_to_monitor'])}**",
            color=discord.Color.green()
        )
        embed.add_field(name="Status", value="๐ŸŸข Active", inline=False)
        embed.add_field(name="Channel", value=message.channel.mention, inline=False)
        embed.set_footer(text="Use !stop to stop monitoring")
        
        await message.channel.send(embed=embed)
        logger.info(f"Started monitoring in channel {message.channel.name}")
    
    elif content == '!stop':
        if not monitoring_active:
            await message.channel.send("โŒ Not currently monitoring! Use `!start` to begin monitoring.")
            return
        
        monitoring_active = False
        monitored_channel_id = None
        
        embed = discord.Embed(
            title="โ›” Queue Sniper Deactivated",
            description="Stopped monitoring for queues",
            color=discord.Color.red()
        )
        embed.set_footer(text="Use !start to resume monitoring")
        
        await message.channel.send(embed=embed)
        logger.info(f"Stopped monitoring")
    
    elif content == '!status':
        embed = discord.Embed(
            title="Queue Sniper Status",
            color=discord.Color.green() if monitoring_active else discord.Color.orange()
        )
        embed.add_field(name="Status", value="๐ŸŸข Active" if monitoring_active else "๐Ÿ”ด Inactive", inline=False)
        embed.add_field(name="Monitoring Tiers", value=', '.join(config['tiers_to_monitor']), inline=False)
        embed.add_field(name="Keywords", value=', '.join(config['queue_keywords']), inline=False)
        
        if monitoring_active and monitored_channel_id:
            channel = client.get_channel(monitored_channel_id)
            if channel:
                embed.add_field(name="Channel", value=channel.mention, inline=False)
        
        await message.channel.send(embed=embed)
    
    elif content.startswith('!settiers '):
        tiers = content.split()[1:]
        if not tiers:
            await message.channel.send("โŒ Please specify tiers! Example: `!settiers sword mace bow`")
            return
        
        config['tiers_to_monitor'] = [tier.lower() for tier in tiers]
        
        with open('config.json', 'w') as f:
            json.dump(config, f, indent=4)
        
        embed = discord.Embed(
            title="โœ… Tiers Updated",
            description=f"Now monitoring: **{', '.join(config['tiers_to_monitor'])}**",
            color=discord.Color.blue()
        )
        await message.channel.send(embed=embed)
        logger.info(f"Updated tiers to: {', '.join(config['tiers_to_monitor'])}")


async def handle_queue_button(message, tier):
    """Handle detected queue message with button"""
    global monitored_channel_id
    
    try:
        logger.info(f"๐ŸŽฏ Detected {tier} queue message from mctiers bot")
        
        if message.components:
            for action_row in message.components:
                for component in action_row.children:
                    if isinstance(component, discord.Button):
                        try:
                            await component.click()
                            logger.info(f"โœ… Successfully clicked button for {tier} queue!")
                            
                            if monitored_channel_id:
                                channel = client.get_channel(monitored_channel_id)
                                if channel:
                                    embed = discord.Embed(
                                        title="โšก Queue Joined!",
                                        description=f"Clicked button for **{tier}** queue!",
                                        color=discord.Color.green()
                                    )
                                    embed.add_field(name="Tier", value=tier.upper(), inline=True)
                                    embed.add_field(name="Message", value=f"[Jump]({message.jump_url})", inline=True)
                                    embed.set_footer(text="mctiers queue sniper")
                                    await channel.send(embed=embed)
                            return
                        except Exception as e:
                            logger.error(f"โŒ Failed to click button: {e}")
        else:
            logger.warning(f"โš ๏ธ No buttons found on {tier} queue message")
    
    except Exception as e:
        logger.error(f"Error handling queue button: {e}")


client.run(os.getenv('DISCORD_TOKEN'))