mirror of
https://github.com/syuilo/ai.git
synced 2024-11-22 05:08:00 +00:00
Refactor
This commit is contained in:
parent
eeeda6c359
commit
87b6ba55c5
20
src/ai.ts
20
src/ai.ts
|
@ -12,16 +12,16 @@ import { User } from './misskey/user';
|
|||
import getCollection from './utils/get-collection';
|
||||
import Stream from './stream';
|
||||
|
||||
type OnMentionHandler = (msg: MessageLike) => boolean | HandlerResult;
|
||||
type OnContextReplyHandler = (msg: MessageLike, data?: any) => void | HandlerResult;
|
||||
type MentionHook = (msg: MessageLike) => boolean | HandlerResult;
|
||||
type ContextHook = (msg: MessageLike, data?: any) => void | HandlerResult;
|
||||
|
||||
export type HandlerResult = {
|
||||
reaction: string;
|
||||
};
|
||||
|
||||
export type InstallerResult = {
|
||||
onMention?: OnMentionHandler;
|
||||
onContextReply?: OnContextReplyHandler;
|
||||
mentionHook?: MentionHook;
|
||||
contextHook?: ContextHook;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -31,8 +31,8 @@ export default class 藍 {
|
|||
public account: User;
|
||||
public connection: Stream;
|
||||
public modules: Module[] = [];
|
||||
private onMentionHandlers: OnMentionHandler[] = [];
|
||||
private onContextReplyHandlers: { [moduleName: string]: OnContextReplyHandler } = {};
|
||||
private mentionHooks: MentionHook[] = [];
|
||||
private contextHooks: { [moduleName: string]: ContextHook } = {};
|
||||
public db: loki;
|
||||
|
||||
private contexts: loki.Collection<{
|
||||
|
@ -112,8 +112,8 @@ export default class 藍 {
|
|||
this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`);
|
||||
const res = m.install();
|
||||
if (res != null) {
|
||||
if (res.onMention) this.onMentionHandlers.push(res.onMention);
|
||||
if (res.onContextReply) this.onContextReplyHandlers[m.name] = res.onContextReply;
|
||||
if (res.mentionHook) this.mentionHooks.push(res.mentionHook);
|
||||
if (res.contextHook) this.contextHooks[m.name] = res.contextHook;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -135,7 +135,7 @@ export default class 藍 {
|
|||
let reaction = 'love';
|
||||
|
||||
if (context != null) {
|
||||
const handler = this.onContextReplyHandlers[context.module];
|
||||
const handler = this.contextHooks[context.module];
|
||||
const res = handler(msg, context.data);
|
||||
|
||||
if (res != null && typeof res === 'object') {
|
||||
|
@ -144,7 +144,7 @@ export default class 藍 {
|
|||
} else {
|
||||
let res: boolean | HandlerResult;
|
||||
|
||||
this.onMentionHandlers.some(handler => {
|
||||
this.mentionHooks.some(handler => {
|
||||
res = handler(msg);
|
||||
return res === true || typeof res === 'object';
|
||||
});
|
||||
|
|
|
@ -15,13 +15,13 @@ export default class CoreModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention,
|
||||
onContextReply: this.onContextReply
|
||||
mentionHook: this.mentionHook,
|
||||
contextHook: this.contextHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (!msg.text) return false;
|
||||
|
||||
return (
|
||||
|
@ -313,7 +313,7 @@ export default class CoreModule extends Module {
|
|||
}
|
||||
|
||||
@autobind
|
||||
private onContextReply(msg: MessageLike, data: any) {
|
||||
private contextHook(msg: MessageLike, data: any) {
|
||||
if (msg.text == null) return;
|
||||
|
||||
const done = () => {
|
||||
|
|
|
@ -9,12 +9,12 @@ export default class DiceModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.text == null) return false;
|
||||
|
||||
const query = msg.text.match(/([0-9]+)[dD]([0-9]+)/);
|
||||
|
|
|
@ -132,12 +132,12 @@ export default class EmojiModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.includes(['顔文字', '絵文字', 'emoji', '福笑い'])) {
|
||||
const hand = hands[Math.floor(Math.random() * hands.length)];
|
||||
const face = faces[Math.floor(Math.random() * faces.length)];
|
||||
|
|
|
@ -8,12 +8,12 @@ export default class FollowModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.text && msg.includes(['フォロー', 'フォロバ', 'follow me'])) {
|
||||
if (!msg.user.isFollowing) {
|
||||
this.ai.api('following/create', {
|
||||
|
|
|
@ -11,12 +11,12 @@ export default class FortuneModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) {
|
||||
const date = new Date();
|
||||
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDate()}@${msg.userId}`;
|
||||
|
|
|
@ -25,13 +25,13 @@ export default class GuessingGameModule extends Module {
|
|||
//#endregion
|
||||
|
||||
return {
|
||||
onMention: this.onMention,
|
||||
onContextReply: this.onContextReply
|
||||
mentionHook: this.mentionHook,
|
||||
contextHook: this.contextHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.includes(['数当て', '数あて'])) {
|
||||
const exist = this.guesses.findOne({
|
||||
userId: msg.userId,
|
||||
|
@ -70,7 +70,7 @@ export default class GuessingGameModule extends Module {
|
|||
}
|
||||
|
||||
@autobind
|
||||
private onContextReply(msg: MessageLike) {
|
||||
private contextHook(msg: MessageLike) {
|
||||
if (msg.text == null) return;
|
||||
|
||||
const exist = this.guesses.findOne({
|
||||
|
|
|
@ -8,12 +8,12 @@ export default class PingModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.text && msg.text.includes('ping')) {
|
||||
msg.reply('PONG!');
|
||||
return true;
|
||||
|
|
|
@ -28,12 +28,12 @@ export default class ReversiModule extends Module {
|
|||
this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg));
|
||||
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) {
|
||||
if (config.reversiEnabled) {
|
||||
msg.reply(serifs.reversi.ok);
|
||||
|
|
|
@ -9,12 +9,12 @@ export default class TimerModule extends Module {
|
|||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
onMention: this.onMention
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private onMention(msg: MessageLike) {
|
||||
private mentionHook(msg: MessageLike) {
|
||||
const secondsQuery = (msg.text || '').match(/([0-9]+)秒/);
|
||||
const minutesQuery = (msg.text || '').match(/([0-9]+)分/);
|
||||
const hoursQuery = (msg.text || '').match(/([0-9]+)時間/);
|
||||
|
|
Loading…
Reference in a new issue