Commit 457f2fb4 authored by nanahira's avatar nanahira

add auto manage guild

parent ff5b8068
...@@ -10,6 +10,12 @@ export class WyWallBacklistPluginConfig { ...@@ -10,6 +10,12 @@ export class WyWallBacklistPluginConfig {
default: 'https://blapi.wenaiwu.net', default: 'https://blapi.wenaiwu.net',
}) })
endpoint: string; endpoint: string;
@SchemaProperty({
description: '自动管理群聊。',
default: false,
})
autoManage: boolean;
} }
export type WyWallBacklistPluginConfigLike = Partial<WyWallBacklistPluginConfig>; export type WyWallBacklistPluginConfigLike = Partial<WyWallBacklistPluginConfig>;
...@@ -5,17 +5,84 @@ import { ...@@ -5,17 +5,84 @@ import {
CommandLocale, CommandLocale,
CRenderer, CRenderer,
DefinePlugin, DefinePlugin,
If,
Inject, Inject,
InjectLogger, InjectLogger,
Isolate,
OnGuild,
OnPlatform,
PluginDef,
Provide,
PutArg, PutArg,
PutCommonRenderer, PutCommonRenderer,
StarterPlugin, StarterPlugin,
UseCommand, UseCommand,
UseEvent,
UseMiddleware,
UsePlugin,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Logger, Quester } from 'koishi'; import { Logger, Next, Quester, Session } from 'koishi';
import { BlacklistReturn } from './types'; import { BlacklistReturn } from './types';
export * from './config'; export * from './config';
@OnPlatform('onebot')
@OnGuild()
@DefinePlugin()
class ManageHandler extends StarterPlugin() {
@Inject()
private wybl: WyWallBacklistPlugin;
async handleAccount(session: Session, skipAdminCheck = false) {
if (!skipAdminCheck) {
const selfInfo = await session.bot.getGuildMember(
session.guildId,
session.selfId,
);
if (
!selfInfo ||
(!selfInfo.roles.includes('admin') && !selfInfo.roles.includes('owner'))
) {
return [];
}
}
return await this.wybl.checkAccount(session.userId);
}
@UseMiddleware(true)
async onGuildMessage(session: Session, next: Next) {
const result = await this.handleAccount(session);
if (result.length) {
await session.bot.deleteMessage(session.channelId, session.messageId);
await session.bot.kickGuildMember(session.guildId, session.userId);
return [
`用户 ${session.username}(${session.userId}) 位于蔷蔷黑名单,已被踢出群聊。`,
'',
...result,
].join('\n');
}
return next();
}
@UseEvent('guild-member-request')
async onGuildMemberRequest(session: Session) {
const result = await this.handleAccount(session, true);
if (result.length) {
await session.bot.handleGuildMemberRequest(
session.messageId,
false,
result.join(', '),
);
await session.bot.sendMessage(session.guildId, [
`用户 ${session.userId} 位于蔷蔷黑名单,已禁止入群。`,
'',
...result,
]);
}
}
}
@Isolate('wybl')
@Provide('wybl', { immediate: true })
@DefinePlugin({ name: 'wyblacklist' }) @DefinePlugin({ name: 'wyblacklist' })
export default class WyWallBacklistPlugin extends StarterPlugin( export default class WyWallBacklistPlugin extends StarterPlugin(
WyWallBacklistPluginConfig, WyWallBacklistPluginConfig,
...@@ -26,6 +93,32 @@ export default class WyWallBacklistPlugin extends StarterPlugin( ...@@ -26,6 +93,32 @@ export default class WyWallBacklistPlugin extends StarterPlugin(
@InjectLogger() @InjectLogger()
private logger: Logger; private logger: Logger;
@If<WyWallBacklistPlugin>((p) => p.config.autoManage)
@UsePlugin()
loadManage() {
this.logger.info(`Auto manage is enabled.`);
return PluginDef(ManageHandler);
}
async checkAccount(account: string | number) {
const result = await this.http.get<BlacklistReturn>(
`${this.config.endpoint}/api/blacklist`,
{
responseType: 'json',
params: {
recordsPerPage: 100,
account,
},
},
);
return result.data.map(
(item) =>
`${new Date(item.blacklist.time).toLocaleString()} ${
item.blacklist.content
}`,
);
}
@UseCommand('wybl <account:posint>') @UseCommand('wybl <account:posint>')
@CommandExample('wybl 123456789') @CommandExample('wybl 123456789')
@CommandLocale('zh', { @CommandLocale('zh', {
...@@ -52,29 +145,11 @@ export default class WyWallBacklistPlugin extends StarterPlugin( ...@@ -52,29 +145,11 @@ export default class WyWallBacklistPlugin extends StarterPlugin(
} }
try { try {
const result = await this.http.get<BlacklistReturn>( const result = await this.checkAccount(account);
`${this.config.endpoint}/api/blacklist`, if (!result.length) {
{
responseType: 'json',
params: {
recordsPerPage: 100,
account,
},
},
);
if (!result.data.length) {
return r('.safe', { account }); return r('.safe', { account });
} }
return [ return [r('.unsafe', { account }), '', ...result].join('\n');
r('.unsafe', { account }),
'',
...result.data.map(
(item) =>
`${new Date(item.blacklist.time).toLocaleString()} ${
item.blacklist.content
}`,
),
].join('\n');
} catch (e) { } catch (e) {
this.logger.error(`Failed to fetch blacklist of ${account}: ${e}`); this.logger.error(`Failed to fetch blacklist of ${account}: ${e}`);
return r('.fail'); return r('.fail');
......
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