Add welcome module

This commit is contained in:
syuilo 2018-08-14 06:14:47 +09:00
parent 05ada0d74e
commit de72383156
4 changed files with 64 additions and 2 deletions

View file

@ -20,11 +20,17 @@ export default class 藍 {
*/ */
private connection: any; private connection: any;
/**
*
*/
private localTimelineConnection: any;
private modules: IModule[] = []; private modules: IModule[] = [];
constructor(account: any) { constructor(account: any) {
this.account = account; this.account = account;
//#region Home stream
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], { this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], {
WebSocket: WebSocket WebSocket: WebSocket
}); });
@ -42,9 +48,27 @@ export default class 藍 {
this.onMessage(msg); this.onMessage(msg);
}); });
//#endregion
if (config.reversiEnabled) { //#region Local timeline stream
} this.localTimelineConnection = new ReconnectingWebSocket(`${config.wsUrl}/local-timeline?i=${config.i}`, [], {
WebSocket: WebSocket
});
this.localTimelineConnection.addEventListener('open', () => {
console.log('local-timeline stream opened');
});
this.localTimelineConnection.addEventListener('close', () => {
console.log('local-timeline stream closed');
});
this.localTimelineConnection.addEventListener('message', message => {
const msg = JSON.parse(message.data);
this.onLocalNote(msg.body);
});
//#endregion
} }
public install = (module: IModule) => { public install = (module: IModule) => {
@ -82,6 +106,12 @@ export default class 藍 {
} }
} }
private onLocalNote = (note: any) => {
this.modules.filter(m => m.hasOwnProperty('onLocalNote')).forEach(m => {
return m.onLocalNote(note);
});
}
private onMention = (msg: MessageLike) => { private onMention = (msg: MessageLike) => {
console.log(`mention received: ${msg.id}`); console.log(`mention received: ${msg.id}`);

View file

@ -7,6 +7,7 @@ import EmojiModule from './modules/emoji';
import FortuneModule from './modules/fortune'; import FortuneModule from './modules/fortune';
import GuessingGameModule from './modules/guessing-game'; import GuessingGameModule from './modules/guessing-game';
import KeywordModule from './modules/keyword'; import KeywordModule from './modules/keyword';
import WelcomeModule from './modules/welcome';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
const promiseRetry = require('promise-retry'); const promiseRetry = require('promise-retry');
@ -20,6 +21,7 @@ promiseRetry(retry => {
const ai = new (account); const ai = new (account);
ai.install(new PingModule()); ai.install(new PingModule());
ai.install(new WelcomeModule());
ai.install(new EmojiModule()); ai.install(new EmojiModule());
ai.install(new FortuneModule()); ai.install(new FortuneModule());
ai.install(new GuessingGameModule()); ai.install(new GuessingGameModule());

View file

@ -5,5 +5,6 @@ export default interface IModule {
name: string; name: string;
install?: (ai: ) => void; install?: (ai: ) => void;
onMention?: (msg: MessageLike) => boolean; onMention?: (msg: MessageLike) => boolean;
onLocalNote?: (note: any) => void;
onReplyThisModule?: (msg: MessageLike) => void; onReplyThisModule?: (msg: MessageLike) => void;
} }

View file

@ -0,0 +1,29 @@
import from '../../ai';
import IModule from '../../module';
export default class WelcomeModule implements IModule {
public name = 'welcome';
private ai: ;
public install = (ai: ) => {
this.ai = ai;
}
public onLocalNote = (note: any) => {
if (note.user.notesCount == 1) {
setTimeout(() => {
this.ai.api('notes/create', {
renoteId: note.id
});
}, 3000);
setTimeout(() => {
this.ai.api('notes/reactions/create', {
noteId: note.id,
reaction: 'congrats'
});
}, 5000);
}
}
}