-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfabaxi.py
More file actions
109 lines (97 loc) · 3.69 KB
/
Copy pathfabaxi.py
File metadata and controls
109 lines (97 loc) · 3.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import asyncio
import logging
import os
from pathlib import Path
import random
import aiohttp
import discord
from discord.ext import commands, tasks
from dotenv import load_dotenv
from ext.cache import load_random_status
from ext.system import send_system_message
from ext.api import ApiServer
from db import Database
load_dotenv()
##########################################################################
# Python 3.14 removed the implicit event loop creation in
# asyncio.get_event_loop(); py-cord's Client.__init__ still relies on it
# when no loop is passed explicitly, so we set one up ourselves here.
asyncio.set_event_loop(asyncio.new_event_loop())
##########################################################################
intents = discord.Intents().default()
intents.message_content = True
intents.guild_messages = True
intents.messages = True
intents.guilds = True
intents.members = True
##########################################################################
environment = os.getenv('ENVIRONMENT') # get the environment variable
if environment == 'PROD':
TOKEN = os.getenv('PROD_TOKEN')
bot = commands.Bot(command_prefix='!', intents=intents)
else:
TOKEN = os.getenv('DEV_TOKEN')
bot = commands.Bot(command_prefix='!', debug_guilds=[os.getenv("DEV_SERVER")], intents=intents)
_api_server = ApiServer(bot)
##########################################################################
logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(message)s', handlers=[logging.StreamHandler()])
##########################################################################
@bot.event
async def on_ready():
print('--------------------------------------')
print('Bot is ready.')
print('Eingeloggt als')
print(bot.user.name)
print(bot.user.id)
print('--------------------------------------')
logging.info(f"{bot.user} is ready and online!")
try:
await Database().init_db()
logging.info("Database initialized!")
except Exception as e:
logging.warning(f"Database connection failed: {e}")
await send_system_message(bot=bot, content="Bot is ready and online!")
await _api_server.start()
if not change_status.is_running():
change_status.start()
if not uptime_heartbeat.is_running():
uptime_heartbeat.start()
@bot.event
async def on_close():
await _api_server.stop()
@tasks.loop(seconds=30)
async def uptime_heartbeat():
if not bot.is_ready():
uptime_heartbeat.stop()
return
url = os.getenv("UPTIME_KUMA_PUSH_URL")
if not url:
return
try:
async with aiohttp.ClientSession() as session:
async with session.get(url):
pass
except Exception as e:
logging.warning(f"Uptime Kuma heartbeat failed: {e}")
@tasks.loop(hours=12)
async def change_status():
new_status = random.choice(load_random_status())
logging.info(f"Current Status: {new_status}")
await send_system_message(bot, f"Set new Status:\n**{new_status}**")
await bot.change_presence(activity=discord.CustomActivity(name=new_status))
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Load Cogs dynamically from app/cogs
cogs_dir = Path("app/cogs")
if cogs_dir.is_dir():
for cog_file in sorted(cogs_dir.glob("*.py")):
if cog_file.name.startswith("__"):
continue
module_name = f"app.cogs.{cog_file.stem}"
try:
bot.load_extension(module_name)
except Exception as error:
logging.error(f"{cog_file.name} could not be loaded: [{error}]")
else:
logging.info(f"{cog_file.name} was loaded correctly")
bot.run(TOKEN)