-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (95 loc) · 4.05 KB
/
main.py
File metadata and controls
112 lines (95 loc) · 4.05 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
110
111
112
import os
import discord
import asyncio
from discord.ext import commands
from dotenv import load_dotenv
import aiomysql
load_dotenv()
token = os.getenv("TOKEN")
db_host = os.getenv("DB_HOST")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_name = os.getenv("DB_NAME")
class MainDatei(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True
intents.presences = True
super().__init__(command_prefix="!", intents=intents)
async def setup_hook(self):
geladene_cogs = 0
cog_files = [f for f in os.listdir("datein") if f.endswith(".py")]
print(f"Gefundene Cogs: {len(cog_files)}")
for filename in cog_files:
await self.load_extension(f"datein.{filename[:-3]}")
geladene_cogs += 1
print(f"Cog geladen: {filename} ({geladene_cogs}/{len(cog_files)})")
print("Verbindung zur Datenbank wird aufgebaut...")
try:
print(db_host, db_user, db_name)
self.pool = await aiomysql.create_pool(
host=db_host,
port=3306,
user=db_user,
password=db_password,
db=db_name,
minsize=1,
maxsize=10,
autocommit=True,
connect_timeout=10,
pool_recycle=300
)
print("✅ Datenbank verbunden!")
except Exception as e:
print(f"❌ Fehler bei der Datenbankverbindung: {e}")
return
async with self.pool.acquire() as conn:
await conn.ping(reconnect=True)
async with conn.cursor() as cur:
await cur.execute(
"CREATE TABLE IF NOT EXISTS nexory_user_tasks("
"userID BIGINT, title VARCHAR(50), des LONGTEXT, date DATE, remindme BOOLEAN DEFAULT FALSE, tag VARCHAR(10), status VARCHAR(10) DEFAULT 'open', priority VARCHAR(20) DEFAULT 'normal')"
)
print("Tabelle nexory_user_tasks überprüft/erstellt")
await cur.execute(
"CREATE TABLE IF NOT EXISTS nexory_guild_tasks("
"guildID BIGINT, title VARCHAR(50), des LONGTEXT, date DATE, remindme BOOLEAN DEFAULT FALSE, tag VARCHAR(10), status VARCHAR(10) DEFAULT 'open', priority VARCHAR(20) DEFAULT 'normal')"
)
print("Tabelle nexory_guild_tasks überprüft/erstellt")
await cur.execute(
"CREATE TABLE IF NOT EXISTS nexory_guild_config (guildID BIGINT, reminde_channel BIGINT, mode TEXT)"
)
print("Tabelle nexory_guild_config überprüft/erstellt")
await cur.execute(
"CREATE TABLE IF NOT EXISTS nexory_user_config (userID BIGINT, mode TEXT)"
)
print("Tabelle nexory_user_config überprüft/erstellt")
await cur.execute(
"CREATE TABLE IF NOT EXISTS nexory_guild_custom_tags (guildID BIGINT, tag VARCHAR(10))"
)
print("Tabelle nexory_guild_custom_tags überprüft/erstellt")
async def on_ready(self):
print(f"✅ Eingeloggt als {self.user}")
bot = MainDatei()
async def main():
async with bot:
await bot.start(token)
@bot.command()
@commands.is_owner()
async def dbtest(ctx: commands.Context):
try:
async with bot.pool.acquire() as conn:
await conn.ping(reconnect=True)
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
result = await cur.fetchone()
if result:
await ctx.send("✅ Datenbankverbindung ist aktiv!")
else:
await ctx.send("❌ Datenbankverbindung ist nicht aktiv!")
except Exception as e:
await ctx.send(f"❌ Fehler bei der Datenbankverbindung: {e}")
if __name__ == "__main__":
asyncio.run(main())