fix unhandled timeout on members fetch

This commit is contained in:
vincent 2026-02-26 20:58:42 +01:00
parent 8b6cfd6db9
commit 8e57652a72
4 changed files with 93 additions and 44 deletions

View file

@ -2,48 +2,55 @@ const DiscordClient = require("../../DiscordBot");
const IRCClient = require("../../IRCBot"); const IRCClient = require("../../IRCBot");
class Bridge { class Bridge {
constructor(config) { constructor(config) {
const verbose = config.verbose ?? false; const verbose = config.verbose ?? false;
const discordChannels = config.discordChannels; const discordChannels = config.discordChannels;
const ircChannels = config.ircChannels.map((chan) => chan.toLowerCase()); const ircChannels = config.ircChannels.map((chan) =>
chan.toLowerCase(),
);
if (!discordChannels || !ircChannels) { if (!discordChannels || !ircChannels) {
this.log("Missing channels"); this.log("Missing channels");
throw new Error("Missing discordChannels or ircChannels"); throw new Error("Missing discordChannels or ircChannels");
}
this.clients = [
new DiscordClient(this, verbose, discordChannels),
new IRCClient(this, verbose, ircChannels),
];
}
start() {
return new Promise(async (resolve, reject) => {
try {
for (const client of this.clients) {
await client.start();
} }
resolve(); this.clients = [
} catch (error) { new DiscordClient(
reject(error); this,
} verbose,
}); discordChannels,
} config.discordAppId,
),
async broadcast(msg) { new IRCClient(this, verbose, ircChannels),
this.log("Broadcasting", msg); ];
for (const client of this.clients) {
client.broadcast(msg);
} }
}
log(...args) { start() {
return console.log("[BRIDGE]", ...args); return new Promise(async (resolve, reject) => {
} try {
for (const client of this.clients) {
await client.start();
}
resolve();
} catch (error) {
reject(error);
}
});
}
async broadcast(msg) {
this.log("Broadcasting", msg);
for (const client of this.clients) {
client.broadcast(msg);
}
}
log(...args) {
return console.log("[BRIDGE]", ...args);
}
} }
module.exports = Bridge; module.exports = Bridge;

View file

@ -2,7 +2,7 @@ const { Client } = require("discord.js");
const { Message } = require("../../Bridge/class/Message"); const { Message } = require("../../Bridge/class/Message");
class DiscordClient extends Client { class DiscordClient extends Client {
constructor(bridge, verbose = false, channels) { constructor(bridge, verbose = false, channels, appId) {
super({ super({
intents: [ intents: [
"Guilds", "Guilds",
@ -14,6 +14,7 @@ class DiscordClient extends Client {
], ],
}); });
this.appId = appId;
this.bridge = bridge; this.bridge = bridge;
this.verbose = verbose; this.verbose = verbose;
this.trackedChannelsIds = channels; this.trackedChannelsIds = channels;
@ -40,6 +41,7 @@ class DiscordClient extends Client {
} }
async broadcast(message) { async broadcast(message) {
this.log("broadcasting to discord");
const msg = await this.formatMessageForForward(message); const msg = await this.formatMessageForForward(message);
const mentioned_users = this.getMentionedUsers(message.text); const mentioned_users = this.getMentionedUsers(message.text);
@ -203,13 +205,52 @@ class DiscordClient extends Client {
async refreshMembersCache() { async refreshMembersCache() {
for (const [channel, guild] of this.channelsGuilds) { for (const [channel, guild] of this.channelsGuilds) {
guild.members.fetch().then((members) => { guild.members
this.log("members cache loaded for ", guild.name); .fetch()
.then((members) => {
this.log("members cache loaded for ", guild.name);
})
.catch((err) => this.log(err));
}
}
async syncCommands() {
const url = `https://discord.com/api/v10/applications/${this.appId}/guilds/1335942797726846986/commands`;
// Command JSON payload for the "High Five" command (type 2 = USER command)
const command = {
name: "trump",
type: 1,
description: "uwu user documentation",
};
const headers = {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
"Content-Type": "application/json",
};
try {
const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(command),
}); });
if (!response.ok) {
throw new Error(
`Error registering command: ${response.statusText}`,
);
}
const data = await response.json();
console.log("Command registered successfully:", data);
} catch (error) {
console.error("Error:", error);
} }
} }
start() { start() {
this.syncCommands();
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
await this.login(process.env.DISCORD_TOKEN); await this.login(process.env.DISCORD_TOKEN);

View file

@ -9,13 +9,13 @@ This is a Bridge made to tie Discord Channels and IRC Channels. It works by usin
build l'image: build l'image:
```bash ```bash
docker build -t urbridge:latest . podman build -t urbridge:latest .
``` ```
run un container temporaire (pour l'instant le bot ne sauve aucune data, donc il n'y a pas d'inconvénient à le recréer à chaque démarrage et delete quand il est arêté): run un container temporaire (pour l'instant le bot ne sauve aucune data, donc il n'y a pas d'inconvénient à le recréer à chaque démarrage et delete quand il est arêté):
```bash ```bash
docker run --name urlab_viaduk --rm --env-file .env urbridge:latest podman run --name urlab_viaduk --rm --env-file .env urbridge:latest
``` ```
il faut un fichier .env spécifiant les variables suivantes: il faut un fichier .env spécifiant les variables suivantes:

View file

@ -1,5 +1,6 @@
{ {
"verbose": true, "verbose": true,
"discordChannels": ["1161739009614151751", "727212463988146206"], "discordChannels": ["1335942797726846989", "1335962777063264326"],
"ircChannels": ["#urlab"] "ircChannels": ["#vincentdev"],
"discordAppId": "866317638337953792"
} }