@hieuzest/koishi-plugin-mahjongpub 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.d.ts +18 -0
- package/lib/api.js +81 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +151 -0
- package/lib/locales/zh-CN.json +1 -0
- package/package.json +20 -0
- package/readme.md +5 -0
package/lib/api.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Context, Quester } from 'koishi';
|
|
2
|
+
export declare class Team {
|
|
3
|
+
private pw;
|
|
4
|
+
private options;
|
|
5
|
+
tid: string;
|
|
6
|
+
cid: string;
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
players: string[];
|
|
10
|
+
img: string;
|
|
11
|
+
ready: boolean;
|
|
12
|
+
http: Quester;
|
|
13
|
+
constructor(ctx: Context, pw: string, options: {
|
|
14
|
+
endpoint: string;
|
|
15
|
+
});
|
|
16
|
+
read(): Promise<void>;
|
|
17
|
+
write(): Promise<any>;
|
|
18
|
+
}
|
package/lib/api.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Team = void 0;
|
|
4
|
+
class Team {
|
|
5
|
+
pw;
|
|
6
|
+
options;
|
|
7
|
+
tid;
|
|
8
|
+
cid;
|
|
9
|
+
name;
|
|
10
|
+
description;
|
|
11
|
+
players;
|
|
12
|
+
img;
|
|
13
|
+
ready;
|
|
14
|
+
http;
|
|
15
|
+
constructor(ctx, pw, options) {
|
|
16
|
+
this.pw = pw;
|
|
17
|
+
this.options = options;
|
|
18
|
+
console.log('endpoint', options.endpoint);
|
|
19
|
+
this.http = ctx.http.extend({
|
|
20
|
+
endpoint: options.endpoint,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
async read() {
|
|
24
|
+
const data = await this.http.get('/api/data.php', {
|
|
25
|
+
responseType: 'json',
|
|
26
|
+
params: {
|
|
27
|
+
t: 'tm_pw',
|
|
28
|
+
cid: this.pw,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
if (!data?.tid)
|
|
32
|
+
throw new Error('failed to read team info');
|
|
33
|
+
this.tid = data.tid;
|
|
34
|
+
this.cid = data.cid;
|
|
35
|
+
this.name = data.t_name;
|
|
36
|
+
this.description = data.t_ps;
|
|
37
|
+
this.players = [...(data.t_player?.split(/\s+/) ?? []), ...(data.t_sub?.split(/\s+/) ?? [])];
|
|
38
|
+
this.img = data.img;
|
|
39
|
+
this.ready = !!+(data.t_type ?? 0);
|
|
40
|
+
}
|
|
41
|
+
async write() {
|
|
42
|
+
const payload = {
|
|
43
|
+
posttype: 'update',
|
|
44
|
+
t_pw: this.pw,
|
|
45
|
+
t_ps: this.description,
|
|
46
|
+
cid: this.cid,
|
|
47
|
+
linkurl: this.img,
|
|
48
|
+
team_type: this.ready ? '1' : '0',
|
|
49
|
+
postdata: this.players.join('\n'),
|
|
50
|
+
};
|
|
51
|
+
const form = new FormData();
|
|
52
|
+
Object.entries(payload).forEach(([key, value]) => form.append(key, value));
|
|
53
|
+
console.log('payload', payload);
|
|
54
|
+
return (await this.http.post('/team_post.php', form, { responseType: 'json' }))?.msg;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Team = Team;
|
|
58
|
+
// class Contest {
|
|
59
|
+
// }
|
|
60
|
+
// namespace Contest {
|
|
61
|
+
// interface Payload {
|
|
62
|
+
// CS_EBD?: string
|
|
63
|
+
// JOIN_END?: string
|
|
64
|
+
// ONLY_GET?: string
|
|
65
|
+
// STOP_EDIT?: string
|
|
66
|
+
// bm_ed: string
|
|
67
|
+
// bm_st: string
|
|
68
|
+
// c_banner: string
|
|
69
|
+
// c_gonggao: string
|
|
70
|
+
// c_name: string
|
|
71
|
+
// c_pad: string
|
|
72
|
+
// c_round: string
|
|
73
|
+
// c_s_po: string
|
|
74
|
+
// c_sub_t: string
|
|
75
|
+
// cid: string
|
|
76
|
+
// r_type: string
|
|
77
|
+
// t_max: string
|
|
78
|
+
// t_sub: string
|
|
79
|
+
// t_type: string
|
|
80
|
+
// }
|
|
81
|
+
// }
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
declare module 'koishi' {
|
|
3
|
+
interface User {
|
|
4
|
+
'mahjongpub/bind-team': string;
|
|
5
|
+
}
|
|
6
|
+
interface Channel {
|
|
7
|
+
'mahjongpub/bind-team': string;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare class MahjongPub {
|
|
11
|
+
constructor(ctx: Context, config: MahjongPub.Config);
|
|
12
|
+
}
|
|
13
|
+
export declare namespace MahjongPub {
|
|
14
|
+
interface Config {
|
|
15
|
+
informNotbind: boolean;
|
|
16
|
+
endpoint: string;
|
|
17
|
+
}
|
|
18
|
+
const Config: Schema<Config>;
|
|
19
|
+
}
|
|
20
|
+
export default MahjongPub;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MahjongPub = void 0;
|
|
4
|
+
const koishi_1 = require("koishi");
|
|
5
|
+
const api_1 = require("./api");
|
|
6
|
+
class MahjongPub {
|
|
7
|
+
constructor(ctx, config) {
|
|
8
|
+
ctx.i18n.define('zh-CN', require('./locales/zh-CN'));
|
|
9
|
+
ctx.model.extend('user', {
|
|
10
|
+
'mahjongpub/bind-team': 'string',
|
|
11
|
+
});
|
|
12
|
+
ctx.model.extend('channel', {
|
|
13
|
+
'mahjongpub/bind-team': 'string',
|
|
14
|
+
});
|
|
15
|
+
ctx.command('mahjongpub.team.bind <pw:string>', { admin: { user: true, channel: true } })
|
|
16
|
+
.alias('!绑定', '!绑定')
|
|
17
|
+
.userFields(['mahjongpub/bind-team'])
|
|
18
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
19
|
+
.action(async ({ session }, pw) => {
|
|
20
|
+
if (pw?.length !== 20)
|
|
21
|
+
return;
|
|
22
|
+
const team = new api_1.Team(ctx, pw, config);
|
|
23
|
+
try {
|
|
24
|
+
await team.read();
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
ctx.logger.warn(e);
|
|
28
|
+
return session.text('.failed');
|
|
29
|
+
}
|
|
30
|
+
if (session.isDirect)
|
|
31
|
+
session.user['mahjongpub/bind-team'] = pw;
|
|
32
|
+
else
|
|
33
|
+
session.channel['mahjongpub/bind-team'] = pw;
|
|
34
|
+
return session.text('.success', team);
|
|
35
|
+
});
|
|
36
|
+
ctx.command('mahjongpub.team.unbind', { admin: { user: true, channel: true } })
|
|
37
|
+
.alias('!解绑', '!解绑')
|
|
38
|
+
.userFields(['mahjongpub/bind-team'])
|
|
39
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
40
|
+
.action(async ({ session }) => {
|
|
41
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
42
|
+
if (!pw)
|
|
43
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
44
|
+
if (session.isDirect)
|
|
45
|
+
session.user['mahjongpub/bind-team'] = '';
|
|
46
|
+
else
|
|
47
|
+
session.channel['mahjongpub/bind-team'] = '';
|
|
48
|
+
return session.text('.success');
|
|
49
|
+
});
|
|
50
|
+
ctx.private().command('mahjongpub.team.password', { admin: { user: true, channel: true } })
|
|
51
|
+
.alias('!密码', '!密码')
|
|
52
|
+
.userFields(['mahjongpub/bind-team'])
|
|
53
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
54
|
+
.action(async ({ session }) => {
|
|
55
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
56
|
+
if (!pw)
|
|
57
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
58
|
+
return session.text('.output', [pw]);
|
|
59
|
+
});
|
|
60
|
+
ctx.command('mahjongpub.team.stats', { admin: { user: true, channel: true } })
|
|
61
|
+
.alias('!信息', '!信息', '!概况', '!概况')
|
|
62
|
+
.userFields(['mahjongpub/bind-team'])
|
|
63
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
64
|
+
.action(async ({ session }) => {
|
|
65
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
66
|
+
if (!pw)
|
|
67
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
68
|
+
const team = new api_1.Team(ctx, pw, config);
|
|
69
|
+
try {
|
|
70
|
+
await team.read();
|
|
71
|
+
return session.text('.output', team);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
ctx.logger.warn(e);
|
|
75
|
+
return session.text('.failed');
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
ctx.command('mahjongpub.team.add <...users:string>', { admin: { user: true, channel: true } })
|
|
79
|
+
.alias('!添加', '!添加')
|
|
80
|
+
.userFields(['mahjongpub/bind-team'])
|
|
81
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
82
|
+
.action(async ({ session }, ...users) => {
|
|
83
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
84
|
+
if (!pw)
|
|
85
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
86
|
+
const team = new api_1.Team(ctx, pw, config);
|
|
87
|
+
try {
|
|
88
|
+
await team.read();
|
|
89
|
+
team.players.push(...users);
|
|
90
|
+
await team.write();
|
|
91
|
+
return await team.write() ?? session.text('.success', team);
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
ctx.logger.warn(e);
|
|
95
|
+
return session.text('.failed');
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
ctx.command('mahjongpub.team.remove <...indices:natural>', { admin: { user: true, channel: true } })
|
|
99
|
+
.alias('!删除', '!删除')
|
|
100
|
+
.userFields(['mahjongpub/bind-team'])
|
|
101
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
102
|
+
.action(async ({ session }, ...indices) => {
|
|
103
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
104
|
+
if (!pw)
|
|
105
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
106
|
+
const team = new api_1.Team(ctx, pw, config);
|
|
107
|
+
try {
|
|
108
|
+
await team.read();
|
|
109
|
+
team.players = team.players.filter((_, i) => !indices.includes(i + 1));
|
|
110
|
+
await team.write();
|
|
111
|
+
return await team.write() ?? session.text('.success', team);
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
ctx.logger.warn(e);
|
|
115
|
+
return session.text('.failed');
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
ctx.command('mahjongpub.team.swap <...indices:natural>', { admin: { user: true, channel: true } })
|
|
119
|
+
.alias('!交换', '!交换')
|
|
120
|
+
.userFields(['mahjongpub/bind-team'])
|
|
121
|
+
.channelFields(['mahjongpub/bind-team'])
|
|
122
|
+
.action(async ({ session }, ...indices) => {
|
|
123
|
+
const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
|
|
124
|
+
if (!pw)
|
|
125
|
+
return config.informNotbind ? session.text('.notbind') : '';
|
|
126
|
+
if (indices.length % 2 !== 0)
|
|
127
|
+
return session.text('.invalid');
|
|
128
|
+
const team = new api_1.Team(ctx, pw, config);
|
|
129
|
+
try {
|
|
130
|
+
await team.read();
|
|
131
|
+
team.players = team.players.map((player, i) => {
|
|
132
|
+
const index = indices.indexOf(i + 1);
|
|
133
|
+
return index === -1 ? player : team.players[indices[index + (index % 2 === 0 ? 1 : -1)] - 1];
|
|
134
|
+
});
|
|
135
|
+
return await team.write() ?? session.text('.success', team);
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
ctx.logger.warn(e);
|
|
139
|
+
return session.text('.failed');
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.MahjongPub = MahjongPub;
|
|
145
|
+
(function (MahjongPub) {
|
|
146
|
+
MahjongPub.Config = koishi_1.Schema.object({
|
|
147
|
+
informNotbind: koishi_1.Schema.boolean().default(false),
|
|
148
|
+
endpoint: koishi_1.Schema.string().default('https://cdn.r-mj.com/'),
|
|
149
|
+
});
|
|
150
|
+
})(MahjongPub || (exports.MahjongPub = MahjongPub = {}));
|
|
151
|
+
exports.default = MahjongPub;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"commands":{"mahjongpub.team.bind":{"description":"绑定队伍","messages":{"failed":"绑定失败","success":"成功: [{cid}:{tid}] {name}"}},"mahjongpub.team.unbind":{"description":"解除绑定","messages":{"notbind":"未绑定","success":"成功"}},"mahjongpub.team.password":{"description":"查看密码","messages":{"notbind":"未绑定","output":"密码: {0}"}},"mahjongpub.team.stats":{"description":"查看队伍信息","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.add":{"description":"添加队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.remove":{"description":"删除队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.swap":{"description":"交换队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}}}}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hieuzest/koishi-plugin-mahjongpub",
|
|
3
|
+
"description": "Mahjong.pub API",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"chatbot",
|
|
14
|
+
"koishi",
|
|
15
|
+
"plugin"
|
|
16
|
+
],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"koishi": "^4.17.1"
|
|
19
|
+
}
|
|
20
|
+
}
|