generated from mezotv/Discord-Bot-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prometheus/Grafana metrics (#232)
* add promhandler * oop * add: user/server metrics * fix: text fill translations
- Loading branch information
Showing
9 changed files
with
185 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,120 @@ | ||
import WouldYou from "./wouldYou"; | ||
|
||
interface GuildDB { | ||
language: string; | ||
} | ||
|
||
export function generateWYR( | ||
client: WouldYou, | ||
text: string, | ||
id: string, | ||
guilddb: GuildDB, | ||
): object { | ||
const wyr = /^(?!.*(?:would you rather)).*$/i; | ||
const wyrRegexes: Record<string, RegExp> = { | ||
"en_EN": /^(?!.*(?:would you rather)).*$/i, | ||
"de_DE": /^(?!.*(?:würdest du eher)).*$/i, | ||
"es_ES": /^(?!.*(?:preferirías)).*$/i, | ||
"fr_FR": /^(?!.*(?:préfères-tu)).*$/i, | ||
}; | ||
|
||
if (wyr.test(text)) { | ||
const wyrMap: Record<string, string> = { | ||
"en_EN": "Would you rather", | ||
"de_DE": "Würdest du eher", | ||
"es_ES": "Preferirías", | ||
"fr_FR": "Préfères-tu", | ||
}; | ||
|
||
const guildLanguage = guilddb.language as string; | ||
const wyrRegexPattern = wyrRegexes[guildLanguage]; | ||
const responsePhrase = wyrMap[guildLanguage]; | ||
|
||
if (wyrRegexPattern.test(text.toLowerCase())) { | ||
client.customAdd.set(id, { | ||
type: "wouldyourather", | ||
text: `Would you rather ${text}`, | ||
text: `${responsePhrase} ${text}`, | ||
}); | ||
return { | ||
value: false, | ||
type: "wouldyourather", | ||
text: `Would you rather ${text}`, | ||
text: `${responsePhrase} ${text}`, | ||
}; | ||
} else { | ||
return { value: true, type: "wouldyourather", text: text }; | ||
} | ||
} | ||
|
||
|
||
export function generateWWYD( | ||
client: WouldYou, | ||
text: string, | ||
id: string, | ||
guilddb: GuildDB, | ||
): object { | ||
const wwyd = /^(?!.*(?:what would you do)).*$/i; | ||
const languageRegex: Record<string, RegExp> = { | ||
"en_EN": /^(?!.*(?:what would you do)).*$/i, | ||
"de_DE": /^(?!.*(?:was würdest du tun)).*$/i, | ||
"es_ES": /^(?!.*(?:qué harías)).*$/i, | ||
"fr_FR": /^(?!.*(?:que ferais-tu)).*$/i, | ||
}; | ||
|
||
if (wwyd.test(text)) { | ||
const languageMap: Record<string, string> = { | ||
"en_EN": "What would you do", | ||
"de_DE": "Was würdest du tun", | ||
"es_ES": "Qué harías", | ||
"fr_FR": "Que ferais-tu", | ||
}; | ||
|
||
const guildLanguage = guilddb.language as string; | ||
const languageRegexPattern = languageRegex[guildLanguage]; | ||
const responsePhrase = languageMap[guildLanguage]; | ||
if (languageRegexPattern.test(text.toLowerCase())) { | ||
client.customAdd.set(id, { | ||
type: "wwyd", | ||
text: `What would you do ${text}`, | ||
text: `${responsePhrase} ${text}`, | ||
}); | ||
return { value: false, type: "wwyd", text: `What would you do ${text}` }; | ||
return { value: false, type: "wwyd", text: `${responsePhrase} ${text}` }; | ||
} else { | ||
return { value: true, type: "wwyd", text: text }; | ||
} | ||
} | ||
|
||
|
||
export function generateNHIE( | ||
client: WouldYou, | ||
text: string, | ||
id: string, | ||
guilddb: GuildDB, | ||
): object { | ||
const nhie = /^(?!.*(?:never have i ever)).*$/i; | ||
const nhieRegexes: Record<string, RegExp> = { | ||
"en_EN": /^(?!.*(?:never have i ever)).*$/i, | ||
"de_DE": /^(?!.*(?:niemals habe ich)).*$/i, | ||
"es_ES": /^(?!.*(?:nunca he)).*$/i, | ||
"fr_FR": /^(?!.*(?:jamais je)).*$/i, | ||
}; | ||
|
||
const nhieMap: Record<string, string> = { | ||
"en_EN": "Never have I ever", | ||
"de_DE": "Niemals habe ich", | ||
"es_ES": "Nunca he", | ||
"fr_FR": "Jamais je", | ||
}; | ||
|
||
if (nhie.test(text)) { | ||
const guildLanguage = guilddb.language as string; | ||
const nhieRegexPattern = nhieRegexes[guildLanguage]; | ||
const responsePhrase = nhieMap[guildLanguage]; | ||
|
||
if (nhieRegexPattern.test(text.toLowerCase())) { | ||
client.customAdd.set(id, { | ||
type: "neverhaveiever", | ||
text: `Never have I ever ${text}`, | ||
text: `${responsePhrase} ${text}`, | ||
}); | ||
return { | ||
value: false, | ||
type: "neverhaveiever", | ||
text: `Never have I ever ${text}`, | ||
text: `${responsePhrase} ${text}`, | ||
}; | ||
} else { | ||
return { value: true, type: "neverhaveiever", text: text }; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import express from "express"; | ||
import { Registry, Gauge } from "prom-client" | ||
import WouldYou from "./wouldYou"; | ||
|
||
export default class prometheusClient { | ||
private registry: Registry; | ||
private serverGauge: Gauge; | ||
private userGauge: Gauge; | ||
private client: WouldYou; | ||
|
||
constructor(client: WouldYou) { | ||
this.client = client; | ||
} | ||
|
||
initialize() { | ||
this.registry = new Registry(); | ||
this.serverGauge = new Gauge({ | ||
name: "server_count", | ||
help: "The amount of servers the bot is in.", | ||
registers: [this.registry], | ||
}); | ||
this.userGauge = new Gauge({ | ||
name: "user_count", | ||
help: "The amount of users the bot has.", | ||
registers: [this.registry], | ||
}); | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
|
||
// Expose Prometheus metrics endpoint | ||
app.get("/metrics", async (req, res) => { | ||
this.serverGauge.set(this.client.guilds.cache.size); | ||
this.userGauge.set(this.client.guilds.cache | ||
.reduce((a, b) => a + b.memberCount, 0)); | ||
res.end(await this.registry.metrics()); | ||
}); | ||
|
||
app.listen(3029, () => { | ||
console.log("Prometheus metrics listening on port 3029"); | ||
}); | ||
} | ||
|
||
|
||
getMetrics() { | ||
return this.registry.metrics(); | ||
} | ||
} |
Oops, something went wrong.