Make async

This commit is contained in:
syuilo 2019-01-23 21:49:10 +09:00
parent 29eae273e5
commit e88bffa93c
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
10 changed files with 18 additions and 18 deletions

View file

@ -15,8 +15,8 @@ import getCollection from './utils/get-collection';
import Stream from './stream'; import Stream from './stream';
import log from './utils/log'; import log from './utils/log';
type MentionHook = (msg: Message) => boolean | HandlerResult; type MentionHook = (msg: Message) => Promise<boolean | HandlerResult>;
type ContextHook = (msg: Message, data?: any) => void | HandlerResult; type ContextHook = (msg: Message, data?: any) => Promise<void | HandlerResult>;
export type HandlerResult = { export type HandlerResult = {
reaction: string; reaction: string;
@ -165,7 +165,7 @@ export default class 藍 {
// なければそれぞれのモジュールについてフックが引っかかるまで呼び出し // なければそれぞれのモジュールについてフックが引っかかるまで呼び出し
if (context != null) { if (context != null) {
const handler = this.contextHooks[context.module]; const handler = this.contextHooks[context.module];
const res = handler(msg, context.data); const res = await handler(msg, context.data);
if (res != null && typeof res === 'object') { if (res != null && typeof res === 'object') {
reaction = res.reaction; reaction = res.reaction;
@ -173,10 +173,10 @@ export default class 藍 {
} else { } else {
let res: boolean | HandlerResult; let res: boolean | HandlerResult;
this.mentionHooks.some(handler => { for (const handler of this.mentionHooks) {
res = handler(msg); res = await handler(msg);
return res === true || typeof res === 'object'; if (res === true || typeof res === 'object') break;
}); }
if (res != null && typeof res === 'object') { if (res != null && typeof res === 'object') {
reaction = res.reaction; reaction = res.reaction;

View file

@ -21,7 +21,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
if (!msg.text) return false; if (!msg.text) return false;
return ( return (
@ -313,7 +313,7 @@ export default class extends Module {
} }
@autobind @autobind
private contextHook(msg: Message, data: any) { private async contextHook(msg: Message, data: any) {
if (msg.text == null) return; if (msg.text == null) return;
const done = () => { const done = () => {

View file

@ -14,7 +14,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -137,7 +137,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -13,7 +13,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -16,7 +16,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -32,7 +32,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
if (!msg.includes(['数当て', '数あて'])) return false; if (!msg.includes(['数当て', '数あて'])) return false;
const exist = this.guesses.findOne({ const exist = this.guesses.findOne({
@ -69,7 +69,7 @@ export default class extends Module {
} }
@autobind @autobind
private contextHook(msg: Message) { private async contextHook(msg: Message) {
if (msg.text == null) return; if (msg.text == null) return;
const exist = this.guesses.findOne({ const exist = this.guesses.findOne({

View file

@ -13,7 +13,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -33,7 +33,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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

@ -14,7 +14,7 @@ export default class extends Module {
} }
@autobind @autobind
private mentionHook(msg: Message) { private async mentionHook(msg: Message) {
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]+)時間/);