koishi-plugin-crypot-bot
Version:
BTC
99 lines (97 loc) • 4.19 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var import_axios = __toESM(require("axios"));
var import_https_proxy_agent = require("https-proxy-agent");
var API_URL = "https://www.okx.com/api/v5/market/ticker?instId=${coin}-USDT";
var name = "crypot";
var Config = import_koishi.Schema.object({
alertThreshold: import_koishi.Schema.number().default(1).description("价格变动的阈值"),
proxy: import_koishi.Schema.string().description("代理服务器地址,例如 http://192.168.1.100:8080")
});
function apply(ctx, config) {
const watchList = {};
const buyPrices = {};
ctx.command("crypto <currency>", "获取指定货币的当前价格").action(async ({ session }, currency) => {
const price = await getCurrentPrice(currency, config.proxy);
return price ? `当前价格: ${price} USDT` : "获取价格失败";
});
ctx.command("crypto.watch <currency>", "添加货币到监控列表").action(({ session }, currency) => {
watchList[currency] = 0;
return `已添加 ${currency} 到监控列表`;
});
ctx.command("crypto.unwatch <currency>", "从监控列表中移除货币").action(({ session }, currency) => {
delete watchList[currency];
return `已移除 ${currency} 从监控列表`;
});
ctx.command("crypto.buy <currency> <price>", "记录购买合约的价格").action(({ session }, currency, price) => {
buyPrices[currency] = parseFloat(price);
return `已记录 ${currency} 的购买价格为 ${price} USDT`;
});
setInterval(async () => {
for (const currency in watchList) {
const currentPrice = await getCurrentPrice(currency, config.proxy);
if (currentPrice) {
const buyPrice = buyPrices[currency];
if (buyPrice) {
const change = (currentPrice - buyPrice) / buyPrice * 100;
if (Math.abs(change) >= config.alertThreshold) {
ctx.broadcast(`警告: ${currency} 当前价格变动 ${change.toFixed(2)}%`);
}
}
}
}
}, 6e4);
}
__name(apply, "apply");
async function getCurrentPrice(currency, proxy) {
try {
const agent = new import_https_proxy_agent.HttpsProxyAgent(proxy);
const response = await import_axios.default.get(API_URL.replace("${coin}", currency), { httpsAgent: agent });
return response.data.data[0].last;
} catch (error) {
console.error("获取价格失败:", error);
return null;
}
}
__name(getCurrentPrice, "getCurrentPrice");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
name
});