-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleFeature.ts
59 lines (50 loc) · 2.08 KB
/
ExampleFeature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Comment, PrivateMessage, Submission } from "snoowrap";
import Command from "../../shared/commands/Command";
import Feature from "../../shared/features/Feature";
import Logger from "../../shared/logging/Logger";
import { injectable } from "tsyringe";
/**
* An example feature that reacts to comments, submissions and private messages.
*/
@injectable()
export default class ExampleFeature extends Feature {
constructor(logger: Logger) {
super(logger);
}
/**
* Logs comment, replies if it contains "hello".
*/
public async onComment(comment: Comment, _: Command): Promise<void> {
this._logger.info(`onComment, author: ${comment.author.name}, comment body: ${comment.body}`);
if (comment.body.toLowerCase().includes('hello')) {
//@ts-ignore
await comment.reply('Hi!');
}
}
/**
* Logs submission, replies if its title contains "hello".
*/
public async onSubmission(submission: Submission): Promise<void> {
this._logger.info(`onSubmission, author: ${submission.author.name}, submission title: ${submission.title}`);
if (submission.title.toLowerCase().includes('hello')) {
//@ts-ignore
await submission.reply(`Hi!`);
}
}
/**
* Logs private message, replies with a copy of its body.
*/
public async onPrivateMessage(privateMessage: PrivateMessage, command: Command): Promise<void> {
const options = command.options; // Command is never null (see Application.processNewPrivateMessages)
const optionsAsString = options == null ? '' : Array.from(command.options).map((value) => `${value[0]}=${value[1]}`);
this._logger.info(`onPrivateMessage, author: ${privateMessage.author.name}, private message body: ${privateMessage.body}, command: ${command.name}, command options: ${optionsAsString}`);
//@ts-ignore
await privateMessage.reply(`echo: ${privateMessage.body}`);
}
/**
* Logs onInit event.
*/
public async onInit(): Promise<void> {
this._logger.info('onInit');
}
}