forked from LanceMaverick/skybeard-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·144 lines (107 loc) · 4.23 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
#!/usr/bin/env python
import os
import asyncio
import sys
import logging
import importlib
import argparse
import telepot
from telepot.aio.delegate import (per_chat_id,
create_open,
pave_event_space,
include_callback_query_chat_id)
import config
from skybeard.beards import Beard, BeardChatHandler, SlashCommand
from help import create_help
logger = logging.getLogger(__name__)
class DuplicateCommand(Exception):
pass
def is_module(filename):
fname, ext = os.path.splitext(filename)
if ext == ".py":
return True
elif os.path.exists(os.path.join(filename, "__init__.py")):
return True
else:
return False
def get_literal_path(path_or_autoloader):
try:
return path_or_autoloader.path
except AttributeError:
assert type(path_or_autoloader) is str,\
"beard_path is not a str or an AutoLoader!"
return path_or_autoloader
def get_literal_beard_paths(beard_paths):
return [get_literal_path(x) for x in beard_paths]
def all_possible_beards(paths):
literal_paths = get_literal_beard_paths(paths)
for path in literal_paths:
for f in os.listdir(path):
if is_module(os.path.join(path, f)):
yield os.path.basename(f)
def delegator_beard_gen(beards):
for beard in beards:
if hasattr(beard, "on_callback_query"):
yield include_callback_query_chat_id(pave_event_space())(
per_chat_id(), create_open, beard, timeout=beard._timeout)
else:
yield pave_event_space()(
per_chat_id(), create_open, beard, timeout=beard._timeout)
def main(config):
for beard_path in config.beard_paths:
sys.path.insert(0, get_literal_path(beard_path))
logger.info("The following plugins were found:\n {}".format(
', '.join(list(all_possible_beards(config.beard_paths)))))
logger.info("config.beards: {}".format(config.beards))
if config.beards == "all":
for beard_name in all_possible_beards(config.beard_paths):
importlib.import_module(beard_name)
else:
for beard_name in config.beards:
importlib.import_module(beard_name)
for beard_path in config.beard_paths:
sys.path.pop(0)
# Check if there are any duplicate commands
all_cmds = set()
for beard in Beard.beards:
if hasattr(beard, '__commands__'):
for cmd in beard.__commands__:
if isinstance(cmd, SlashCommand):
if cmd.cmd in (x.cmd for x in all_cmds):
# TODO Tell the user which beards conflict
raise DuplicateCommand(
"The command /{} occurs in more than "
"one beard.".format(cmd.cmd))
all_cmds.add(cmd)
bot = telepot.aio.DelegatorBot(
BeardChatHandler.key,
list(delegator_beard_gen(Beard.beards))
)
loop = asyncio.get_event_loop()
loop.create_task(bot.message_loop())
print('Listening ...')
loop.run_forever()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Skybeard hails you!')
parser.add_argument('-k', '--key', default=os.environ.get('TG_BOT_TOKEN'))
parser.add_argument('--no-help', action='store_true')
parser.add_argument('-d', '--debug', action='store_const', dest="loglevel",
const=logging.DEBUG, default=logging.INFO)
parsed = parser.parse_args()
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=parsed.loglevel)
# Set up the master beard
BeardChatHandler.setup_beards(parsed.key)
# If the user does not specially request --no-help, set up help command.
if not parsed.no_help:
create_help(config)
logger.debug("Config found to be: {}".format(dir(config)))
# TODO make an argparse here to override the config file (and also specify
# the config file)
main(config)
# bot = telepot.aio.DelegatorBot(TOKEN, [
# include_callback_query_chat_id(
# pave_event_space())(
# per_chat_id(types=['private']), create_open, Lover, timeout=10),
# ])