commit 7b6fa53913c16d8e1cce09bfdf9689460fbd5de6 Author: SiHuan Date: Thu Feb 27 21:47:36 2020 +0800 emm diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1743cb --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# XZZ +小智障,一个拓展 CoolQ HttpApi,快速构建命令式 QQ 机器人的框架. + +## 如何使用 + +### 安装配置 + +#### 0.自己安装配置好 CoolQ 及其 HttpApi 插件. + +包括上报过滤! + +#### 1.clone 本仓库, 填写 `config.py` + +其中 `APIURL` 为 CoolQ HttpApi 的 URL, `AUTHORIZATION` 为 CoolQ HttpApi 的 `access_token`, `PORT` 和 CoolQ HttpApi `post_url` 中的端口保持一致. + +#### 2.安装依赖, 启动服务 + +```shell +pip install -r req.txt +python main.py +``` + + + +### 拓展功能 + +在 `worker` 文件夹中添加 python 模块.文件名即受该文件响应的命令. + +如果要响应 `/ping` 命令的话, 文件名应为 `ping.py` + + + +在该模块实现一个 `Ans` 类, 此类继承自 `zzcore`里的 `StdAns` 类. + +在这个类中你可选的重写以下属性和方法: + +`AllowGroup` , int 类型的 list, 在此 list 中的群组可以执行此命令.默认为 [], 全部允许. + +`AllowUser` , int 类型的 list, 在此 list 中的QQ号可以执行此命令.默认为 [], 全部允许. + +`AllowRole` , string 类型的 list, 在此 list 中的角色可以执行此命令.默认为 [], 全部允许, 可选值为 owner、admin、member , 对应 群主、管理员、群员 三种角色. + +`GroupNotAllow`、`UserNotAllow`、`RoleNotAllow` 三个字符串, 分别为当用户因为以上三个原因被拒绝使用本命令时的回复, 自己去看默认值吧.. + +`GETMSG()` 方法, 该方法应当返回一个字符串, 在此方法中你可以借助以下变量: + +`self.uid `发起命令的用户的 QQ , int 类型 + +`self.gid` 发起命令的群组的群号, int 类型 + +`self.role` 发起命令的用户的角色,可能值 'owner'、'admin'、'member' + +`self.parms` 命令详情, string 类型的 list. 看例子 + +如果用户发送了 /ping xxx 12 + +那么 self.parms 为 ['ping', 'xxx', '12'] + +self.raw_msg 详见 example.json \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..1301d36 --- /dev/null +++ b/config.py @@ -0,0 +1,7 @@ +ALLWORKERS = ['ping','yiyan','mc',] + +APIURL = 'http://127.0.0.1:5700/' + +AUTHORIZATION = '' + +PORT = 5580 \ No newline at end of file diff --git a/exmple.json b/exmple.json new file mode 100644 index 0000000..bd8d1df --- /dev/null +++ b/exmple.json @@ -0,0 +1,25 @@ +{ + "anonymous": "None", + "font": 1501544, + "group_id": 959613000, + "message": "/ping", + "message_id": 2071, + "message_type": "group", + "post_type": "message", + "raw_message": "/ping", + "self_id": 161795000, + "sender": { + "age": 18, + "area": "济宁", + "card": "", + "level": "活跃", + "nickname": "SiHuan", + "role": "owner", + "sex": "unknown", + "title": "", + "user_id": 1318000000 + }, + "sub_type": "normal", + "time": 1582805370, + "user_id": 1318000000 +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c138b5c --- /dev/null +++ b/main.py @@ -0,0 +1,14 @@ +from flask import Flask, request +from config import PORT +from route import ZZRouter + +app = Flask(__name__) + +@app.route('/', methods=['POST']) +def whenmsg(): + ZZRouter(request.get_json()) + return '' + +app.run(host='0.0.0.0', port=PORT) +if __name__ == '__main__': + app.run() diff --git a/req.txt b/req.txt new file mode 100644 index 0000000..142eed5 --- /dev/null +++ b/req.txt @@ -0,0 +1,19 @@ +astroid==2.3.3 +certifi==2019.11.28 +chardet==3.0.4 +Click==7.0 +Flask==1.1.1 +idna==2.9 +isort==4.3.21 +itsdangerous==1.1.0 +Jinja2==2.11.1 +lazy-object-proxy==1.4.3 +MarkupSafe==1.1.1 +mccabe==0.6.1 +pylint==2.4.4 +requests==2.23.0 +six==1.14.0 +typed-ast==1.4.1 +urllib3==1.25.8 +Werkzeug==1.0.0 +wrapt==1.11.2 diff --git a/route.py b/route.py new file mode 100644 index 0000000..3fd872b --- /dev/null +++ b/route.py @@ -0,0 +1,20 @@ +from worker import emmm + +def ZZRouter(data): + uid = data['user_id'] + gid = data['group_id'] + role = data['sender']['role'] + parms = str.split(data['message'][1:]) + worker = parms[0] + + try: + package = __import__(name='worker.'+ worker, fromlist=worker) + Ans_ = getattr(package,'Ans') + except: + Ans_ = emmm.Ans + + Ans = Ans_(parms,uid,gid,role,data) + Message = Ans.CheckPermission() + if Message == 0: + Message = Ans.GETMSG() + Ans.sendmsg(Message) \ No newline at end of file diff --git a/worker/emmm.py b/worker/emmm.py new file mode 100644 index 0000000..8a470bd --- /dev/null +++ b/worker/emmm.py @@ -0,0 +1,8 @@ +from zzcore import StdAns + +class Ans(StdAns): + def GETMSG(self): + if self.parms: + return '咱也不知道' + self.parms[0] + '是啥呢~' + else: + return '汝再调戏咱,咱可就生气了!!' \ No newline at end of file diff --git a/worker/mc.py b/worker/mc.py new file mode 100644 index 0000000..138f7bc --- /dev/null +++ b/worker/mc.py @@ -0,0 +1,14 @@ +from zzcore import StdAns +from subprocess import getoutput + +class Ans(StdAns): + def GETMSG(self): + if self.parms[1] == 'status': + return getoutput('spigot status') + AllowCmd = ['list','tps'] + if self.parms[1] in AllowCmd: + cmd = 'spigot command ' + cmd = cmd + self.parms[1] + return getoutput(cmd) + else: + return self.parms[1] + ' 是暂时不被允许的命令!' \ No newline at end of file diff --git a/worker/ping.py b/worker/ping.py new file mode 100644 index 0000000..3d08025 --- /dev/null +++ b/worker/ping.py @@ -0,0 +1,5 @@ +from zzcore import StdAns + +class Ans(StdAns): + def GETMSG(self): + return 'Pong!' \ No newline at end of file diff --git a/worker/yiyan.py b/worker/yiyan.py new file mode 100644 index 0000000..b6d0e05 --- /dev/null +++ b/worker/yiyan.py @@ -0,0 +1,22 @@ +from zzcore import StdAns +import requests + +class Ans(StdAns): + def GETMSG(self): + url = 'https://v1.hitokoto.cn/?encode=text&' + cmdlst = {'动画': 'a', '漫画': 'b', '游戏': 'c', '文学': 'd', '原创': 'e', '来自网络': 'f', + '其他': 'g', '影视': 'h', '诗词': 'i', '网易云': 'j', '哲学': 'k', '抖机灵': 'l'} + try: + cmd = self.parms[1] + except : + cmd = '' + if cmd == '帮助': + msg = '您可以使用以下参数:\n 动画,漫画,游戏,文学,原创,来自网络,其他,影视,诗词,网易云,哲学,抖机灵' + else: + if cmd == '': + msg = requests.get(url).text + elif cmd in cmdlst: + msg = requests.get(url+cmdlst[cmd]).text + else: + msg = '我不知道 ' + cmd + ' 这个参数,你可以使用"/yiyan 帮助"来获取帮助' + return msg \ No newline at end of file diff --git a/zzcore.py b/zzcore.py new file mode 100644 index 0000000..16d48d6 --- /dev/null +++ b/zzcore.py @@ -0,0 +1,46 @@ +import requests +import json +from config import APIURL, ALLWORKERS, AUTHORIZATION +from worker import emmm + +class StdAns(): + AllowGroup = [] + AllowUser = [] + AllowRole = [] + GroupNotAllow = '汝所在的群组不被允许这样命令咱呢.' + UserNotAllow = '汝不被允许呢.' + RoleNotAllow = '汝的角色不被允许哦.' + + def __init__(self,parms,uid,gid,role,raw_msg): + self.parms = parms + self.uid = uid + self.gid = gid + self.role = role + self.raw_msg = raw_msg + + + def CheckPermission(self): + if self.AllowGroup and self.gid not in self.AllowGroup: + return self.GroupNotAllow + if self.AllowUser and self.uid not in self.AllowUser: + return self.UserNotAllow + if self.AllowRole and self.role not in self.AllowRole: + return self.RoleNotAllow + return 0 + + def GETMSG(self): + return self.__module__ +'的话,咱已经知道了,但是还在学习呢!' + + def sendmsg(self,msg): + url = APIURL + "send_msg" + Headers = { + 'content-type': 'application/json', + 'Authorization':'Bearer ' + AUTHORIZATION + } + + data = { + 'message_type' : 'group', + 'group_id' : self.gid, + 'message': msg + } + requests.post(url = url, data = json.dumps(data),headers = Headers)