@hieuzest/koishi-plugin-mahjongpub 0.1.2 → 0.1.4

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/index.d.ts CHANGED
@@ -11,6 +11,9 @@ export declare class MahjongPub {
11
11
  constructor(ctx: Context, config: MahjongPub.Config);
12
12
  }
13
13
  export declare namespace MahjongPub {
14
+ const inject: {
15
+ required: string[];
16
+ };
14
17
  interface Config {
15
18
  informNotbind: boolean;
16
19
  endpoint: string;
package/lib/index.js CHANGED
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MahjongPub = void 0;
4
4
  const koishi_1 = require("koishi");
5
5
  const api_1 = require("./api");
6
+ function parsePlatform(target) {
7
+ const index = target.startsWith('sandbox:') ? target.lastIndexOf(':') : target.indexOf(':');
8
+ const platform = target.slice(0, index);
9
+ const id = target.slice(index + 1);
10
+ return [platform, id];
11
+ }
6
12
  class MahjongPub {
7
13
  constructor(ctx, config) {
8
14
  ctx.i18n.define('zh-CN', require('./locales/zh-CN'));
@@ -12,11 +18,13 @@ class MahjongPub {
12
18
  ctx.model.extend('channel', {
13
19
  'mahjongpub/bind-team': 'string',
14
20
  });
15
- ctx.command('mahjongpub.team.bind <pw:string>', { admin: { user: true, channel: true } })
21
+ ctx.command('mahjongpub.team.bind <pw:string>')
16
22
  .alias('!绑定', '!绑定')
23
+ .option('user', '-u <user:user>')
24
+ .option('channel', '-c <channel:channel>')
17
25
  .userFields(['mahjongpub/bind-team'])
18
26
  .channelFields(['mahjongpub/bind-team'])
19
- .action(async ({ session }, pw) => {
27
+ .action(async ({ session, options }, pw) => {
20
28
  if (pw?.length !== 20)
21
29
  return;
22
30
  const team = new api_1.Team(ctx, pw, config);
@@ -27,18 +35,24 @@ class MahjongPub {
27
35
  ctx.logger.warn(e);
28
36
  return session.text('.failed');
29
37
  }
30
- if (session.isDirect)
38
+ if (options.user)
39
+ ctx.database.setUser(...parsePlatform(options.user), { 'mahjongpub/bind-team': pw });
40
+ else if (options.channel)
41
+ ctx.database.setChannel(...parsePlatform(options.channel), { 'mahjongpub/bind-team': pw });
42
+ else if (session.isDirect)
31
43
  session.user['mahjongpub/bind-team'] = pw;
32
44
  else
33
45
  session.channel['mahjongpub/bind-team'] = pw;
34
46
  return session.text('.success', team);
35
47
  });
36
- ctx.command('mahjongpub.team.unbind', { admin: { user: true, channel: true } })
48
+ ctx.command('mahjongpub.team.unbind')
37
49
  .alias('!解绑', '!解绑')
50
+ .option('channel', '-c <channel:channel>')
38
51
  .userFields(['mahjongpub/bind-team'])
39
52
  .channelFields(['mahjongpub/bind-team'])
40
- .action(async ({ session }) => {
41
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
53
+ .action(async ({ session, options }) => {
54
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
55
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
42
56
  if (!pw)
43
57
  return config.informNotbind ? session.text('.notbind') : '';
44
58
  if (session.isDirect)
@@ -47,22 +61,23 @@ class MahjongPub {
47
61
  session.channel['mahjongpub/bind-team'] = '';
48
62
  return session.text('.success');
49
63
  });
50
- ctx.private().command('mahjongpub.team.password', { admin: { user: true, channel: true } })
64
+ ctx.private().command('mahjongpub.team.password')
51
65
  .alias('!密码', '!密码')
52
66
  .userFields(['mahjongpub/bind-team'])
53
- .channelFields(['mahjongpub/bind-team'])
54
67
  .action(async ({ session }) => {
55
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
68
+ const pw = session.user['mahjongpub/bind-team'];
56
69
  if (!pw)
57
70
  return config.informNotbind ? session.text('.notbind') : '';
58
71
  return session.text('.output', [pw]);
59
72
  });
60
- ctx.command('mahjongpub.team.stats', { admin: { user: true, channel: true } })
73
+ ctx.command('mahjongpub.team.stats')
61
74
  .alias('!信息', '!信息', '!概况', '!概况')
75
+ .option('channel', '-c <channel:channel>')
62
76
  .userFields(['mahjongpub/bind-team'])
63
77
  .channelFields(['mahjongpub/bind-team'])
64
- .action(async ({ session }) => {
65
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
78
+ .action(async ({ session, options }) => {
79
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
80
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
66
81
  if (!pw)
67
82
  return config.informNotbind ? session.text('.notbind') : '';
68
83
  const team = new api_1.Team(ctx, pw, config);
@@ -75,12 +90,14 @@ class MahjongPub {
75
90
  return session.text('.failed');
76
91
  }
77
92
  });
78
- ctx.command('mahjongpub.team.add <...users:string>', { admin: { user: true, channel: true } })
93
+ ctx.command('mahjongpub.team.add <...users:string>')
79
94
  .alias('!添加', '!添加')
95
+ .option('channel', '-c <channel:channel>')
80
96
  .userFields(['mahjongpub/bind-team'])
81
97
  .channelFields(['mahjongpub/bind-team'])
82
- .action(async ({ session }, ...users) => {
83
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
98
+ .action(async ({ session, options }, ...users) => {
99
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
100
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
84
101
  if (!pw)
85
102
  return config.informNotbind ? session.text('.notbind') : '';
86
103
  const team = new api_1.Team(ctx, pw, config);
@@ -95,12 +112,14 @@ class MahjongPub {
95
112
  return session.text('.failed');
96
113
  }
97
114
  });
98
- ctx.command('mahjongpub.team.remove <...indices:natural>', { admin: { user: true, channel: true } })
115
+ ctx.command('mahjongpub.team.remove <...indices:natural>')
99
116
  .alias('!删除', '!删除')
117
+ .option('channel', '-c <channel:channel>')
100
118
  .userFields(['mahjongpub/bind-team'])
101
119
  .channelFields(['mahjongpub/bind-team'])
102
- .action(async ({ session }, ...indices) => {
103
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
120
+ .action(async ({ session, options }, ...indices) => {
121
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
122
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
104
123
  if (!pw)
105
124
  return config.informNotbind ? session.text('.notbind') : '';
106
125
  const team = new api_1.Team(ctx, pw, config);
@@ -115,12 +134,14 @@ class MahjongPub {
115
134
  return session.text('.failed');
116
135
  }
117
136
  });
118
- ctx.command('mahjongpub.team.swap <...indices:natural>', { admin: { user: true, channel: true } })
137
+ ctx.command('mahjongpub.team.swap <...indices:natural>')
119
138
  .alias('!交换', '!交换')
139
+ .option('channel', '-c <channel:channel>')
120
140
  .userFields(['mahjongpub/bind-team'])
121
141
  .channelFields(['mahjongpub/bind-team'])
122
- .action(async ({ session }, ...indices) => {
123
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
142
+ .action(async ({ session, options }, ...indices) => {
143
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
144
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
124
145
  if (!pw)
125
146
  return config.informNotbind ? session.text('.notbind') : '';
126
147
  if (indices.length % 2 !== 0)
@@ -144,6 +165,9 @@ class MahjongPub {
144
165
  }
145
166
  exports.MahjongPub = MahjongPub;
146
167
  (function (MahjongPub) {
168
+ MahjongPub.inject = {
169
+ required: ['database'],
170
+ };
147
171
  MahjongPub.Config = koishi_1.Schema.object({
148
172
  informNotbind: koishi_1.Schema.boolean().default(false),
149
173
  endpoint: koishi_1.Schema.string().default('https://cdn.r-mj.com/'),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hieuzest/koishi-plugin-mahjongpub",
3
3
  "description": "Mahjong.pub API",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [