Commit bd1cc002 authored by nanahira's avatar nanahira

add getContext

parent a91be29e
import { Adapter, Bot } from 'koishi';
import { Adapter, Bot, Context, Filter, s } from 'koishi';
import _ from 'lodash';
import { RegisterSchema, SchemaProperty } from 'schemastery-gen';
......@@ -12,6 +12,18 @@ export class ChannelTarget {
toDesc(): string | [string, string] {
return this.guildId ? [this.channelId, this.guildId] : this.channelId;
}
toFilter(): Filter {
return (s) => {
if (this.channelId !== s.channelId) {
return false;
}
if (this.guildId && this.guildId !== s.guildId) {
return false;
}
return true;
};
}
}
@RegisterSchema()
......@@ -51,6 +63,25 @@ export class SendTarget {
);
}
getContext(ctx: Context) {
if (this.bot) {
const [platform, id] = this.bot.split(':');
ctx = ctx.platform(platform).self(id);
}
let filters: Filter[] = [];
if (this.users?.length) {
const userSet = new Set(this.users);
filters.push((s) => s.type === 'private' && userSet.has(s.userId));
}
if (this.channels?.length) {
filters = filters.concat(this.channels.map((c) => c.toFilter()));
}
if (filters.length) {
ctx = ctx.intersect((s) => filters.some((f) => f(s)));
}
return ctx;
}
async send(bots: Adapter.BotList, content: string) {
const bot = this.getBot(bots);
if (!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