码桶
发现社区成员的开源项目
index.js5.9 KB
/**
* 插件:实时金价(gold-price)
* --------------------------------------------------
* 查询实时黄金 / 白银 / 铂金 / 钯金价格,以及各大品牌金店、
* 银行投资金条、黄金回收价格。
*
* 命令:
* - 「金价」/「黄金价格」/「今日金价」 → 实时贵金属行情(元/克、美元/盎司)
* - 「金店」/「金价 店」 → 各大品牌金店金价 + 银行投资金条
* - 「回收」/「金价 回收」 → 各类黄金 / 铂钯银回收价
* - 「金价 <品牌>」 → 模糊查询某品牌/银行的金价(如:金价 周大福)
*
* 数据源:https://tmini.net/api/gold-price (返回 JSON,实时更新,缓存 5 分钟)
*/
const axios = require('axios');
const API_URL = 'https://tmini.net/api/gold-price';
const CACHE_TTL = 5 * 60 * 1000; // 5 分钟缓存(行情实时变动,不宜太长)
let cache = null; // { data, time }
async function fetchData() {
if (cache && Date.now() - cache.time < CACHE_TTL) return cache.data;
const { data } = await axios.get(API_URL, {
timeout: 12000,
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; NGBot/1.0)' },
});
if (!data || !Array.isArray(data.metals)) throw new Error('接口无数据');
cache = { data, time: Date.now() };
return data;
}
// 实时贵金属行情
function buildMetals(data) {
const lines = [];
lines.push('📊 实时金价 · ' + (data.date || ''));
const m0 = data.metals[0];
if (m0 && m0.updated) lines.push('🕒 更新:' + m0.updated);
lines.push('');
for (const m of data.metals) {
const u = m.unit || '';
lines.push(`【${m.name}】${m.sell_price} ${u}`);
lines.push(` 今开 ${m.today_price} | 高 ${m.high_price} | 低 ${m.low_price}`);
}
lines.push('');
lines.push('———');
lines.push('发送「金店」看品牌金价/银行金条,「回收」看回收价');
return lines.join('\n').trim();
}
// 品牌金店 + 银行金条
function buildStores(data) {
const lines = [];
lines.push('🏬 各大品牌金价(' + (data.date || '') + ')');
lines.push('');
for (const s of data.stores || []) {
lines.push(`【${s.brand}】${s.price} ${s.unit}`);
}
lines.push('');
lines.push('🏦 银行投资金条');
for (const b of data.banks || []) {
lines.push(`【${b.bank}·${b.product}】${b.price} ${b.unit}`);
}
return lines.join('\n').trim();
}
// 黄金回收价
function buildRecycle(data) {
const lines = [];
lines.push('♻️ 黄金 / 贵金属回收价(' + (data.date || '') + ')');
lines.push('');
for (const r of data.recycle || []) {
const purity = r.purity ? `(${r.purity})` : '';
lines.push(`【${r.type}】${r.price} ${r.unit} ${purity}`);
}
return lines.join('\n').trim();
}
// 模糊查询某品牌/银行的金价
function searchBrand(data, q) {
const hits = [];
for (const s of data.stores || []) {
if (s.brand.includes(q) || q.includes(s.brand)) hits.push(`【${s.brand}】${s.price} ${s.unit}`);
}
for (const b of data.banks || []) {
if (b.bank.includes(q) || q.includes(b.bank)) hits.push(`【${b.bank}·${b.product}】${b.price} ${b.unit}`);
}
return hits;
}
module.exports = {
meta: {
id: 'gold-price',
name: '实时金价',
version: '1.0.0',
author: '奶狗',
category: '信息获取',
description: '发送「金价」查询实时黄金/白银/铂钯行情;「金店」看品牌金价与银行金条;「回收」看回收价;「金价 品牌」模糊查询。',
entry: 'gold-price/index.js',
configurable: false,
commandPrefix: ['金价', '黄金', '金店', '回收', '银行金条'],
},
async onMessage(msg, ctx) {
const text = (msg.content || '').trim();
// 金店 / 品牌金价 + 银行金条
if (text === '金店' || text === '金价 店' || text === '金价店' || text === '品牌金价') {
try {
const data = await fetchData();
await ctx.sendText(buildStores(data));
} catch (e) {
console.error('[gold-price] 出错:', e.message);
await ctx.sendText('金价获取失败,请稍后再试。');
}
return true;
}
// 回收价
if (text === '回收' || text === '金价 回收' || text === '回收价' || text === '黄金回收') {
try {
const data = await fetchData();
await ctx.sendText(buildRecycle(data));
} catch (e) {
console.error('[gold-price] 出错:', e.message);
await ctx.sendText('金价获取失败,请稍后再试。');
}
return true;
}
// 模糊查询某品牌/银行
const brandMatch = text.match(/^金价\s+(.+)$/);
if (brandMatch) {
const q = brandMatch[1].trim();
try {
const data = await fetchData();
const hits = searchBrand(data, q);
if (!hits.length) {
await ctx.sendText('未找到品牌/银行「' + q + '」。可发「金店」查看全部品牌与银行金条。');
} else {
await ctx.sendText('🔍 查询「' + q + '」:\n' + hits.join('\n'));
}
} catch (e) {
console.error('[gold-price] 出错:', e.message);
await ctx.sendText('金价获取失败,请稍后再试。');
}
return true;
}
// 实时贵金属行情(主命令)
if (text === '金价' || text === '黄金价格' || text === '今日金价' || text === '实时金价' || text === '黄金') {
try {
const data = await fetchData();
await ctx.sendText(buildMetals(data));
} catch (e) {
console.error('[gold-price] 出错:', e.message);
await ctx.sendText('金价获取失败,请稍后再试。');
}
return true;
}
return false;
},
// 导出内部函数(便于单测/调试;插件系统只读 meta/onMessage)
fetchData,
};