-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
44 lines (38 loc) · 1.33 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import discord
from discord.ext import commands
import os
import requests
from PIL import Image
import aiosqlite
import asyncio
import logging
# Configure logging
logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="/p!", intents=intents)
@bot.event
async def on_ready():
logger.info(f'Logged in as {bot.user} (ID: {bot.user.id})')
for filename in os.listdir("./commands"):
if filename.endswith(".py"):
try:
bot.load_extension(f"commands.{filename[:-3]}")
except Exception as e:
logger.exception(f"Failed to load extension {filename}: {e}")
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
if DISCORD_TOKEN is None:
logger.critical("DISCORD_TOKEN not found in environment variables!")
exit(1)
try:
bot.run(DISCORD_TOKEN)
except discord.errors.LoginFailure as e:
logger.critical(f"Login failed: {e}")
except discord.errors.HTTPException as e:
logger.critical(f"HTTP error during connection: {e}")
except Exception as e:
logger.exception(f"An unexpected error occurred: {e}")