2019-01-15 03:29:11 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-08-11 06:26:25 +00:00
|
|
|
import 藍 from './ai';
|
2018-08-27 11:22:59 +00:00
|
|
|
import Friend from './friend';
|
2018-08-28 21:37:14 +00:00
|
|
|
import { User } from './misskey/user';
|
2018-09-02 12:50:57 +00:00
|
|
|
import includes from './utils/includes';
|
2018-09-02 13:35:38 +00:00
|
|
|
import or from './utils/or';
|
2019-01-15 03:29:11 +00:00
|
|
|
import chalk from 'chalk';
|
2018-08-12 14:03:00 +00:00
|
|
|
const delay = require('timeout-as-promise');
|
2018-08-11 01:42:06 +00:00
|
|
|
|
|
|
|
export default class MessageLike {
|
|
|
|
private ai: 藍;
|
|
|
|
private messageOrNote: any;
|
|
|
|
public isMessage: boolean;
|
|
|
|
|
2018-08-28 21:37:14 +00:00
|
|
|
public get id(): string {
|
2018-08-11 01:42:06 +00:00
|
|
|
return this.messageOrNote.id;
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:37:14 +00:00
|
|
|
public get user(): User {
|
2018-08-11 12:28:17 +00:00
|
|
|
return this.messageOrNote.user;
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:37:14 +00:00
|
|
|
public get userId(): string {
|
2018-08-11 01:42:06 +00:00
|
|
|
return this.messageOrNote.userId;
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:37:14 +00:00
|
|
|
public get text(): string {
|
2018-08-11 01:42:06 +00:00
|
|
|
return this.messageOrNote.text;
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:37:14 +00:00
|
|
|
public get replyId(): string {
|
2018-08-12 14:03:00 +00:00
|
|
|
return this.messageOrNote.replyId;
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:22:59 +00:00
|
|
|
public friend: Friend;
|
2018-08-27 10:04:09 +00:00
|
|
|
|
2018-08-11 01:42:06 +00:00
|
|
|
constructor(ai: 藍, messageOrNote: any, isMessage: boolean) {
|
|
|
|
this.ai = ai;
|
|
|
|
this.messageOrNote = messageOrNote;
|
|
|
|
this.isMessage = isMessage;
|
2018-08-27 10:04:09 +00:00
|
|
|
|
2018-08-27 11:22:59 +00:00
|
|
|
this.friend = new Friend(ai, { user: this.user });
|
|
|
|
|
|
|
|
// メッセージなどに付いているユーザー情報は省略されている場合があるので完全なユーザー情報を持ってくる
|
|
|
|
this.ai.api('users/show', {
|
2018-08-27 10:04:09 +00:00
|
|
|
userId: this.userId
|
2018-08-27 11:22:59 +00:00
|
|
|
}).then(user => {
|
|
|
|
this.friend.updateUser(user);
|
2018-08-27 10:04:09 +00:00
|
|
|
});
|
2018-08-11 01:42:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 03:29:11 +00:00
|
|
|
@autobind
|
|
|
|
public async reply(text: string, cw?: string) {
|
2018-09-02 13:23:10 +00:00
|
|
|
if (text == null) return;
|
|
|
|
|
2019-01-15 03:29:11 +00:00
|
|
|
this.ai.log(`>>> Sending reply to ${chalk.underline(this.id)}`);
|
2018-08-11 03:34:24 +00:00
|
|
|
|
2018-08-12 14:03:00 +00:00
|
|
|
await delay(2000);
|
|
|
|
|
|
|
|
if (this.isMessage) {
|
|
|
|
return await this.ai.sendMessage(this.messageOrNote.userId, {
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return await this.ai.post({
|
|
|
|
replyId: this.messageOrNote.id,
|
|
|
|
text: text,
|
|
|
|
cw: cw
|
|
|
|
});
|
|
|
|
}
|
2018-08-11 01:42:06 +00:00
|
|
|
}
|
2018-09-02 12:50:57 +00:00
|
|
|
|
2019-01-15 03:29:11 +00:00
|
|
|
@autobind
|
|
|
|
public includes(words: string[]): boolean {
|
2018-09-02 12:50:57 +00:00
|
|
|
return includes(this.text, words);
|
|
|
|
}
|
2018-09-02 13:23:10 +00:00
|
|
|
|
2019-01-15 03:29:11 +00:00
|
|
|
@autobind
|
|
|
|
public or(words: (string | RegExp)[]): boolean {
|
2018-09-02 13:23:10 +00:00
|
|
|
return or(this.text, words);
|
|
|
|
}
|
2018-08-11 01:42:06 +00:00
|
|
|
}
|