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
|
import asyncio
import importlib
import re
import traceback
from string import whitespace
import discord
import sys
import json
import argparse
import os
client = discord.Client()
commands = []
def description(desc):
def wrapper(func):
func.description = desc
return func
return wrapper
def name(name):
def wrapper(func):
func.name = name
return func
return wrapper
@description('Helps you...')
@name('help')
@asyncio.coroutine
def help_cmd(cmd_args, author, channel, message):
embed = discord.Embed()
embed.colour = discord.Colour.gold()
embed.description = 'Helping you...'
for command in commands:
embed.add_field(name=command.name, value=command.description, inline=False)
embed.title = 'Command list'
yield from client.send_message(channel, embed=embed)
return True
commands += [help_cmd]
class Config(object):
def __init__(self, **kwargs):
self.token = kwargs['token']
self.postfix = kwargs.get('postfix', '...')
def parse_split(content):
def temp():
buffer = None
inquote = False
escape = False
for c in content:
if escape:
if buffer is None:
buffer = c
else:
buffer += c
if c in "'\"":
inquote = not inquote
if buffer is not None:
yield buffer
buffer = None
elif c in whitespace and not inquote:
if buffer is not None:
yield buffer
buffer = None
elif c == '\\':
escape = True
else:
if buffer is None:
buffer = c
else:
buffer += c
if buffer is not None:
yield buffer
return list(temp())
@client.event
@asyncio.coroutine
def process_message(message):
content = message.content[:-len(config.postfix)]
cmd_args = list(parse_split(content))
print(cmd_args)
cmd = cmd_args[-1]
cmd_args = cmd_args[:-1]
author = message.author
for c in commands:
if c.name.lower() == cmd.lower():
print('Executing command: %s(%s)' % (c.name, str(c)))
yield from c(cmd_args, author, message.channel, message)
return
help_cmd(cmd_args, author, message.channel, message)
@client.event
@asyncio.coroutine
def on_message(message: discord.Message):
if message.author.id == client.user.id:
return
if message.content.endswith(config.postfix):
yield from process_message(message)
@client.event
@asyncio.coroutine
def on_ready():
print('Ready')
def load_modules(dir='modules', module='modules') -> tuple:
print(flush=True, end='')
loaded_count = 0
total = 0
fails = 0
for file in os.listdir(dir):
newdir = os.path.join(dir, file)
if os.path.isdir(newdir):
add_loaded, add_fails, add_total = load_modules(newdir, module + '.' + file)
loaded_count += add_loaded
fails += add_fails
total += add_total
if file.endswith('.py'):
file = file[:-3]
total += 1
print('Loading module: %s' % (module + '.' + file), flush=True)
try:
importlib.import_module('%s.%s' % (module, file)).setup(commands, client)
except KeyboardInterrupt:
raise
except Exception as e:
traceback.print_tb(e)
print('Module load failed: %s' % (module + '.' + file), flush=True)
fails += 1
continue
print('Loaded module: %s' % (module + '.' + file), flush=True)
loaded_count += 1
return loaded_count, fails, total
if __name__ == '__main__':
args = argparse.ArgumentParser()
args.add_argument('config_file', type=open, help='Config file in json format.')
ns = args.parse_args(sys.argv[1:])
config = json.load(ns.config_file)
print('Config: %s' % str(config))
print()
print('Loading modules')
loaded, fails, total = load_modules()
print('[' + '#' * loaded + '-' * fails + ']')
if fails == 0:
print('Loaded all modules successfully')
else:
print('Some modules failed to load.')
line = ""
while line.lower() not in ["y", "n"]:
line = input("Start anyway? (Y/N)")
if line.lower() == 'n':
sys.exit(1)
config = Config(**config)
client.run(config.token, bot=True)
|