koishi-plugin-kbot
Version:
A muti-function qq bot for koishi
133 lines (132 loc) • 6.07 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.apply = exports.logger = exports.Config = void 0;
const jsx_runtime_1 = require("@satorijs/element/jsx-runtime");
const koishi_1 = require("koishi");
const statusPlugin = __importStar(require("./status"));
const ttsPlugin = __importStar(require("./tts"));
exports.Config = koishi_1.Schema.object({
yiyan: koishi_1.Schema.boolean().default(false).description('是否开启一言'),
renjian: koishi_1.Schema.boolean().default(false).description('是否开启人间一言'),
alApiToken: koishi_1.Schema.string().description('ALAPI Token, 注: 前往 https://www.alapi.cn/ 个人中心获取token (可选)'),
news: koishi_1.Schema.boolean().default(false).description('是否开启今日新闻 (需要alApiToken)'),
weather: koishi_1.Schema.boolean().default(false).description('是否开启天气查询 (需要alApiToken)'),
checkBody: koishi_1.Schema.intersect([
koishi_1.Schema.object({
enabled: koishi_1.Schema.boolean().default(false).description('是否开启自带的 status, 等同于插件 `status-pro` (需要 puppeteer)'),
}),
koishi_1.Schema.union([
koishi_1.Schema.object({
enabled: koishi_1.Schema.const(true).required(),
...statusPlugin.Config.dict,
}),
koishi_1.Schema.object({
enabled: koishi_1.Schema.const(false),
}),
]),
]),
tts: koishi_1.Schema.boolean().default(false).description('是否开启文字转语音, API 来源于 https://www.text-to-speech.cn/'),
});
exports.logger = new koishi_1.Logger('KBot-basic');
async function apply(ctx, config) {
if (config.yiyan) {
ctx.command('kbot/一言', '随机一言', {
checkArgCount: true,
showWarning: false,
}).action(async () => {
const yiyan = await ctx.http
.get('https://api.pmay.cn/api/yiyan')
.catch((err) => {
exports.logger.error(`获取一言时出错: ${err}`);
return '获取一言失败';
});
if (!yiyan)
return '获取一言失败';
return yiyan;
});
}
if (config.renjian) {
ctx.command('kbot/人间', '随机一言散文集《我在人间凑数的日子》', {
checkArgCount: true,
showWarning: false,
}).action(async () => {
const data = await ctx.http
.get('https://api.pmay.cn/api/renjian')
.catch((err) => {
exports.logger.error(`获取人间一言时出错: ${err}`);
});
if (data.code !== 1)
return '获取人间一言失败';
return data.data.yiyan;
});
}
if (config.alApiToken) {
if (config.news) {
ctx.command('kbot/今日新闻', '获取60秒看世界新闻', {
checkArgCount: true,
showWarning: false,
}).action(async ({ session }) => {
await ctx.http
.get(`https://v2.alapi.cn/api/zaobao?token=${config.alApiToken}&format=json`)
.then((res) => {
const { data } = res;
session.send((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)("image", { url: data.image }) }));
}).catch((err) => {
exports.logger.error(`获取今日新闻时出错: ${err}`);
return '获取今日新闻失败';
});
});
}
if (config.weather) {
ctx
.command('kbot/天气 <city:string>', '查询城市天气')
.shortcut(/^查询(.+)天气$/, { args: ['$1'] })
.action(async (_, city) => {
const weather = await ctx.http
.get(`https://v2.alapi.cn/api/tianqi?token=${config.alApiToken}&city=${city}`)
.catch((err) => {
exports.logger.error(`获取天气信息时出错: ${err}`);
});
if (!weather)
return '获取天气信息失败';
const weatherData = weather.data;
return `
城市: ${weatherData.city}
气温: ${weatherData.min_temp}℃/${weatherData.max_temp}℃ ${weatherData.weather}
日出时间: ${weatherData.sunrise}
日落时间: ${weatherData.sunset}
今日天气实况: 气温: ${weatherData.temp}℃; 风力/风向: ${weatherData.wind} ${weatherData.wind_speed}; 湿度: ${weatherData.humidity}; 空气等级: ${weatherData.aqi.air_level};
${Object.values(weatherData.index).map((item) => `${item.name}: ${item.level}, ${item.content}`).join('\n')}
`;
});
}
}
if (config.checkBody.enabled)
ctx.plugin(statusPlugin, config.checkBody);
if (config.tts)
ctx.plugin(ttsPlugin);
}
exports.apply = apply;