koishi-plugin-smmcat-dontstarveapi 0.0.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/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "smmcat-dontstarveapi";
3
+ export interface Config {
4
+ }
5
+ export declare const inject: string[];
6
+ export declare const Config: Schema<Config>;
7
+ export declare function apply(ctx: Context): void;
package/lib/index.js ADDED
@@ -0,0 +1,250 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name2 in all)
10
+ __defProp(target, name2, { get: all[name2], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Config: () => Config,
34
+ apply: () => apply,
35
+ inject: () => inject,
36
+ name: () => name
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+ var import_koishi = require("koishi");
40
+ var import_fs = __toESM(require("fs"));
41
+ var import_path = __toESM(require("path"));
42
+ var name = "smmcat-dontstarveapi";
43
+ var inject = ["localstorage"];
44
+ var Config = import_koishi.Schema.object({});
45
+ function apply(ctx) {
46
+ const baseApi = "https://api.dstserverlist.top/api/list/";
47
+ const dontstarve = {
48
+ guildBind: {},
49
+ // 初始化信息
50
+ async initData() {
51
+ const dirPath = import_path.default.join(ctx.localstorage.basePath, "smm_dontstarve");
52
+ if (!import_fs.default.existsSync(dirPath)) {
53
+ await import_fs.default.promises.mkdir(dirPath, { recursive: true });
54
+ }
55
+ const guildBindMap = import_fs.default.readdirSync(dirPath);
56
+ const guildBind = {};
57
+ const dict = { ok: 0, err: 0 };
58
+ const eventList = guildBindMap.map((guildId) => {
59
+ return new Promise(async (resolve, reject) => {
60
+ try {
61
+ guildBind[guildId] = JSON.parse(await ctx.localstorage.getItem(`smm_dontstarve/${guildId}`) || "{}");
62
+ dict.ok++;
63
+ resolve(true);
64
+ } catch (error) {
65
+ dict.err++;
66
+ resolve(false);
67
+ }
68
+ });
69
+ });
70
+ await Promise.all(eventList);
71
+ console.log(`[smmcat-dontstarveapi] 饥荒服务器绑定加载完成,成功${dict.ok}个,失败${dict.err}个`);
72
+ this.guildBind = guildBind;
73
+ console.log(this.guildBind);
74
+ },
75
+ // 添加绑定
76
+ async addBind(guildId, { RowId = "", serveName = "" }) {
77
+ if (!guildId)
78
+ return { code: false, msg: "绑定失败,请在群内使用" };
79
+ this.guildBind[guildId] = { RowId, serveName };
80
+ await this.updateGuildBind(guildId);
81
+ return { code: true, msg: `与饥荒服务器 ${serveName} 绑定成功,
82
+ 如若需要解绑,可以发送 /移除绑定` };
83
+ },
84
+ // 移除绑定
85
+ async removeBind(guildId) {
86
+ if (!this.guildBind[guildId] || !this.guildBind[guildId]?.RowId) {
87
+ return { code: false, msg: "你群还没和任何饥荒服务器绑定呢!\n可以通过 /查询服务器 关键字 获取对应服务器的 RowId 进行绑定" };
88
+ }
89
+ this.guildBind[guildId] = { RowId: "", serveName: "" };
90
+ await this.updateGuildBind(guildId);
91
+ return { code: true, msg: "移除成功" };
92
+ },
93
+ // 查看绑定
94
+ getBind(guildId) {
95
+ if (!this.guildBind[guildId] || !this.guildBind[guildId]?.RowId) {
96
+ return { code: false, msg: "你群还没和任何饥荒服务器绑定呢!\n可以通过 /查询服务器 关键字 获取对应服务器的 RowId 进行绑定" };
97
+ }
98
+ return { code: true, msg: `本群绑定的服务器名:${this.guildBind[guildId].serveName}
99
+ 服务器RowId:${this.guildBind[guildId].RowId}` };
100
+ },
101
+ // 返回群绑定的 信息
102
+ getRowId(guildId) {
103
+ if (!this.guildBind[guildId] || !this.guildBind[guildId]?.RowId) {
104
+ return { code: false, msg: "你群还没和任何饥荒服务器绑定呢!\n可以通过 /查询服务器 关键字 获取对应服务器的 RowId 进行绑定" };
105
+ }
106
+ return { code: true, info: this.guildBind[guildId] };
107
+ },
108
+ // 更新本地数据
109
+ async updateGuildBind(guildId) {
110
+ const temp = this.guildBind[guildId];
111
+ await ctx.localstorage.setItem(`smm_dontstarve/${guildId}`, JSON.stringify(temp));
112
+ }
113
+ };
114
+ const webGet = {
115
+ // 获取服务器列表
116
+ async getServerList(name2) {
117
+ try {
118
+ return await ctx.http.post(baseApi + `?name=${encodeURIComponent(name2)}&pageCount=10&page=0`);
119
+ } catch (error) {
120
+ console.log(error);
121
+ }
122
+ },
123
+ // 获取服务器详情
124
+ async getServerDetail(RowId) {
125
+ try {
126
+ return await ctx.http.post(`https://api.dstserverlist.top/api/v2/server/details?id=${RowId}&forceUpdate=false`);
127
+ } catch (error) {
128
+ console.log(error);
129
+ }
130
+ }
131
+ };
132
+ ctx.on("ready", () => {
133
+ dontstarve.initData();
134
+ });
135
+ ctx.command("饥荒查服");
136
+ ctx.command("饥荒查服/查询服务器 <keyword>").action(async ({ session }, keyword) => {
137
+ if (!keyword) {
138
+ await session.send("请输入关键字");
139
+ return;
140
+ }
141
+ const result = await webGet.getServerList(keyword);
142
+ if (!result) {
143
+ await session.send("获取失败");
144
+ return;
145
+ }
146
+ const msgList = result.List.length > 0 ? `一共获得 ${result.AllCount < 10 ? result.AllCount : `${result.AllCount} (最多显示 10 个)`} 个服务器信息:
147
+
148
+ ` + result.List.map((item, index) => {
149
+ return `[RowId] ${item.RowId}
150
+ [服务器名] ${item.Name}
151
+ [模式] ${item.Mode} / ${item.Intent}
152
+ [在线人数] ${item.Connected} / ${item.MaxConnections}
153
+ [当前季节] ${item.Season} [平台] ${item.Platform}`;
154
+ }).join("\n\n") + "\n\n若群内需要绑定对应的服务器,则为 /设置绑定 {RowId}" : "没有查询到任何服务器信息...";
155
+ await session.send(msgList);
156
+ });
157
+ ctx.command("饥荒查服/设置绑定 <RowId>").action(async ({ session }, RowId) => {
158
+ if (!session.guildId) {
159
+ await session.send("请在群内使用这个功能");
160
+ return;
161
+ }
162
+ if (!RowId) {
163
+ await session.send("请输入饥荒服务器对应的 RowId!\ntip: 可以通过 /查询服务器 关键字 获得。");
164
+ return;
165
+ }
166
+ const result = await webGet.getServerDetail(RowId);
167
+ const server = result.Server;
168
+ if (!result || !result.Server) {
169
+ await session.send("绑定失败,检查你的 RowId 是否正确?");
170
+ return;
171
+ }
172
+ console.log(server);
173
+ await session.send(`你群即将与名为 ${server.Name} 的服务器世界绑定。
174
+ 绑定后可使用 /本群服务器 指令来查看本群服务器详情
175
+ 如若确定,请在10秒内发送 是`);
176
+ const type = await session.prompt(1e4);
177
+ if (type !== "是") {
178
+ return;
179
+ }
180
+ const msginfo = await dontstarve.addBind(session.guildId, { RowId: server.RowId, serveName: server.Name });
181
+ await session.send(msginfo.msg);
182
+ });
183
+ ctx.command("饥荒查服/移除绑定").action(async ({ session }, RowId) => {
184
+ if (!session.guildId) {
185
+ await session.send("请在群内使用这个功能");
186
+ return;
187
+ }
188
+ const guildBind = dontstarve.getRowId(session.guildId);
189
+ if (!guildBind.code) {
190
+ await session.send(guildBind.msg);
191
+ return;
192
+ }
193
+ await session.send(`你群即将移除与 ${guildBind.info.serveName} 的服务器世界绑定。
194
+ 如若确定,请在10秒内发送 是`);
195
+ const type = await session.prompt(1e4);
196
+ if (type !== "是") {
197
+ return;
198
+ }
199
+ const result = await dontstarve.removeBind(session.guildId);
200
+ await session.send(result.msg);
201
+ });
202
+ ctx.command("饥荒查服/查看绑定").action(async ({ session }) => {
203
+ if (!session.guildId) {
204
+ await session.send("请在群内使用这个功能");
205
+ return;
206
+ }
207
+ const result = dontstarve.getBind(session.guildId);
208
+ await session.send(result.msg);
209
+ });
210
+ ctx.command("饥荒查服/本群服务器").action(async ({ session }) => {
211
+ const guildBind = dontstarve.getRowId(session.guildId);
212
+ if (!guildBind.code) {
213
+ await session.send(guildBind.msg);
214
+ return;
215
+ }
216
+ await session.send(`本群绑定的饥荒服务器为:${guildBind.info.serveName}
217
+ 稍等,正在获取...`);
218
+ const result = await webGet.getServerDetail(guildBind.info.RowId);
219
+ const server = result.Server;
220
+ const dict = { spring: "春", summer: "夏", autumn: "秋", winter: "冬", survival: "生存", relaxed: "轻松", adventure: "冒险", endless: "无尽", cooperative: "合作" };
221
+ const msg = `该服务器以下详情:
222
+
223
+ [服务器名] ${server.Name} (${server.Address.IsoCode})
224
+ [服务器平台] ${server.Platform}
225
+ [服务器版本] ${server.Version}
226
+ [服务器简介] ${server.Description?.length > 10 ? server.Description.slice(0, 10) + "..." : server.Description}
227
+ [服务器状态] ${server.IsServerPaused ? "暂停" : "运行"}
228
+
229
+ [模式] ${dict[server.Mode] ? dict[server.Mode] : server.Mode} / ${dict[server.Intent] ? dict[server.Intent] : server.Intent}
230
+ [模组数量] ${!server.IsMods ? "无" : server.ModsInfo.length}
231
+ [在线人数] ${server.Connected} / ${server.MaxConnections}
232
+ [世界天数] ${server.DaysInfo.Day}天 (${dict[server.Season]} ${server.DaysInfo.DaysElapsedInSeason + 1}/${server.DaysInfo.TotalDaysSeason})
233
+ [是否友伤] ${server.IsPvp ? "是" : "否"}
234
+ [密码保护] ${server.IsPassword ? "是" : "否"}
235
+ [延迟] ${server.LastPing ? server.LastPing + "📶" : "???"}` + (server.Players.length ? `
236
+
237
+ 当前正在游玩的玩家:
238
+ ` + server.Players.map((item) => `${item.Name}(${item.Prefab})`).join("\n") : "");
239
+ console.log(result);
240
+ await session.send(msg);
241
+ });
242
+ }
243
+ __name(apply, "apply");
244
+ // Annotate the CommonJS export names for ESM import in node:
245
+ 0 && (module.exports = {
246
+ Config,
247
+ apply,
248
+ inject,
249
+ name
250
+ });
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "koishi-plugin-smmcat-dontstarveapi",
3
+ "description": "饥荒服务器查询",
4
+ "version": "0.0.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.9"
19
+ }
20
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-smmcat-dontstarveapi
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-smmcat-dontstarveapi?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-smmcat-dontstarveapi)
4
+
5
+ 饥荒服务器查询