-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
56 lines (46 loc) · 1.62 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
import wxpy
import jinja2
from roll import roll
from weather import get_weather_forecast_msg, get_aqi_msg
bot = wxpy.Bot(console_qr=True, cache_path='/data/wxpy.pkl')
group = bot.groups()
HELP_TEXT = "使用说明:\n" \
"【.r 骰子个数d面数】" \
"如 .r 2d6 即为掷2个6面骰子,高级用法见文档\n" \
"【.tq 城市】查询天气\n" \
"【.aqi 城市】查询空气质量\n" \
"【.help】查看帮助\n" \
"源代码&文档: https://github.com/Windfarer/bot"
def help(msg):
return HELP_TEXT
def roll_dice(msg):
sender = msg.member.name if msg.member else msg.sender.name
text = msg.text[2:].strip()
print(text)
result = roll(text.split(" ", 2)[0])
if result:
print(result)
return "{} 掷了骰子「 {} 」\n" \
"掷出: {} \n" \
"总和为: {}".format(sender, text, str(result)[1:-1], str(sum([sum(i) for i in result])))
def get_weather_forecast(msg):
city_str = msg.text[4:].strip()
return get_weather_forecast_msg(city_str=city_str)
def get_aqi(msg):
city_str = msg.text[5:].strip()
return get_aqi_msg(city_str)
@bot.register(msg_types=wxpy.TEXT)
def entrypoint(msg):
if msg.text.startswith(".r "):
return roll_dice(msg)
if msg.text.startswith(".tq "):
return get_weather_forecast(msg)
if msg.text.startswith(".aqi "):
return get_aqi(msg)
elif msg.text == '.help':
return help(msg)
@bot.register(msg_types=wxpy.FRIENDS)
def add_friends(msg):
new_friend = msg.card.accept()
new_friend.send(HELP_TEXT)
bot.join()