@hieuzest/koishi-plugin-mahjongpub 0.1.1 → 0.1.3

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.js CHANGED
@@ -15,7 +15,6 @@ class Team {
15
15
  constructor(ctx, pw, options) {
16
16
  this.pw = pw;
17
17
  this.options = options;
18
- console.log('endpoint', options.endpoint);
19
18
  this.http = ctx.http.extend({
20
19
  endpoint: options.endpoint,
21
20
  });
@@ -50,7 +49,6 @@ class Team {
50
49
  };
51
50
  const form = new FormData();
52
51
  Object.entries(payload).forEach(([key, value]) => form.append(key, value));
53
- console.log('payload', payload);
54
52
  return (await this.http.post('/team_post.php', form, { responseType: 'json' }))?.msg;
55
53
  }
56
54
  }
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,12 @@ 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('channel', '-c <channel:channel>')
17
24
  .userFields(['mahjongpub/bind-team'])
18
25
  .channelFields(['mahjongpub/bind-team'])
19
- .action(async ({ session }, pw) => {
26
+ .action(async ({ session, options }, pw) => {
20
27
  if (pw?.length !== 20)
21
28
  return;
22
29
  const team = new api_1.Team(ctx, pw, config);
@@ -27,18 +34,22 @@ class MahjongPub {
27
34
  ctx.logger.warn(e);
28
35
  return session.text('.failed');
29
36
  }
30
- if (session.isDirect)
37
+ if (options.channel)
38
+ ctx.database.setChannel(...parsePlatform(options.channel), { 'mahjongpub/bind-team': pw });
39
+ else if (session.isDirect)
31
40
  session.user['mahjongpub/bind-team'] = pw;
32
41
  else
33
42
  session.channel['mahjongpub/bind-team'] = pw;
34
43
  return session.text('.success', team);
35
44
  });
36
- ctx.command('mahjongpub.team.unbind', { admin: { user: true, channel: true } })
45
+ ctx.command('mahjongpub.team.unbind')
37
46
  .alias('!解绑', '!解绑')
47
+ .option('channel', '-c <channel:channel>')
38
48
  .userFields(['mahjongpub/bind-team'])
39
49
  .channelFields(['mahjongpub/bind-team'])
40
- .action(async ({ session }) => {
41
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
50
+ .action(async ({ session, options }) => {
51
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
52
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
42
53
  if (!pw)
43
54
  return config.informNotbind ? session.text('.notbind') : '';
44
55
  if (session.isDirect)
@@ -47,24 +58,23 @@ class MahjongPub {
47
58
  session.channel['mahjongpub/bind-team'] = '';
48
59
  return session.text('.success');
49
60
  });
50
- ctx.private().command('mahjongpub.team.password', { admin: { user: true, channel: true } })
61
+ ctx.private().command('mahjongpub.team.password')
51
62
  .alias('!密码', '!密码')
52
63
  .userFields(['mahjongpub/bind-team'])
53
- .channelFields(['mahjongpub/bind-team'])
54
64
  .action(async ({ session }) => {
55
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
65
+ const pw = session.user['mahjongpub/bind-team'];
56
66
  if (!pw)
57
67
  return config.informNotbind ? session.text('.notbind') : '';
58
68
  return session.text('.output', [pw]);
59
69
  });
60
- ctx.command('mahjongpub.team.stats', { admin: { user: true, channel: true } })
70
+ ctx.command('mahjongpub.team.stats')
61
71
  .alias('!信息', '!信息', '!概况', '!概况')
72
+ .option('channel', '-c <channel:channel>')
62
73
  .userFields(['mahjongpub/bind-team'])
63
74
  .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') : '';
75
+ .action(async ({ session, options }) => {
76
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
77
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
68
78
  const team = new api_1.Team(ctx, pw, config);
69
79
  try {
70
80
  await team.read();
@@ -75,12 +85,14 @@ class MahjongPub {
75
85
  return session.text('.failed');
76
86
  }
77
87
  });
78
- ctx.command('mahjongpub.team.add <...users:string>', { admin: { user: true, channel: true } })
88
+ ctx.command('mahjongpub.team.add <...users:string>')
79
89
  .alias('!添加', '!添加')
90
+ .option('channel', '-c <channel:channel>')
80
91
  .userFields(['mahjongpub/bind-team'])
81
92
  .channelFields(['mahjongpub/bind-team'])
82
- .action(async ({ session }, ...users) => {
83
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
93
+ .action(async ({ session, options }, ...users) => {
94
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
95
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
84
96
  if (!pw)
85
97
  return config.informNotbind ? session.text('.notbind') : '';
86
98
  const team = new api_1.Team(ctx, pw, config);
@@ -95,12 +107,14 @@ class MahjongPub {
95
107
  return session.text('.failed');
96
108
  }
97
109
  });
98
- ctx.command('mahjongpub.team.remove <...indices:natural>', { admin: { user: true, channel: true } })
110
+ ctx.command('mahjongpub.team.remove <...indices:natural>')
99
111
  .alias('!删除', '!删除')
112
+ .option('channel', '-c <channel:channel>')
100
113
  .userFields(['mahjongpub/bind-team'])
101
114
  .channelFields(['mahjongpub/bind-team'])
102
- .action(async ({ session }, ...indices) => {
103
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
115
+ .action(async ({ session, options }, ...indices) => {
116
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
117
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
104
118
  if (!pw)
105
119
  return config.informNotbind ? session.text('.notbind') : '';
106
120
  const team = new api_1.Team(ctx, pw, config);
@@ -115,12 +129,14 @@ class MahjongPub {
115
129
  return session.text('.failed');
116
130
  }
117
131
  });
118
- ctx.command('mahjongpub.team.swap <...indices:natural>', { admin: { user: true, channel: true } })
132
+ ctx.command('mahjongpub.team.swap <...indices:natural>')
119
133
  .alias('!交换', '!交换')
134
+ .option('channel', '-c <channel:channel>')
120
135
  .userFields(['mahjongpub/bind-team'])
121
136
  .channelFields(['mahjongpub/bind-team'])
122
- .action(async ({ session }, ...indices) => {
123
- const pw = session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
137
+ .action(async ({ session, options }, ...indices) => {
138
+ const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
139
+ : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'];
124
140
  if (!pw)
125
141
  return config.informNotbind ? session.text('.notbind') : '';
126
142
  if (indices.length % 2 !== 0)
@@ -128,10 +144,11 @@ class MahjongPub {
128
144
  const team = new api_1.Team(ctx, pw, config);
129
145
  try {
130
146
  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
- });
147
+ for (let i = 0; i < indices.length; i += 2) {
148
+ const t = team.players[indices[i] - 1];
149
+ team.players[indices[i] - 1] = team.players[indices[i + 1] - 1];
150
+ team.players[indices[i + 1] - 1] = t;
151
+ }
135
152
  return await team.write() ?? session.text('.success', team);
136
153
  }
137
154
  catch (e) {
@@ -143,6 +160,9 @@ class MahjongPub {
143
160
  }
144
161
  exports.MahjongPub = MahjongPub;
145
162
  (function (MahjongPub) {
163
+ MahjongPub.inject = {
164
+ required: ['database'],
165
+ };
146
166
  MahjongPub.Config = koishi_1.Schema.object({
147
167
  informNotbind: koishi_1.Schema.boolean().default(false),
148
168
  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.1",
4
+ "version": "0.1.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [