koishi-plugin-chatsound 0.0.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/lib/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "chatsound";
3
+ export interface Config {
4
+ soundPath: string[];
5
+ defaultPitch: number;
6
+ minPitch: number;
7
+ maxPitch: number;
8
+ }
9
+ export declare const Config: Schema<Config>;
10
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,94 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ name: () => name
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+ var import_koishi = require("koishi");
29
+ var import_promises = require("fs/promises");
30
+ var import_path = require("path");
31
+ var import_child_process = require("child_process");
32
+ var import_util = require("util");
33
+ var execAsync = (0, import_util.promisify)(import_child_process.exec);
34
+ var name = "chatsound";
35
+ var Config = import_koishi.Schema.object({
36
+ soundPath: import_koishi.Schema.array(import_koishi.Schema.string()).description("用于搜索的音频路径"),
37
+ defaultPitch: import_koishi.Schema.number().default(100).description("默认的语音音调(百分比)"),
38
+ minPitch: import_koishi.Schema.number().default(80).min(0).description("最小Pitch,不小于0"),
39
+ maxPitch: import_koishi.Schema.number().default(200).description("最大Pitch")
40
+ });
41
+ var AUDIO_EXTENSIONS = [".mp3", ".wav", ".ogg", ".flac", ".m4a", ".aac"];
42
+ async function findAudioFile(trigger, soundPaths) {
43
+ for (const basePath of soundPaths) {
44
+ try {
45
+ const files = await (0, import_promises.readdir)(basePath);
46
+ for (const file of files) {
47
+ const ext = (0, import_path.extname)(file).toLowerCase();
48
+ if (AUDIO_EXTENSIONS.includes(ext)) {
49
+ const nameWithoutExt = file.slice(0, -ext.length);
50
+ if (nameWithoutExt === trigger) {
51
+ return (0, import_path.join)(basePath, file);
52
+ }
53
+ }
54
+ }
55
+ } catch (error) {
56
+ continue;
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+ __name(findAudioFile, "findAudioFile");
62
+ async function applyPitch(inputPath, pitch) {
63
+ const pitchFactor = pitch / 100;
64
+ const command = `ffmpeg -i "${inputPath}" -filter:a "asetrate=44100*${pitchFactor},aresample=44100" -f mp3 -`;
65
+ const { stdout } = await execAsync(command);
66
+ return Buffer.from(stdout);
67
+ }
68
+ __name(applyPitch, "applyPitch");
69
+ function apply(ctx, config) {
70
+ ctx.command("v <trigger:string> [pitch:number]").action(async ({ session }, trigger, pitch) => {
71
+ if (!trigger) {
72
+ return "至少给个名字吧大王";
73
+ }
74
+ const actualPitch = Math.min(Math.max(pitch ?? config.defaultPitch, config.minPitch), config.maxPitch);
75
+ const audioPath = await findAudioFile(trigger, config.soundPath);
76
+ if (!audioPath) {
77
+ return `没有这种音频`;
78
+ }
79
+ try {
80
+ const audioBuffer = await applyPitch(audioPath, actualPitch);
81
+ await session.send(import_koishi.h.audio(audioBuffer, "audio/mp3"));
82
+ } catch (error) {
83
+ ctx.logger.error("发送音频失败:", error);
84
+ return "发送音频失败";
85
+ }
86
+ });
87
+ }
88
+ __name(apply, "apply");
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ Config,
92
+ apply,
93
+ name
94
+ });
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "koishi-plugin-chatsound",
3
+ "description": "发送预定的噪音,可以变调,依赖ffmpeg",
4
+ "contributors": [
5
+ "Dr.Abc <me@drabc.net>"
6
+ ],
7
+ "version": "0.0.2",
8
+ "homepage": "https://github.com/DrAbcOfficial/koishi-plugin-chatsound",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/DrAbcOfficial/koishi-plugin-chatsound.git"
12
+ },
13
+ "main": "lib/index.js",
14
+ "typings": "lib/index.d.ts",
15
+ "files": [
16
+ "lib"
17
+ ],
18
+ "license": "MIT",
19
+ "scripts": {},
20
+ "keywords": [
21
+ "chatbot",
22
+ "koishi",
23
+ "plugin"
24
+ ],
25
+ "devDependencies": {},
26
+ "peerDependencies": {
27
+ "koishi": "^4.18.7"
28
+ },
29
+ "koishi": {
30
+ "description": {
31
+ "en": "发送预定的噪音,可以变调,依赖ffmpeg",
32
+ "zh": "发送预定的噪音,可以变调,依赖ffmpeg"
33
+ },
34
+ "preview": true
35
+ }
36
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-chatsound
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-chatsound?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-chatsound)
4
+
5
+ 发送预定的噪音