-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (131 loc) · 6.77 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
const fs = require('fs');
const fetchArticleContent = require('./modules/fetchArticle.js');
const fetchHowTos = require('./modules/fetchHowTos.js');
/*****************/
/* CONFIGURATION */
const commandPrefix = 'kerific'; // The prefix for bot commands
const replyCharacterCount = 1950; // Discord has a 2000 character limit
/* END CONFIGURATION */
/*********************/
// Synchronously read the content
const quickLinks = fs.readFileSync('./content/quickLinks.md', { encoding: 'utf8', flag: 'r', cache: false });
const introduction = fs.readFileSync('./content/introduction.md', { encoding: 'utf8', flag: 'r', cache: false });
const help = fs.readFileSync('./content/help.md', { encoding: 'utf8', flag: 'r', cache: false });
const { Client, IntentsBitField } = require('discord.js');
require('dotenv').config();
const htmlToText = require('html-to-text');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
],
});
client.login(process.env.TOKEN); // Replace with your bot's token
// The pattern looks for the occurrence of the word "See" followed by an optional colon and space, then a link enclosed in <a> tags. It captures the text inside the <a> tags and returns it as the result. If there is no match, it returns null.
function findLinkTextAfterSee(str) {
const pattern = /See\s?:? ?<.*?<a.*?>(.*?)<\/a>/is;
const match = pattern.exec(str);
console.log('match: ', match);
return match ? match[1] : null;
}
// // Example usage:
// const exampleString = 'Random text See <a href="link.html">Link Text</a> more text';
// console.log(findLinkTextAfterSee(exampleString)); // Outputs: "Link Text"
client.on('ready', (c) => {
console.log(`👍 ${c.user.tag} is online!`);
});
client.on('messageCreate', (message) => {
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} define`)) {
message.reply('Ok, one moment please…');
const input = message.content.slice(commandPrefix.length + 8); // Remove the command prefix
const glossaryJsonUrl = process.env.GLOSSARY_JSON_URL;
const removeUrls = (text) => {
// Regex pattern to match URLs
const urlPattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
// Replace URLs with an empty string
return text.replace(urlPattern, '');
};
// Fetch and process glossary data
fetch(glossaryJsonUrl)
.then(response => response.json())
.then(glossaryData => {
let reply = "";
glossaryData.forEach(glossaryEntry => {
if (glossaryEntry.term === input) {
reply = `These are the ${glossaryEntry.definitions.length} definitions found for “${input}”:\n`;
glossaryEntry.definitions.forEach(definition => {
if (findLinkTextAfterSee(definition.definition) !== null) {
glossaryData.forEach(element => {
if (element.term === findLinkTextAfterSee(definition.definition)) {
console.log('findLinkTextAfterSee(definition.definition: ', findLinkTextAfterSee(definition.definition));
element.definitions.forEach(el => {
if (el.organisation === definition.organisation) {
reply += '# ' + el.organisation + " definition:\n";
reply += "\nRedirected\n";
const removedUrls = removeUrls(el.definition);
reply += htmlToText.htmlToText(removedUrls) + "\n";
}
});
};
});
} else {
reply += '# ' + definition.organisation + " definition:\n";
const removedUrls = removeUrls(definition.definition);
reply += htmlToText.htmlToText(removedUrls) + "\n";
}
});
reply += "\n------------\n";
reply += "That's all we could find. Type `kerific help` for more info on commands.";
}
});
if (reply.length > replyCharacterCount) {
reply = reply.slice(0, replyCharacterCount);
reply += "\n\n… answer truncated because it is too long.";
message.reply(reply);
} else if (reply === "") {
message.reply(`No definitions found for “${input}”.`);
} else {
message.reply(reply);
}
})
} // end if message starts with define
// Show Introduction
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} introduction`)) {
message.reply(introduction + "\n\n" + help);
}
// Show Help
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} help`) ||
message.content.toLocaleLowerCase().startsWith(`${commandPrefix} ?`)) {
message.reply(help);
}
// Show Keri Whitepaper
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} whitepaper keri`)) {
message.reply(`[https://github.com/SmithSamuelM/Papers/blob/master/whitepapers/KERI_WP_2.x.web.pdf]`);
}
// Show Quicklinks
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} show quicklinks`)) {
message.reply(quickLinks);
}
// Show a URL
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} show url`)) {
const input = message.content.slice(commandPrefix.length + 10); // Adjust depending on your command length
let articleContent;
(async () => {
const url = input;
articleContent = await fetchArticleContent(url);
message.reply(articleContent.slice(0, replyCharacterCount));
})();
}
// Show How To articles
if (message.content.toLocaleLowerCase().startsWith(`${commandPrefix} how to`)) {
message.reply(`Ok, searching…`);
const input = message.content.slice(commandPrefix.length + 8); // Remove the command prefix
let articleContent;
(async () => {
articleContent = await fetchHowTos(input);
message.reply(articleContent.slice(0, replyCharacterCount));
})();
}
});