-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (48 loc) · 2.58 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
from koduck import Koduck
import sys, importlib, logging
import yadon
import settings
#Required method to setup Koduck. Can also be run as a comamnd after startup to update any manual changes to the commands table.
async def refresh_commands(context, *args, **kwargs):
errors = []
table_items = yadon.ReadTable(settings.commands_table_name, named_columns=True).items()
if table_items is not None:
context.koduck.clear_commands()
for command_name, details in table_items:
if details["Module Name"] != "main":
if details["Module Name"] not in sys.modules:
try:
importlib.import_module(details["Module Name"])
except Exception as e:
errors.append("Failed to import module '{}': `{}`".format(details["Module Name"], str(e)))
try:
context.koduck.add_command(command_name, getattr(sys.modules[details["Module Name"]], details["Method Name"]), details["Command Type"], int(details["Command Tier"]), details["Description"])
except Exception as e:
errors.append("Failed to import command '{}': `{}`".format(details, str(e)))
else:
try:
context.koduck.add_command(command_name, globals()[details["Method Name"]], details["Command Type"], int(details["Command Tier"]), details["Description"])
except Exception as e:
errors.append("Failed to import command '{}': `{}`".format(details, str(e)))
if settings.enable_run_command:
context.koduck.add_run_slash_command()
errors = "\n".join(errors)
if context.message is not None:
if errors:
await context.koduck.send_message(context.message, content=settings.message_refresh_commands_errors + "\n" + errors)
else:
await context.koduck.send_message(context.message, content=settings.message_refresh_commands_success)
elif errors:
print(errors)
#Background task is run every set interval while bot is running (by default every 10 seconds)
async def background_task(koduck_instance):
pass
settings.background_task = background_task
koduck = Koduck()
koduck.add_command("refreshcommands", refresh_commands, "prefix", 3)
bot_token = open("token.txt").read()
if settings.enable_debug_logger:
log_handler = logging.FileHandler(filename=settings.debug_log_file_name, encoding='utf-8', mode='w')
koduck.client.run(bot_token, log_handler=log_handler, log_level=logging.DEBUG)
else:
koduck.client.run(bot_token, log_handler=None)