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
|
// Require the necessary discord.js classes
import { Client, GatewayIntentBits, Message, MessageType } from 'discord.js';
import { request } from "undici";
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages] });
// `autofeur_db` service
export const DB = process.env.DB || "http://localhost:3000";
/**
* Completes a grapheme using the `autofeur_db` service.
* @param grapheme Grapheme to complete
* @returns Completed grapheme
*/
export const completeWord = (grapheme) => {
let resp = request(`${DB}?grapheme=${encodeURIComponent(grapheme)}`);
return resp.then((x) => {
if (x.statusCode === 200) {
return x.body.text();
}
});
};
/**
* Cleans a sentence for usage with this program, strips unwanted chars
* @param sentence Raw discord sentence
* @returns The last word without any specials characters
*/
const cutWord = (sentence) => {
let lastWord = sentence
.replaceAll("?", "")
.replaceAll("!", "")
.replaceAll(".", "")
.replaceAll(",", "")
.replaceAll(";", "")
.trim()
.split(" ")
.slice(-1)[0]
.replaceAll(/(\s)?([^\x41-\x5A\s^\x61-\x7A^\xC0-\xFF])/g, "")
.replaceAll(/(?:https?|ftp):\/\/[\n\S]+/g, '');
return lastWord;
};
client.on("messageCreate", async (message) => {
// we shall not repond to bots
if (message.author.bot) return;
try {
// Get the completed word found by the db.
let response = await completeWord(cutWord(message.cleanContent));
// Ignore if there is no completion
if ((response || response === "") && (Math.random() > 0.95 || message.channelId == '1248226018406301696' || message.guild == null)) {
message.reply(response);
}
} catch (e) {
console.log(e);
}
});
client.on("messageUpdate", async (message) => {
if (message.author.bot) return;
try {
// Get the completed word found by the db.
let response = await completeWord(cutWord(message.cleanContent));
// Ignore if there is no completion
if ((response || response === "") && (Math.random() > 0.95 || message.channelId == '1248226018406301696' || message.guild == null)) {
message.reply(response);
}
} catch (e) {
console.log(e);
}
});
client.login(process.env.TOKEN);
|