-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
173 lines (153 loc) · 5.29 KB
/
main.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import discord
from discord.ext import commands
import os
import aiohttp
from dotenv import load_dotenv
import logging
from discord.app_commands import AppCommandError
import aiomysql
from discord import app_commands
import json
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
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)
class MyBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix = commands.when_mentioned,
intents = discord.Intents.default(),
status=discord.Status.online,
activity=discord.Game(name="with your tickets ✉️"))
async def setup_hook(self):
with open("db.json", "r") as f:
db = json.load(f)
conn = await aiomysql.connect(
host=db["HOST"],
user=db["USER"],
password=db["PASSWORD"],
db=db["DB"],
autocommit=True
)
cursor = await conn.cursor()
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS setup_category (
SERVERID VARCHAR(100) PRIMARY KEY,
CATEGORYID VARCHAR(100)
)
"""
)
print("✅ Created setup_category database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS log_channels (
SERVERID VARCHAR(100) PRIMARY KEY,
CHANNELID VARCHAR(100)
)
"""
)
print("✅ Created log_channels database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ticket_channels (
SERVERID VARCHAR(100) PRIMARY KEY,
CHANNELID VARCHAR(100)
)
"""
)
print("✅ Created ticket_channels database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ticket_messages (
SERVERID VARCHAR(100) PRIMARY KEY,
CHANNELID VARCHAR(100),
MESSAGEID VARCHAR(100)
)
"""
)
print("✅ Created ticket_messages database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ticket_categories (
SERVERID VARCHAR(100) PRIMARY KEY,
CATEGORYID VARCHAR(100)
)
"""
)
print("✅ Created ticket_categories database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ticket_button_messages (
SERVERID VARCHAR(100) PRIMARY KEY,
MESSAGE TEXT
)
"""
)
print("✅ Created ticket_button_messages database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ticket_manager_roles (
SERVERID VARCHAR(100) PRIMARY KEY,
ROLEID VARCHAR(100),
STATE TEXT
)
"""
)
print("✅ Created ticket_manager_roles database")
await cursor.execute(
"""
CREATE TABLE IF NOT EXISTS languages (
SERVERID VARCHAR(100) PRIMARY KEY,
LANGUAGE TEXT
)
"""
)
print("✅ Created languages database")
await cursor.close()
conn.close()
self.session = aiohttp.ClientSession()
for f in os.listdir("./cogs"):
if f.endswith(".py"):
await self.load_extension("cogs." + f[:-3])
await self.load_extension('jishaku')
print("Loaded all extensions.")
await bot.tree.sync()
async def on_ready(self):
print(f'{self.user} is now online. [{self.user.id}]')
bot = MyBot()
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: AppCommandError):
await interaction.response.send_message(error, ephemeral=True)
@bot.event
async def on_command_error(ctx: commands.Context, error: discord.errors):
await ctx.reply(error)
@bot.tree.command(name="set-language", description="Set the bot language")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.choices(
language = [
app_commands.Choice(name="English", value="en"),
app_commands.Choice(name="German", value="de"),
app_commands.Choice(name="Spanish", value="es"),
app_commands.Choice(name="French", value="fr"),
]
)
async def set_language(interaction: discord.Interaction, language: app_commands.Choice[str]):
await interaction.response.defer(ephemeral=True)
with open("db.json", "r") as f:
db = json.load(f)
conn = await aiomysql.connect(
host=db["HOST"],
user=db["USER"],
password=db["PASSWORD"],
db=db["DB"],
autocommit=True
)
cursor = await conn.cursor()
await cursor.execute("INSERT INTO languages (SERVERID, LANGUAGE) VALUES (%s, %s) ON DUPLICATE KEY UPDATE LANGUAGE = %s", (interaction.guild.id, language.value, language.value,))
await cursor.close()
conn.close()
await interaction.followup.send(f"Language set to **{language.name}**", ephemeral=True)
load_dotenv()
bot.run(os.getenv("TOKEN"))