koishi-plugin-cs2-update-log 2.1.0 → 2.1.2
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/README.md +1 -1
- package/lib/index.js +90 -9
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/index.js
CHANGED
|
@@ -103,7 +103,12 @@ function apply(ctx, config) {
|
|
|
103
103
|
async function fetchNews() {
|
|
104
104
|
try {
|
|
105
105
|
const xml = await ctx.http.get(STEAM_RSS_URL, {
|
|
106
|
+
headers: {
|
|
107
|
+
Accept: 'application/rss+xml, application/xml, text/xml;q=0.9, */*;q=0.8',
|
|
108
|
+
'User-Agent': 'koishi-plugin-cs2-update-log/2.1',
|
|
109
|
+
},
|
|
106
110
|
responseType: 'text',
|
|
111
|
+
timeout: 30000,
|
|
107
112
|
});
|
|
108
113
|
const parsed = rssParser.parse(xml);
|
|
109
114
|
const rawItems = parsed.rss?.channel?.item;
|
|
@@ -114,7 +119,7 @@ function apply(ctx, config) {
|
|
|
114
119
|
.slice(0, config.count);
|
|
115
120
|
}
|
|
116
121
|
catch (error) {
|
|
117
|
-
logger.error('拉取 Steam CS2 RSS
|
|
122
|
+
logger.error('拉取 Steam CS2 RSS 失败:url=%s\n%s', STEAM_RSS_URL, formatError(error));
|
|
118
123
|
return [];
|
|
119
124
|
}
|
|
120
125
|
}
|
|
@@ -157,15 +162,18 @@ function apply(ctx, config) {
|
|
|
157
162
|
polling = false;
|
|
158
163
|
}
|
|
159
164
|
}
|
|
160
|
-
async function
|
|
165
|
+
async function buildNewsContent(news) {
|
|
161
166
|
const link = getNewsLink(news.item);
|
|
162
167
|
const baseMarkdown = steamContentToMarkdown(news.item.content);
|
|
163
168
|
const translated = await maybeTranslate(news.item.title, baseMarkdown);
|
|
164
169
|
const displayTitle = translated.title || news.item.title;
|
|
165
170
|
const displayMarkdown = translated.markdown || baseMarkdown;
|
|
166
|
-
|
|
171
|
+
return config.picture
|
|
167
172
|
? await renderOrFallbackText(news, displayTitle, displayMarkdown, link)
|
|
168
173
|
: buildTextMessage(news, displayTitle, displayMarkdown, link);
|
|
174
|
+
}
|
|
175
|
+
async function pushNews(news) {
|
|
176
|
+
const content = await buildNewsContent(news);
|
|
169
177
|
if (!config.targets.length) {
|
|
170
178
|
logger.warn('未配置推送目标,已跳过:%s', news.item.title);
|
|
171
179
|
return;
|
|
@@ -301,15 +309,18 @@ function apply(ctx, config) {
|
|
|
301
309
|
return pushed ? `已推送 ${pushed} 条新的 CS2 官方新闻。` : '没有发现新的 CS2 官方新闻。';
|
|
302
310
|
});
|
|
303
311
|
ctx.command('cs2log.test', '测试推送最近 2 条 CS2 官方新闻')
|
|
304
|
-
.action(async () => {
|
|
312
|
+
.action(async ({ session }) => {
|
|
313
|
+
if (!session?.bot || !session.channelId)
|
|
314
|
+
return '测试指令需要在可发送消息的会话中使用。';
|
|
305
315
|
const items = await fetchNews();
|
|
306
316
|
const testItems = items.slice(0, 2).reverse();
|
|
307
317
|
if (!testItems.length)
|
|
308
318
|
return '没有拉取到可测试推送的 CS2 官方新闻。';
|
|
309
319
|
for (const item of testItems) {
|
|
310
|
-
await
|
|
320
|
+
const content = await buildNewsContent(classifyNews(item));
|
|
321
|
+
await session.bot.sendMessage(session.channelId, content);
|
|
311
322
|
}
|
|
312
|
-
return
|
|
323
|
+
return `已向当前会话触发 ${testItems.length} 条 CS2 官方新闻测试推送。本次测试不会写入 gid 判重 state。`;
|
|
313
324
|
});
|
|
314
325
|
ctx.on('ready', () => {
|
|
315
326
|
void loadState()
|
|
@@ -785,7 +796,77 @@ function isNodeError(error) {
|
|
|
785
796
|
return !!error && typeof error === 'object' && 'code' in error;
|
|
786
797
|
}
|
|
787
798
|
function formatError(error) {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
return
|
|
799
|
+
const lines = [];
|
|
800
|
+
appendErrorDetails(lines, error);
|
|
801
|
+
return lines.join('\n');
|
|
802
|
+
}
|
|
803
|
+
function appendErrorDetails(lines, error, label = 'error', depth = 0, seen = new Set()) {
|
|
804
|
+
if (error == null || typeof error !== 'object') {
|
|
805
|
+
lines.push(`${label}: ${String(error)}`);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
if (seen.has(error)) {
|
|
809
|
+
lines.push(`${label}: [Circular]`);
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
seen.add(error);
|
|
813
|
+
const record = error;
|
|
814
|
+
const name = typeof record.name === 'string' ? record.name : error instanceof Error ? error.name : 'Error';
|
|
815
|
+
const message = typeof record.message === 'string' ? record.message : String(error);
|
|
816
|
+
lines.push(`${label}: ${name}: ${message}`);
|
|
817
|
+
const details = collectErrorDetails(record);
|
|
818
|
+
if (details.length)
|
|
819
|
+
lines.push(`${label} details: ${details.join(', ')}`);
|
|
820
|
+
const stack = typeof record.stack === 'string' ? record.stack : '';
|
|
821
|
+
const stackLines = stack.split(/\r?\n/).slice(1, 7).map((line) => line.trim()).filter(Boolean);
|
|
822
|
+
if (stackLines.length)
|
|
823
|
+
lines.push(`${label} stack:\n ${stackLines.join('\n ')}`);
|
|
824
|
+
appendNestedObjectDetails(lines, record, 'request', label);
|
|
825
|
+
appendNestedObjectDetails(lines, record, 'response', label);
|
|
826
|
+
const cause = record.cause;
|
|
827
|
+
if (cause !== undefined && depth < 5) {
|
|
828
|
+
appendErrorDetails(lines, cause, `${label}.cause`, depth + 1, seen);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
function collectErrorDetails(record) {
|
|
832
|
+
const keys = [
|
|
833
|
+
'code',
|
|
834
|
+
'errno',
|
|
835
|
+
'type',
|
|
836
|
+
'syscall',
|
|
837
|
+
'hostname',
|
|
838
|
+
'host',
|
|
839
|
+
'address',
|
|
840
|
+
'port',
|
|
841
|
+
'method',
|
|
842
|
+
'url',
|
|
843
|
+
'status',
|
|
844
|
+
'statusCode',
|
|
845
|
+
'statusText',
|
|
846
|
+
];
|
|
847
|
+
const details = [];
|
|
848
|
+
for (const key of keys) {
|
|
849
|
+
const value = record[key];
|
|
850
|
+
if (value == null)
|
|
851
|
+
continue;
|
|
852
|
+
const rendered = renderLogValue(value);
|
|
853
|
+
if (rendered)
|
|
854
|
+
details.push(`${key}=${rendered}`);
|
|
855
|
+
}
|
|
856
|
+
return details;
|
|
857
|
+
}
|
|
858
|
+
function appendNestedObjectDetails(lines, record, key, label) {
|
|
859
|
+
const value = record[key];
|
|
860
|
+
if (!value || typeof value !== 'object')
|
|
861
|
+
return;
|
|
862
|
+
const details = collectErrorDetails(value);
|
|
863
|
+
if (details.length)
|
|
864
|
+
lines.push(`${label}.${key} details: ${details.join(', ')}`);
|
|
865
|
+
}
|
|
866
|
+
function renderLogValue(value) {
|
|
867
|
+
if (typeof value === 'string')
|
|
868
|
+
return value;
|
|
869
|
+
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint')
|
|
870
|
+
return String(value);
|
|
871
|
+
return undefined;
|
|
791
872
|
}
|
package/package.json
CHANGED