-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
125 lines (111 loc) · 3.98 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
import hikari
import miru
import lightbulb
import yaml
import os
import datetime
import traceback
import err
from scheduler import scheduler
def load_config():
with open("config.yaml") as cfg:
container = yaml.safe_load(cfg)
return container
if __name__ == "__main__":
intents = (
hikari.Intents.ALL_UNPRIVILEGED
| hikari.Intents.GUILD_MEMBERS
| hikari.Intents.MESSAGE_CONTENT
)
config = load_config()
bot = lightbulb.BotApp(
token=config["token"],
intents=intents,
)
@bot.listen(lightbulb.CommandErrorEvent)
async def on_error(event: lightbulb.CommandErrorEvent) -> None:
exception = event.exception.__cause__ or event.exception
exceptions = [
# General
{
"exception": lightbulb.NotOwner,
"response": "Only the owner of the bot can do that.",
},
{
"exception": lightbulb.MissingRequiredPermission,
"response": "You have insufficient permissions to run this command.",
},
{
"exception": lightbulb.CheckFailure,
"response": "You have insufficient permissions to run this command.",
},
# Public roles
{
"exception": err.NoPublicRoles,
"response": "You don't have any public roles!",
},
# AEGIS
{
"exception": err.NonAlphanumericGlobalBan,
"response": "The provided URL must only have numeric ID's.",
},
{
"exception": err.InvalidGlobalBanURL,
"response": "The provided URL is incorrect or couldn't be reached.",
},
# Probations
{
"exception": err.UserNotInProbation,
"response": "This user isn't in probation!",
},
{
"exception": err.UnstrikeableRole,
"response": "Users that can manage strikes can't be striked.",
},
{
"exception": err.UserHasNoStrikes,
"response": "This user has no strikes.",
},
{
"exception": err.UnstrikeableBot,
"response": "Bots can't be striked.",
},
# Watchlist
{
"exception": err.UserAlreadyWatched,
"response": "This user is already in the watchlist.",
},
]
for response in exceptions:
if isinstance(exception, response["exception"]):
await event.context.respond(
response["response"], flags=hikari.MessageFlag.EPHEMERAL
)
return
with open("/tmp/err.txt", "w+") as err_txt:
err_txt.write(" ".join(traceback.format_exception(exception)))
await event.context.respond(
f"An unknown error occured trying to run `{event.context.command.name}`. Please see the attachment for more info.",
flags=hikari.MessageFlag.EPHEMERAL,
attachment="/tmp/err.txt",
)
os.remove(f"/tmp/err.txt")
@bot.listen()
async def start_scheduler(event: hikari.StartedEvent):
scheduler.start()
bot.d = load_config()
bot.d["start_time"] = datetime.datetime.now()
for folder in bot.d["plugin_folders"]:
bot.load_extensions_from(folder)
miru.install(bot)
bot.run()
def load_plugin_configs(plugin: str, datastore: lightbulb.utils.data_store.DataStore()):
for config in os.listdir("server_configs"):
if not "config" in datastore:
datastore["config"] = {}
if "sample.yaml" not in config:
with open(f"server_configs/{config}") as cfg:
server_id = int(config.split(".")[0])
config = yaml.safe_load(cfg)
if plugin in config:
datastore["config"][server_id] = config[plugin]