This commit is contained in:
syuilo 2019-01-15 12:01:58 +09:00
parent eeeda6c359
commit 87b6ba55c5
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
10 changed files with 32 additions and 32 deletions

View file

@ -12,16 +12,16 @@ import { User } from './misskey/user';
import getCollection from './utils/get-collection'; import getCollection from './utils/get-collection';
import Stream from './stream'; import Stream from './stream';
type OnMentionHandler = (msg: MessageLike) => boolean | HandlerResult; type MentionHook = (msg: MessageLike) => boolean | HandlerResult;
type OnContextReplyHandler = (msg: MessageLike, data?: any) => void | HandlerResult; type ContextHook = (msg: MessageLike, data?: any) => void | HandlerResult;
export type HandlerResult = { export type HandlerResult = {
reaction: string; reaction: string;
}; };
export type InstallerResult = { export type InstallerResult = {
onMention?: OnMentionHandler; mentionHook?: MentionHook;
onContextReply?: OnContextReplyHandler; contextHook?: ContextHook;
}; };
/** /**
@ -31,8 +31,8 @@ export default class 藍 {
public account: User; public account: User;
public connection: Stream; public connection: Stream;
public modules: Module[] = []; public modules: Module[] = [];
private onMentionHandlers: OnMentionHandler[] = []; private mentionHooks: MentionHook[] = [];
private onContextReplyHandlers: { [moduleName: string]: OnContextReplyHandler } = {}; private contextHooks: { [moduleName: string]: ContextHook } = {};
public db: loki; public db: loki;
private contexts: loki.Collection<{ private contexts: loki.Collection<{
@ -112,8 +112,8 @@ export default class 藍 {
this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`); this.log(`Installing ${chalk.cyan.italic(m.name)}\tmodule...`);
const res = m.install(); const res = m.install();
if (res != null) { if (res != null) {
if (res.onMention) this.onMentionHandlers.push(res.onMention); if (res.mentionHook) this.mentionHooks.push(res.mentionHook);
if (res.onContextReply) this.onContextReplyHandlers[m.name] = res.onContextReply; if (res.contextHook) this.contextHooks[m.name] = res.contextHook;
} }
}); });
@ -135,7 +135,7 @@ export default class 藍 {
let reaction = 'love'; let reaction = 'love';
if (context != null) { if (context != null) {
const handler = this.onContextReplyHandlers[context.module]; const handler = this.contextHooks[context.module];
const res = handler(msg, context.data); const res = handler(msg, context.data);
if (res != null && typeof res === 'object') { if (res != null && typeof res === 'object') {
@ -144,7 +144,7 @@ export default class 藍 {
} else { } else {
let res: boolean | HandlerResult; let res: boolean | HandlerResult;
this.onMentionHandlers.some(handler => { this.mentionHooks.some(handler => {
res = handler(msg); res = handler(msg);
return res === true || typeof res === 'object'; return res === true || typeof res === 'object';
}); });

View file

@ -15,13 +15,13 @@ export default class CoreModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention, mentionHook: this.mentionHook,
onContextReply: this.onContextReply contextHook: this.contextHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (!msg.text) return false; if (!msg.text) return false;
return ( return (
@ -313,7 +313,7 @@ export default class CoreModule extends Module {
} }
@autobind @autobind
private onContextReply(msg: MessageLike, data: any) { private contextHook(msg: MessageLike, data: any) {
if (msg.text == null) return; if (msg.text == null) return;
const done = () => { const done = () => {

View file

@ -9,12 +9,12 @@ export default class DiceModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.text == null) return false; if (msg.text == null) return false;
const query = msg.text.match(/([0-9]+)[dD]([0-9]+)/); const query = msg.text.match(/([0-9]+)[dD]([0-9]+)/);

View file

@ -132,12 +132,12 @@ export default class EmojiModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.includes(['顔文字', '絵文字', 'emoji', '福笑い'])) { if (msg.includes(['顔文字', '絵文字', 'emoji', '福笑い'])) {
const hand = hands[Math.floor(Math.random() * hands.length)]; const hand = hands[Math.floor(Math.random() * hands.length)];
const face = faces[Math.floor(Math.random() * faces.length)]; const face = faces[Math.floor(Math.random() * faces.length)];

View file

@ -8,12 +8,12 @@ export default class FollowModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.text && msg.includes(['フォロー', 'フォロバ', 'follow me'])) { if (msg.text && msg.includes(['フォロー', 'フォロバ', 'follow me'])) {
if (!msg.user.isFollowing) { if (!msg.user.isFollowing) {
this.ai.api('following/create', { this.ai.api('following/create', {

View file

@ -11,12 +11,12 @@ export default class FortuneModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) { if (msg.includes(['占', 'うらな', '運勢', 'おみくじ'])) {
const date = new Date(); const date = new Date();
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDate()}@${msg.userId}`; const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDate()}@${msg.userId}`;

View file

@ -25,13 +25,13 @@ export default class GuessingGameModule extends Module {
//#endregion //#endregion
return { return {
onMention: this.onMention, mentionHook: this.mentionHook,
onContextReply: this.onContextReply contextHook: this.contextHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.includes(['数当て', '数あて'])) { if (msg.includes(['数当て', '数あて'])) {
const exist = this.guesses.findOne({ const exist = this.guesses.findOne({
userId: msg.userId, userId: msg.userId,
@ -70,7 +70,7 @@ export default class GuessingGameModule extends Module {
} }
@autobind @autobind
private onContextReply(msg: MessageLike) { private contextHook(msg: MessageLike) {
if (msg.text == null) return; if (msg.text == null) return;
const exist = this.guesses.findOne({ const exist = this.guesses.findOne({

View file

@ -8,12 +8,12 @@ export default class PingModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.text && msg.text.includes('ping')) { if (msg.text && msg.text.includes('ping')) {
msg.reply('PONG!'); msg.reply('PONG!');
return true; return true;

View file

@ -28,12 +28,12 @@ export default class ReversiModule extends Module {
this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg)); this.reversiConnection.on('matched', msg => this.onReversiGameStart(msg));
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) { if (msg.includes(['リバーシ', 'オセロ', 'reversi', 'othello'])) {
if (config.reversiEnabled) { if (config.reversiEnabled) {
msg.reply(serifs.reversi.ok); msg.reply(serifs.reversi.ok);

View file

@ -9,12 +9,12 @@ export default class TimerModule extends Module {
@autobind @autobind
public install() { public install() {
return { return {
onMention: this.onMention mentionHook: this.mentionHook
}; };
} }
@autobind @autobind
private onMention(msg: MessageLike) { private mentionHook(msg: MessageLike) {
const secondsQuery = (msg.text || '').match(/([0-9]+)秒/); const secondsQuery = (msg.text || '').match(/([0-9]+)秒/);
const minutesQuery = (msg.text || '').match(/([0-9]+)分/); const minutesQuery = (msg.text || '').match(/([0-9]+)分/);
const hoursQuery = (msg.text || '').match(/([0-9]+)時間/); const hoursQuery = (msg.text || '').match(/([0-9]+)時間/);