Compare commits

..

3 Commits

92
bot.py
View File

@@ -36,6 +36,7 @@ AUDIT_CHANNEL_ID = int(os.getenv('AUDIT_CHANNEL_ID', '1340861451392520233'))
ANNOUNCEMENT_CHANNEL_ID = int(os.getenv('ANNOUNCEMENT_CHANNEL_ID', '605864889940050088')) ANNOUNCEMENT_CHANNEL_ID = int(os.getenv('ANNOUNCEMENT_CHANNEL_ID', '605864889940050088'))
ADMIN_ROLE_ID = int(os.getenv('ADMIN_ROLE_ID', '605867042104541194')) ADMIN_ROLE_ID = int(os.getenv('ADMIN_ROLE_ID', '605867042104541194'))
MINECRAFT_ROLE_ID = int(os.getenv('MINECRAFT_ROLE_ID', '821163520942145556')) MINECRAFT_ROLE_ID = int(os.getenv('MINECRAFT_ROLE_ID', '821163520942145556'))
HYTALE_ROLE_ID = int(os.getenv('HYTALE_ROLE_ID', '1460750779848589548'))
COOL_KIDS_ROLE_ID = int(os.getenv('COOL_KIDS_ROLE_ID', '788968178117902347')) COOL_KIDS_ROLE_ID = int(os.getenv('COOL_KIDS_ROLE_ID', '788968178117902347'))
OWNER_ID = int(os.getenv('OWNER_ID', '238728085342519296')) OWNER_ID = int(os.getenv('OWNER_ID', '238728085342519296'))
REACTION_MESSAGE_ID = int(os.getenv('REACTION_MESSAGE_ID', '744047519696420914')) REACTION_MESSAGE_ID = int(os.getenv('REACTION_MESSAGE_ID', '744047519696420914'))
@@ -86,84 +87,12 @@ class CustomBot(commands.Bot):
"Ranked Minesweeper" "Ranked Minesweeper"
]) ])
self.remove_command("help") self.remove_command("help")
self.adjectives_data = None
async def setup_hook(self): async def setup_hook(self):
logger.info("Syncing commands...") logger.info("Syncing commands...")
await self.tree.sync() await self.tree.sync()
logger.info("Commands synced!") logger.info("Commands synced!")
# Load adjectives asynchronously
adjectives_path = Path('adjectives.json')
if adjectives_path.exists():
try:
async with aiohttp.ClientSession() as session:
# Use aiofiles-like approach with asyncio
loop = asyncio.get_event_loop()
self.adjectives_data = await loop.run_in_executor(
None,
lambda: json.loads(adjectives_path.read_text())
)
logger.info("Adjectives data loaded successfully")
self.daily_adjective.start()
except Exception as e:
logger.error(f"Failed to load adjectives.json: {e}")
else:
logger.warning("adjectives.json not found, daily adjective task disabled")
@tasks.loop(time=datetime_time(hour=14))
async def daily_adjective(self):
try:
channel = self.get_channel(ANNOUNCEMENT_CHANNEL_ID)
if not channel:
logger.error(f"Failed to get announcement channel {ANNOUNCEMENT_CHANNEL_ID}")
return
if not self.adjectives_data or not self.adjectives_data.get("adjectives"):
logger.warning("No adjectives available")
return
# Get and remove today's adjective
adjective = self.adjectives_data["adjectives"].pop(0)
# Create and send embed
embed = discord.Embed(
title="📚 Adjective of the Day",
color=discord.Color.from_rgb(152, 0, 0),
timestamp=datetime.now()
)
embed.add_field(
name=adjective["word"].capitalize(),
value=adjective["definition"].capitalize(),
inline=False
)
embed.set_footer(text="Expand your vocabulary!")
await channel.send(embed=embed)
# Save updated adjectives list back to file asynchronously
loop = asyncio.get_event_loop()
await loop.run_in_executor(
None,
lambda: Path('adjectives.json').write_text(
json.dumps(self.adjectives_data, indent=4)
)
)
# Reload list if empty
if not self.adjectives_data["adjectives"]:
logger.info("Adjectives list empty, reloading from file")
self.adjectives_data = await loop.run_in_executor(
None,
lambda: json.loads(Path('adjectives.json').read_text())
)
except Exception as e:
logger.error(f"Error in daily_adjective task: {e}", exc_info=True)
@daily_adjective.before_loop
async def before_daily_adjective(self):
await self.wait_until_ready()
@tasks.loop(seconds=STATUS_UPDATE_INTERVAL) @tasks.loop(seconds=STATUS_UPDATE_INTERVAL)
async def change_status(self): async def change_status(self):
try: try:
@@ -233,7 +162,10 @@ def has_role_check(role_id: int):
async def on_message(message): async def on_message(message):
try: try:
logger.info(f"Channel: [{str(message.channel)}] User: {str(message.author)} Content: {message.content}") logger.info(f"Channel: [{str(message.channel)}] User: {str(message.author)} Content: {message.content}")
await client.process_commands(message)
# Special response for user 331862422996647938 when message starts with a period
if message.author.id == 331862422996647938 and message.content.startswith('.'):
await message.channel.send("Whatever conversation is happening right now, Jared is right")
except Exception as e: except Exception as e:
logger.error(f"Error in on_message: {e}", exc_info=True) logger.error(f"Error in on_message: {e}", exc_info=True)
@@ -662,7 +594,7 @@ async def minecraft(interaction: discord.Interaction, minecraft_username: str):
@client.tree.command(name="hytale", description="Request whitelist on the Hytale server") @client.tree.command(name="hytale", description="Request whitelist on the Hytale server")
@app_commands.describe(hytale_username="Your Hytale username") @app_commands.describe(hytale_username="Your Hytale username")
@has_role_check(MINECRAFT_ROLE_ID) @has_role_check(HYTALE_ROLE_ID)
async def hytale(interaction: discord.Interaction, hytale_username: str): async def hytale(interaction: discord.Interaction, hytale_username: str):
"""Request whitelist on the Hytale server""" """Request whitelist on the Hytale server"""
try: try:
@@ -1149,18 +1081,6 @@ async def userinfo(interaction: discord.Interaction, member: discord.Member):
logger.error(f"Error in userinfo command: {e}", exc_info=True) logger.error(f"Error in userinfo command: {e}", exc_info=True)
@client.event
async def on_command_error(ctx, error):
"""Handle prefix command errors."""
try:
if isinstance(error, commands.CommandNotFound):
await ctx.send("Command not found!")
logger.error(f"Command error: {str(error)}")
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to use this command!")
logger.error(f"Command error: {str(error)}")
except Exception as e:
logger.error(f"Error in on_command_error: {e}", exc_info=True)
@client.tree.error @client.tree.error