-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
336 lines (305 loc) · 12.7 KB
/
bot.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os
import sys
import discord
import logging
import importlib
import traceback
import configparser
from bot_func import Bot
from perm import perm_cmd
def import_module():
"""
This function checks modules folder by default, and load every single module
in the folder and store it as a dict.
It couldn't be put in to the bot_func.py as it requires the access to the
modules, commands, filters variables. I know that there's ways to implement
this without global keyword, but not now.
also, I suggest you to not use reload command often as it messes up badly
with some modules that stores informations on their own like 음악, it
literally fucks up and it requires you to manually kick the bot and rejoin.
"""
# TODO: Implement `on_reload` methods to all the modules, and execute it
# when the bot reloads modules.
global modules, commands, filters
folderpath = os.path.join(PATH, "modules")
logging.info(f"Loading {folderpath}...")
for modulefile in os.listdir(folderpath):
if not modulefile.startswith("__"):
module_name = modulefile.split(".")[0]
# if it exists, reload it. else, import it.
try:
importlib.reload(modules[module_name])
log = f"Succesfully Reloaded {module_name}."
except KeyError:
module = importlib.import_module(f"modules.{module_name}")
modules[module_name] = module
if module.PERMISSION == 6:
filters.append(module_name)
else:
commands.append(module_name)
log = f"Succesfully Loaded {module_name}."
logging.info(log)
# Get path based on the script's location.
PATH = os.path.dirname(os.path.abspath(__file__))
# Read configs
config = configparser.ConfigParser()
# This step is required because I don't prefer headers in my config file and
# configparser will definitely not accept it. This workaround idea is from
# CoupleWavyLines' answer, from stackoverflow question
# 'parsing .properties file in Python': https://stackoverflow.com/a/25493615
try:
with open(os.path.join(PATH, 'config.ini')) as f:
config.read_string("[section]\n" + f.read())
except:
raise FileNotFoundError("Config file is not in the directory.")
# Convert value to hexadecimal. raise valueerror if it's not hexadecimal.
try:
MAINCOLOR = int(config['section']['MainColor'], 16)
except ValueError:
raise ValueError("Value of config entry 'MainColor' is not hexadecimal.")
BOTNAME = config['section']['BotName']
PREFIX = config['section']['Prefix']
OWNERID = int(config['section']['OwnerId'])
# STATICS will be passed to modules/bot module.
STATICS = {
"PATH": PATH,
"PREFIX": PREFIX,
"MAINCOLOR": MAINCOLOR,
"BOTNAME": BOTNAME,
"OWNERID": OWNERID
}
# Initialize Bot module with static variables.
Bot.set_statics(STATICS)
del config
# Logging part, This logs at both console window and discordbot.log file
logging.captureWarnings(True)
# Example output:
# 2020/04/20 01:10:37|[LEVEL](any_function): This is example
log_formatter = logging.Formatter(
"%(asctime)s|[%(levelname)s](%(funcName)s): %(message)s",
"%Y/%m/%d %H:%M:%S"
)
logger = logging.getLogger()
# Set loglevel to INFO.
logger.setLevel(logging.INFO)
# This handler will log to the file.
file_handler = logging.FileHandler(os.path.join(PATH, "discordbot.log"))
file_handler.setFormatter(log_formatter)
logger.addHandler(file_handler)
# This handler will log to the console.
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
logger.addHandler(console_handler)
logger.info(
f"\n====================Starting {BOTNAME}...====================\n"
)
token = Bot.get_file("discord_token")
sv_prefix = Bot.get_json("bot_prefix")
sv_perm = Bot.get_json("bot_perm")
modules = {}
commands = []
filters = []
import_module()
client = discord.Client()
# Aight, here goes handlers
@client.event
async def on_ready():
## TODO: Do something in here, like changing the activity for every single
# second
logger.info('Succesfully logged in as {0.user}!'.format(client))
activity = discord.Game("Use ?prefix to get prefix")
await client.change_presence(activity = activity)
@client.event
async def on_message(message):
global sv_prefix, sv_perm
"""
Gets prefix for the server, check if command exists, check permission of
the user, and finally run it.
It won't do anything if message.author is client.user.
it contains the code for permission/prefix.
"""
if message.author == client.user:
return
# Get prefix from sv_prefix dict. if KeyError happens, load default prefix.
try:
prefix = sv_prefix[str(message.guild.id)]
except KeyError:
prefix = PREFIX
# Load user's permission.
perm = await Bot.get_user_perm(message.author, message.guild, sv_perm)
if perm == 9:
return
if not message.author.bot and message.content != prefix:
if message.content == "?prefix":
# Print the prefix that is used in this server
logging.info(f"{str(message.author)} used the command ?prefix.")
embed = await Bot.get_embed(
"prefix",
f"Prefix used in this server is `{prefix}`.",
message.author
)
await Bot.send_msg(message.channel, embed)
elif message.content == "?reset_prefix" and perm <= 3:
# Reset the prefix to !. only svmod user can use this.
logging.info(
f"{str(message.author)} used the command ?prefix_reset."
)
sv_prefix[str(message.guild.id)] = PREFIX
await Bot.json_dump(sv_prefix, "bot_prefix")
embed = await Bot.get_embed(
"reset_prefix",
f"Sucessfully restored the prefix to `{PREFIX}`.",
message.author
)
await Bot.send_msg(message.channel, embed)
elif message.content.startswith(prefix):
# Get a command and run it if it's a thing
fullcmd = message.content.replace(prefix, "", 1)
cmd = fullcmd.split()[0]
logging.info(
f"{str(message.author)} used the command {cmd}. message: {message.content}"
)
if cmd == "set_prefix" and perm <= 3:
cmdsplit = fullcmd.split()
if len(cmdsplit) != 2:
raise ValueError("Usage: set_prefix [prefix]")
prefix = cmdsplit[1]
sv_prefix[str(message.guild.id)] = prefix
await Bot.json_dump(sv_prefix, "bot_prefix")
embed = await Bot.get_embed(
"set_prefix",
"Succesfully set the prefix to " + prefix + ".",
message.author
)
await Bot.send_msg(message.channel, embed)
if cmd == "reload" and perm == 1:
# Call import_module() function to reload modules
logger.info("Reloading...")
embed = await Bot.get_embed(
"reload",
"Reloading Modules...",
message.author
)
msg = await Bot.send_msg(message.channel, embed)
import_module()
embed = await Bot.get_embed(
"reload",
"Succesfully Reloaded modules.",
message.author
)
await Bot.edit_msg(msg, embed)
elif cmd == "perm":
rtnvalue, new_sv_perm = await perm_cmd(
fullcmd.split(), message, sv_perm, modules, commands)
sv_perm = new_sv_perm
embed = await Bot.get_embed(
"permission",
rtnvalue,
message.author
)
await Bot.send_msg(message.channel, embed)
elif cmd in commands:
# Get the module's permission, If user's permission is higher
# than the command, execute it
module = modules[cmd]
module_perm = await Bot.get_module_perm(
module, message.guild, sv_perm
)
if perm <= module_perm:
# module.main function is async generator which yields the
# content to send. so It stores the generator in msggen
# variable and execute it.
logging.info(
f"{str(message.author)} used the command {prefix}{cmd}."
)
msggen = module.main(
message,
client = client,
statics = STATICS,
sv_perm = sv_perm,
sv_prefix = sv_prefix,
modules = modules,
bot_func = Bot
)
msg = None
async for rtnvalue in msggen:
if rtnvalue is None:
# If module wants to remove the message, It will
# return None. If we already sent a message, delete
# it.
if msg:
await msg.delete()
msg == None
else:
# If the type is str, It has to be converted to
# embed unless it's starting with "noembed|", which
# should be sent as is.
# Module can also yield the embed object directly,
# or Yield the file. If the type is not a str, It
# will assume that it is either embed or file, and
# pass it directly to send_msg or edit_msg function.
if type(rtnvalue) == str:
if rtnvalue.startswith("noembed|"):
content = rtnvalue.replace(
"noembed|", "", 1
)
else:
content = await Bot.get_embed(
cmd,
rtnvalue,
message.author
)
else:
content = rtnvalue
if msg:
# If we already sent a message, edit it. else,
# send a new one.
await Bot.edit_msg(msg, content)
else:
msg = await Bot.send_msg(
message.channel, content)
elif module_perm != 0:
# You don't have permission? Fuck off.
embed = await Bot.get_embed(
"Permission Error",
"You don't have permission to use this command.",
message.author,
no_capitalize = True,
colour = 0xff0000)
await Bot.send_msg(message.channel, embed)
for module_name in filters:
# Execute all the filters. it should only recieve a message object and
# return(not yield) a string to be sent as is.
# Inputs from bots are not filtered for fun interactions between bots,
# Modules should filter those.
content = await modules[module_name].main(message)
if content:
await Bot.send_msg(message.channel, content)
@client.event
async def on_error(event, *args, **kwargs):
"""
If event is on_message, get Traceback information and send it to the channel
, also log it using traceback.format_exc().
else, raise the exception again.
"""
if event == "on_message":
message = args[0]
exc = sys.exc_info()
excname = exc[0].__name__
excarglist = [str(x) for x in exc[1].args]
if not excarglist:
tbtxt = excname
else:
tbtxt = excname + ": " + ", ".join(excarglist)
tb = traceback.format_exc()
logger.error(tb)
embed = await Bot.get_embed(
title = "An error has been occured while running the task.",
desc = f"```{tbtxt}```",
colour = 0xff0000,
sender = message.author
)
await Bot.send_msg(message.channel, embed)
else:
raise
client.run(token)