-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
176 lines (157 loc) · 4.6 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//Import dependencies
import {
AutojoinRoomsMixin,
MatrixClient,
SimpleFsStorageProvider,
RichRepliesPreprocessor,
} from "matrix-bot-sdk";
import { readFileSync } from "node:fs";
import { parse } from "yaml";
import axios from "axios";
//Parse YAML configuration file
const loginFile = readFileSync("./db/login.yaml", "utf8");
const loginParsed = parse(loginFile);
const homeserver = loginParsed["homeserver-url"];
const accessToken = loginParsed["login-token"];
const prefix = loginParsed.prefix;
const msgLimit = loginParsed["msg-limit"];
//keep track of whos ranting
const rants = new Map();
//the bot sync something idk bro it was here in the example so i dont touch it ;-;
const storage = new SimpleFsStorageProvider("bot.json");
//login to client
const client = new MatrixClient(homeserver, accessToken, storage);
AutojoinRoomsMixin.setupOnClient(client);
//do not include replied message in message
client.addPreprocessor(new RichRepliesPreprocessor(false));
const filter = {
//dont expect any presence from m.org, but in the case presence shows up its irrelevant to this bot
presence: { senders: [] },
room: {
//ephemeral events are never used in this bot, are mostly inconsequentail and irrelevant
ephemeral: { senders: [] },
//we fetch state manually later, hopefully with better load balancing
state: {
senders: [],
types: [],
lazy_load_members: true,
},
//we dont need much room history, just enough to catch recent messages just before the bot came online
timeline: {
limit: 20,
},
},
};
//Start Client
client.start(filter).then(async (filter) => {
console.log("Client started!");
});
//when the client recieves an event
client.on("room.event", async (roomId, event) => {
//ignore events sent by self, unless its a banlist policy update
if (event.sender === client.getUserId()) {
return;
}
//we just want raw text tbh
if (!event?.content?.body) return;
const body = event.content.body.toLowerCase();
if (body.startsWith(`${prefix}help`)) {
client.replyNotice(
roomId,
event,
"You can find usage information and source code available at https://github.com/jjj333-p/inspirobot-disruptor-matrix",
);
} else if (body.startsWith(`${prefix}help`)) {
client.replyNotice(
roomId,
event,
"You can find usage information and source code available at https://github.com/jjj333-p/matrix-word-counter",
);
} else if (body.startsWith(`${prefix}inspire`)) {
//api to generate a quote image
let imageUrl;
try {
const r = await axios.get("https://inspirobot.me/api?generate=true");
imageUrl = r.data;
} catch (e) {
console.warn(`Failed to generate inspirational quote with error\n${e}`);
client.replyNotice(
roomId,
event,
"Unable to generate an inspiring quote",
);
}
//upload it
let mxc;
try {
mxc = await client.uploadContentFromUrl(imageUrl);
} catch (e) {
console.warn(`Failed to upload inspirational quote with error\n${e}`);
client.replyNotice(roomId, event, "Unable to upload the inspiring quote");
}
//generate content to send
const eventContent = {
body: "inspiration.jpg",
info: { "agency.pain.image-origin": imageUrl },
msgtype: "m.image",
url: mxc,
"m.relates_to": {
"m.in_reply_to": {
event_id: event.event_id,
},
},
"m.mentions": {
user_ids: [event.sender],
},
};
client.sendRawEvent(roomId, "m.room.message", eventContent);
} else {
let rantData = rants.get(roomId);
if (rantData?.user === event.sender) {
rantData.count++;
} else {
const d = {
user: event.sender,
count: 1,
};
rants.set(roomId, d);
rantData = d;
}
if (rantData.count >= msgLimit) {
rants.delete(roomId);
//api to generate a quote image
let imageUrl;
try {
const r = await axios.get("https://inspirobot.me/api?generate=true");
imageUrl = r.data;
} catch (e) {
console.warn(`Failed to generate inspirational quote with error\n${e}`);
client.sendNotice(roomId, "Unable to generate an inspiring quote");
}
//upload it
let mxc;
try {
mxc = await client.uploadContentFromUrl(imageUrl);
} catch (e) {
console.warn(`Failed to upload inspirational quote with error\n${e}`);
client.sendNotice(roomId, "Unable to upload the inspiring quote");
}
//generate content to send
const eventContent = {
body: "inspiration.jpg",
info: { "agency.pain.image-origin": imageUrl },
msgtype: "m.image",
url: mxc,
"m.relates_to": {
"m.in_reply_to": {
event_id: event.event_id,
},
},
"m.mentions": {
user_ids: [event.sender],
},
};
client.sendRawEvent(roomId, "m.room.message", eventContent);
}
}
});