summaryrefslogtreecommitdiff
path: root/matrix.js
blob: 8dea476ff4f45ae8708b96edfb1c8474ff644376 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function startMatrix() {
    try {
        const sdk = require("matrix-js-sdk");
        const showdown = require('showdown');

        global.matrixSend = (room, message, data) => {
            return new Promise((res, rej) => {
                client.sendEvent(room, "m.room.message", {
                    msgtype: "m.text",
                    body: data ?? message,
                    format: "org.matrix.custom.html",
                    formatted_body: markdown(message),
                }, "", (err, response) => {
                    if (err) rej(err);
                    res(response);
                });
            })
        }

        global.markdown = (text) => {
            let converter = new showdown.Converter();
            return converter.makeHtml(text);
        }

        const client = sdk.createClient({
            baseUrl: "https://chat.equestria.dev",
            userId: require('./credentials.json').username,
            accessToken: require('./credentials.json').token
        });

        (async () => {
            await client.startClient({ initialSyncLimit: 0 });

            client.once("sync", async function (state, prevState, res) {
                if (state === "PREPARED") {
                    console.log("Ready!");

                    client.setPresence({
                        presence: "online"
                    });
                } else {
                    console.log(state);
                    process.exit(1);
                }
            });

            client.on("Room.timeline", function (event, room) {
                if (event.getType() !== "m.room.message") {
                    return;
                }

                if (new Date().getTime() - event.event.origin_server_ts < 1000 && event.sender.userId !== require('./credentials.json').username) {
                    let message = event.event.content.body;

                    if (event.event.content.formatted_body && event.event.content.formatted_body.startsWith("<mx-reply>")) {
                        message = event.event.content.formatted_body.split("</mx-reply>")[1];
                    }

                    if (message.startsWith(".") && !message.startsWith("..") && message.trim() !== "." && message.trim() !== ".c.") {
                        let command;

                        try {
                            command = message.replaceAll(/ +/g, " ");
                            if (command.startsWith(". ")) command = "." + command.substring(2);
                            command = command.split(" ")[0].substring(1).replaceAll("/", "-");
                            let parameter = message.split(" ").splice(1).join(" ");

                            require('./handler')({
                                command,
                                parameter,
                                source: "matrix",
                                event,
                                room,
                                client
                            });
                        } catch (e) {
                            matrixSend(room.roomId, "❓ Hmm, something isn't quite right! I was trying to process your message and something wrong happened. I think you need to report this so it can be fixed!\n\n<details><summary>Show stack trace</summary>\n\n```plaintext\n" + e.stack + "\n```\n\n</details>");
                        }
                    } else if (message.includes(require('./credentials.json').username) || (event.event.content.formatted_body && event.event.content.formatted_body.includes(require('./credentials.json').username))) {
                        matrixSend(room.roomId, "👋 Hello! I think you forgot my prefix is `.`, use `.help` to get help.");
                    } else {
                        if (!lastMessages[room.roomId]) lastMessages[room.roomId] = [];
                        lastMessages[room.roomId].unshift(event.event.content.body);
                        lastMessages[room.roomId] = lastMessages[room.roomId].splice(0, 5);

                        let text = event.event.content.body.toLowerCase();

                        if (event.event.sender === "@cloudburst:equestria.dev" || event.event.sender === "@raindrops:equestria.dev") {
                            let worth = 1 / (score['score'] / 100);
                            if (worth < 0.1) worth = 0.1;

                            if (score['score'] === 0) {
                                score['score'] = 0.01;
                            }

                            if (text.includes("....")) {
                                score['score'] -= text.replace(/[^.]/gm, "").length * worth;
                            }

                            if (text.replace(/[^.]/gm, "").length === text.length) {
                                score['score'] -= text.replace(/[^.]/gm, "").length * 5 * worth;
                            }

                            if (text.includes(".c.")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("neigh")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("-c-")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("bye")) {
                                score['score'] -= 50 * worth;
                            }

                            if (text.endsWith(".")) {
                                score['score'] -= 10 * worth;
                            }

                            if (text.match(/^\*(.*)\*$/gm)) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("❤")) {
                                score['score'] += text.replace(/[^❤]/gm, "").length * 3 * worth;
                            }

                            if (text.includes("hehe")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("pff")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("mhm!")) {
                                score['score'] += 1 * worth;
                            }

                            if (text.includes("cutie")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("silly")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("sweetie")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("sis")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("nini")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("good night")) {
                                score['score'] += 2 * worth;
                            }

                            if (text.includes("hehehehe")) {
                                score['score'] += 4 * worth;
                            }

                            if (text.includes("hehehe")) {
                                score['score'] += 3 * worth;
                            }

                            if (text.includes("woo!")) {
                                score['score'] += 3 * worth;
                            }

                            if (text.includes("yay!")) {
                                score['score'] += 3 * worth;
                            }

                            if (text.includes("/s")) {
                                score['score'] += 3 * worth;
                            }

                            if (text.includes("love you")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("love u")) {
                                score['score'] += 5 * worth;
                            }

                            if (text.includes("^c^")) {
                                score['score'] += 3 * worth;
                            }

                            if (Math.floor(score['percentage']) < Math.floor(score['score'] / 20)) {
                                matrixSend(room.roomId, "🎉 Congrats! Your love score is now at " + Math.floor(score['score'] / 20) + "%, keep it up!");
                            }

                            score['percentage'] = score['score'] / 20;
                        }
                    }
                }
            });

            client.on("RoomMember.membership", function (event, member) {
                if (member.membership === "invite" && member.userId === require('./credentials.json').username) {
                    client.joinRoom(member.roomId).then(function () {
                        console.log("Auto-joined %s", member.roomId);
                    });
                }
            });
        })();
    } catch (e) {
        setTimeout(() => {
            startDiscord();
        }, 300000);
    }
}

startMatrix();