Commit a43a82ff authored by nanahira's avatar nanahira

Merge branch 'next'

parents 420d8bd3 42c856d0
Pipeline #6079 passed with stages
in 2 minutes and 12 seconds
...@@ -44,7 +44,7 @@ export interface Config { ...@@ -44,7 +44,7 @@ export interface Config {
### 说明 ### 说明
* `adminContext` 一个上下文函数,设置管理员接口作用域。如 `(ctx) => ctx.private('1111111111')` 为只对 QQ 号是 1111111111 的私聊消息有效 * `adminContext` 管理员接口作用域
* `autoChangeName` 公演开始之前是否修改每个演员机器人的群名片。推荐开启。 * `autoChangeName` 公演开始之前是否修改每个演员机器人的群名片。推荐开启。
...@@ -60,4 +60,4 @@ export interface Config { ...@@ -60,4 +60,4 @@ export interface Config {
* `groupId` 目标群。`playbookFilename` 是公演剧本文件,需要在服务器放好。后面的参数可以以 人物名=帐号 的形式指定特定的人物,否则随机分配。 * `groupId` 目标群。`playbookFilename` 是公演剧本文件,需要在服务器放好。后面的参数可以以 人物名=帐号 的形式指定特定的人物,否则随机分配。
* 例: `act.create 11111111 play1 幽幽子=2222222` 在群 11111111 创建一个公演,剧本是 play1,位于 `/path/to/playbookPathPrefix/play1.json`,同时指定人物幽幽子为 2222222 扮演。 * 例: `act.create 11111111 play1 幽幽子=2222222` 在群 11111111 创建一个公演,剧本是 play1,位于 `/path/to/playbookPathPrefix/play1.json`,同时指定人物幽幽子为 2222222 扮演。
* `.delete <groupId:number>` 停止公演。 * `.delete <groupId:number>` 停止公演。
\ No newline at end of file
This diff is collapsed.
{ {
"name": "koishi-plugin-act", "name": "koishi-plugin-act",
"version": "1.0.3", "version": "2.0.0",
"description": "A plugin playing acts in QQ groups", "description": "A plugin playing acts in QQ groups",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts",
"dependencies": { "dependencies": {
"source-map-support": "^0.5.19" "source-map-support": "^0.5.19"
}, },
"peerDependencies": { "peerDependencies": {
"koishi-adapter-onebot": "^3.1.0", "koishi": "^4.0.0-alpha.8",
"koishi-core": "^3.13.0" "@koishijs/plugin-onebot": "^4.0.0-alpha.7"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-onebot": "^4.0.0-alpha.7",
"@types/lodash": "^4.14.172", "@types/lodash": "^4.14.172",
"@types/mustache": "^4.1.2", "@types/mustache": "^4.1.2",
"@types/node": "^16.4.11", "@types/node": "^16.4.11",
...@@ -21,8 +23,7 @@ ...@@ -21,8 +23,7 @@
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0", "eslint-plugin-prettier": "^3.4.0",
"koishi-adapter-onebot": "^3.1.0", "koishi": "^4.0.0-alpha.8",
"koishi-core": "^3.13.0",
"load-json-file": "^6.2.0", "load-json-file": "^6.2.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.29.1", "moment": "^2.29.1",
......
import 'source-map-support/register'; import 'source-map-support/register';
import type { Context } from 'koishi-core'; import type { Context } from 'koishi';
import { Config, MyPlugin } from './plugin'; import { Config, MyPlugin } from './plugin';
export { Config } from './plugin'; export { Config } from './plugin';
export const name = 'act-index'; export const name = 'act';
const plugin = new MyPlugin();
export const schema = plugin.schema;
export function apply(ctx: Context, config: Config) { export function apply(ctx: Context, config: Config) {
ctx.plugin(new MyPlugin(), config); ctx.plugin(plugin, config);
} }
import { Playbook } from './Playbook'; import { Playbook } from './Playbook';
import type { CQBot } from 'koishi-adapter-onebot'; import type { OneBotBot } from '@koishijs/plugin-onebot/lib/bot';
import moment, { Moment } from 'moment'; import moment, { Moment } from 'moment';
export enum ShowStatus { export enum ShowStatus {
...@@ -10,7 +10,7 @@ export enum ShowStatus { ...@@ -10,7 +10,7 @@ export enum ShowStatus {
export class Show { export class Show {
status: ShowStatus = ShowStatus.Idle; status: ShowStatus = ShowStatus.Idle;
private characterBotMap = new Map<number, CQBot>(); private characterBotMap = new Map<number, OneBotBot>();
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
onFinish: (message: string, show: Show) => void = () => {}; onFinish: (message: string, show: Show) => void = () => {};
private step = 0; private step = 0;
...@@ -23,14 +23,14 @@ export class Show { ...@@ -23,14 +23,14 @@ export class Show {
for (const character of playbook.characters) { for (const character of playbook.characters) {
character.playFun = async (line, text) => { character.playFun = async (line, text) => {
const bot = this.characterBotMap.get(character.id); const bot = this.characterBotMap.get(character.id);
await bot.$sendGroupMsg(this.groupId, text, true); await bot.internal.sendGroupMsg(this.groupId, text, true);
}; };
} }
} }
private getPerformingBots() { private getPerformingBots() {
return Array.from(this.characterBotMap.values()); return Array.from(this.characterBotMap.values());
} }
useCharacter(id: number, bot: CQBot) { useCharacter(id: number, bot: OneBotBot) {
this.characterBotMap.set(id, bot); this.characterBotMap.set(id, bot);
} }
isCharacterFull() { isCharacterFull() {
...@@ -38,20 +38,21 @@ export class Show { ...@@ -38,20 +38,21 @@ export class Show {
this.characterBotMap.has(c.id), this.characterBotMap.has(c.id),
); );
} }
async autoCharacters(bots: CQBot[]) { async autoCharacters(bots: OneBotBot[]) {
let availableBots: CQBot[] = []; let availableBots: OneBotBot[] = [];
const botNameMap = new Map<string, string>(); const botNameMap = new Map<string, string>();
for (const bot of bots) { for (const bot of bots) {
if (!(bot.type && bot.type.startsWith('onebot'))) { if (bot.adapter.platform !== 'onebot') {
continue; continue;
} }
const groups = await bot.$getGroupList(); const groups = await bot.internal.getGroupList();
const matchGroup = groups.find((g) => g.groupId === this.groupId); const matchGroup = groups.find((g) => g.group_id === this.groupId);
if (matchGroup) { if (matchGroup) {
availableBots.push(bot); availableBots.push(bot);
botNameMap.set( botNameMap.set(
bot.selfId.toString(), bot.selfId.toString(),
(await bot.$getGroupMemberInfo(this.groupId, bot.selfId)).nickname, (await bot.internal.getGroupMemberInfo(this.groupId, bot.selfId))
.nickname,
); );
} }
} }
...@@ -109,7 +110,11 @@ export class Show { ...@@ -109,7 +110,11 @@ export class Show {
for (const character of this.playbook.characters) { for (const character of this.playbook.characters) {
const bot = this.characterBotMap.get(character.id); const bot = this.characterBotMap.get(character.id);
try { try {
await bot.$setGroupCard(this.groupId, bot.selfId, character.name); await bot.internal.setGroupCard(
this.groupId,
bot.selfId,
character.name,
);
} catch (e) { } catch (e) {
return `Change name for ${bot.selfId} ${ return `Change name for ${bot.selfId} ${
character.name character.name
......
import 'source-map-support/register'; import 'source-map-support/register';
import type { Context, Session } from 'koishi-core'; import { Context, Schema, Session } from 'koishi';
import { Show } from './playbook/Show'; import { Show } from './playbook/Show';
import { Playbook } from './playbook/Playbook'; import { Playbook } from './playbook/Playbook';
import { plainToClass } from 'class-transformer'; import { plainToClass } from 'class-transformer';
import loadJsonFile from 'load-json-file'; import loadJsonFile from 'load-json-file';
import type { CQBot } from 'koishi-adapter-onebot'; import { MaybeArray } from 'koishi';
import type { OneBotBot } from '@koishijs/plugin-onebot/lib/bot';
const selectors = [
'user',
'guild',
'channel',
'self',
'private',
'platform',
] as const;
type SelectorType = typeof selectors[number];
type SelectorValue = boolean | MaybeArray<string | number>;
type BaseSelection = { [K in SelectorType as `$${K}`]?: SelectorValue };
export interface Selection extends BaseSelection {
$and?: Selection[];
$or?: Selection[];
$not?: Selection;
}
export interface Config { export interface Config {
adminContext: (ctx: Context) => Context; adminContext: Selection;
autoChangeName: boolean; autoChangeName: boolean;
dropHelp: boolean; dropHelp: boolean;
playbookPathPrefix: string; playbookPathPrefix: string;
...@@ -19,21 +39,26 @@ export class MyPlugin { ...@@ -19,21 +39,26 @@ export class MyPlugin {
adminCtx: Context; adminCtx: Context;
shows = new Map<number, Show>(); shows = new Map<number, Show>();
name = 'act'; name = 'act-main';
schema: Schema<Config> = Schema.object({
adminContext: Schema.any('管理员接口作用域。'),
autoChangeName: Schema.boolean(
'公演开始之前是否修改每个演员机器人的群名片。推荐开启。',
).default(true),
playbookPathPrefix: Schema.string(
'公演剧本存放路径。公演剧本的文件名是 `/path/to/playbookPathPrefix/name.json` 对应于 name 剧本。',
).default('./playbooks'),
dropHelp: Schema.boolean('是否删除 `help` 命令,避免社死 (?)').default(
false,
),
});
apply(ctx: Context, config: Config) { apply(ctx: Context, config: Config) {
this.ctx = ctx; this.ctx = ctx;
this.config = { this.config = Schema.validate(config, this.schema);
adminContext: (ctx) => ctx.private(),
autoChangeName: false,
dropHelp: false,
playbookPathPrefix: './playbooks',
...config,
};
if (this.config.dropHelp) { if (this.config.dropHelp) {
ctx.command('help').dispose(); ctx.command('help').dispose();
} }
this.adminCtx = this.config.adminContext(ctx); this.adminCtx = this.ctx.select(this.config.adminContext);
const showComamnd = this.adminCtx const showComamnd = this.adminCtx
.command('act [groupId:number]', '获取公演状态') .command('act [groupId:number]', '获取公演状态')
.usage('不带参数获取所有正在公演的群,带参数则获取特定群。') .usage('不带参数获取所有正在公演的群,带参数则获取特定群。')
...@@ -110,9 +135,9 @@ export class MyPlugin { ...@@ -110,9 +135,9 @@ export class MyPlugin {
} catch (e) { } catch (e) {
return `无法加载剧本文件 ${playbookPath}: ${e.toString()}`; return `无法加载剧本文件 ${playbookPath}: ${e.toString()}`;
} }
const bots: CQBot[] = (this.ctx.bots.filter( const bots: OneBotBot[] = this.ctx.bots.filter(
(b) => b.type && b.type.startsWith('onebot'), (b) => b.adapter.platform === 'onebot',
) as unknown) as CQBot[]; ) as OneBotBot[];
const show = new Show(groupId, playbook, this.config.autoChangeName); const show = new Show(groupId, playbook, this.config.autoChangeName);
for (const specificCharacter of specificCharacters) { for (const specificCharacter of specificCharacters) {
const [characterName, botId] = specificCharacter.split('='); const [characterName, botId] = specificCharacter.split('=');
......
...@@ -25,4 +25,8 @@ module.exports = { ...@@ -25,4 +25,8 @@ module.exports = {
}, },
path: path.resolve(__dirname, "dist"), path: path.resolve(__dirname, "dist"),
}, },
externals: {
'koishi': 'koishi',
'@koishijs/plugin-onebot/lib/bot': '@koishijs/plugin-onebot/lib/bot',
}
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment