-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
163 lines (135 loc) · 5.42 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
import asyncio
import logging
import os
import asyncpg
import discord
from asyncpg.pool import Pool
from discord.ext import commands
from dotenv import load_dotenv
from pyot.core import Settings
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)
# Config aus .env einlesen.
load_dotenv()
startup_extensions = ["clash", "general", "league", "memes", "users", "reaction"]
class Tokens:
TOKEN: str = os.getenv('DISCORD_BOT_TOKEN')
GUILD: str = os.getenv('DISCORD_GUILD')
LOL_REGION: str = os.getenv('myregion')
RIOT_TOKEN: str = os.getenv('RIOTAPI')
eule: str = 'Jonas'
DB_USER: str = os.getenv('DB_USER')
DB_PW: str = os.getenv('DB_PW')
DB_NAME: str = os.getenv('DB_NAME')
DB_HOST: str = os.getenv('DB_HOST')
DB_PORT: str = os.getenv('DB_PORT')
# Variablen assignen
# noinspection PyTypeChecker
pool: Pool = "eule"
intents = discord.Intents.default()
intents.members = True
Settings(
MODEL="LOL",
DEFAULT_PLATFORM="EUW1",
DEFAULT_REGION="EUROPE",
DEFAULT_LOCALE="DE_DE",
PIPELINE=[
{"BACKEND": "pyot.stores.Omnistone"},
{"BACKEND": "pyot.stores.MerakiCDN"},
{"BACKEND": "pyot.stores.CDragon"},
{
"BACKEND": "pyot.stores.RiotAPI",
"API_KEY": Tokens.RIOT_TOKEN, # API KEY
"RATE_LIMITER": {
"BACKEND": "pyot.limiters.MemoryLimiter",
"LIMITING_SHARE": 1,
}
}
]
).activate()
class BetterBot(commands.Bot):
pool = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def main():
global pool
pool = await asyncpg.create_pool(user=os.getenv('DB_USER'), password=os.getenv('DB_PW'),
database=os.getenv('DB_NAME'), host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'))
bot = BetterBot(command_prefix='!', description="Tommy the Turtle", case_insensitive=True, intents=intents)
bot.pool = pool
@bot.event
async def on_ready():
guild = ""
for guild in bot.guilds:
if guild.id == Tokens.GUILD:
break
print(
f'{bot.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
ignored = ["__init__", "league", "eule"]
extensions = [x for x in [os.path.splitext(filename)[0] for filename in os.listdir('./plugins')] if
x not in ignored]
for extension in extensions:
try:
bot.load_extension(f'plugins.{extension}')
except (AttributeError, ImportError) as e:
print("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
print("{} loaded.".format(extension))
@bot.command()
@commands.is_owner()
async def load(ctx, extension_name: str):
"""Loads an extension."""
try:
bot.load_extension(f'plugins.{extension_name}')
except (AttributeError, ImportError) as e:
await ctx.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
await ctx.send("{} loaded.".format(extension_name))
@bot.command()
@commands.is_owner()
async def unload(ctx, extension_name: str):
"""Unloads an extension."""
try:
bot.unload_extension(f'plugins.{extension_name}')
except (AttributeError, ImportError) as e:
await ctx.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
await ctx.send("{} unloaded.".format(extension_name))
@bot.command()
@commands.is_owner()
async def reload(ctx, extension_name: str):
"""Reloads an extension."""
try:
bot.unload_extension(f'plugins.{extension_name}')
bot.load_extension(f'plugins.{extension_name}')
except (AttributeError, ImportError) as e:
await ctx.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
return
await ctx.send("{} reloaded.".format(extension_name))
# End Eule
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.PrivateMessageOnly):
await ctx.send('Dieser Command geht nur in einer Privatnachricht!')
if isinstance(error, commands.errors.MissingRole):
await ctx.send('Du hast für diesen Befehl nicht genügend Rechte!')
if isinstance(error, commands.UserInputError):
await ctx.send('Bitte überprüfe deine Eingabe.')
if isinstance(error, commands.ExtensionAlreadyLoaded):
await ctx.send('Module already loaded')
if isinstance(error, commands.MissingPermissions):
await ctx.send('Hallo du bist ziemlich sicher der Serverowner des Discords auf dem ich laufe. Ich kann '
'deshalb bei dir nichts ändern. Was immer ich auch machen sollte mochs söwa und gib im '
'Viktor bescheid damit er des in da DB manuell mocht.')
print(error)
await bot.start(Tokens.TOKEN)
if __name__ == "__main__":
asyncio.run(main())