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");
class Bridge {
constructor(config) {
const verbose = config.verbose ?? false;
constructor(config) {
const verbose = config.verbose ?? false;
const discordChannels = config.discordChannels;
const ircChannels = config.ircChannels.map((chan) => chan.toLowerCase());
const discordChannels = config.discordChannels;
const ircChannels = config.ircChannels.map((chan) =>
chan.toLowerCase(),
);
if (!discordChannels || !ircChannels) {
this.log("Missing channels");
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();
if (!discordChannels || !ircChannels) {
this.log("Missing channels");
throw new Error("Missing discordChannels or ircChannels");
}
resolve();
} catch (error) {
reject(error);
}
});
}
async broadcast(msg) {
this.log("Broadcasting", msg);
for (const client of this.clients) {
client.broadcast(msg);
this.clients = [
new DiscordClient(
this,
verbose,
discordChannels,
config.discordAppId,
),
new IRCClient(this, verbose, ircChannels),
];
}
}
log(...args) {
return console.log("[BRIDGE]", ...args);
}
start() {
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;

View file

@ -2,7 +2,7 @@ const { Client } = require("discord.js");
const { Message } = require("../../Bridge/class/Message");
class DiscordClient extends Client {
constructor(bridge, verbose = false, channels) {
constructor(bridge, verbose = false, channels, appId) {
super({
intents: [
"Guilds",
@ -14,6 +14,7 @@ class DiscordClient extends Client {
],
});
this.appId = appId;
this.bridge = bridge;
this.verbose = verbose;
this.trackedChannelsIds = channels;
@ -40,6 +41,7 @@ class DiscordClient extends Client {
}
async broadcast(message) {
this.log("broadcasting to discord");
const msg = await this.formatMessageForForward(message);
const mentioned_users = this.getMentionedUsers(message.text);
@ -203,13 +205,52 @@ class DiscordClient extends Client {
async refreshMembersCache() {
for (const [channel, guild] of this.channelsGuilds) {
guild.members.fetch().then((members) => {
this.log("members cache loaded for ", guild.name);
guild.members
.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() {
this.syncCommands();
return new Promise(async (resolve, reject) => {
try {
await this.login(process.env.DISCORD_TOKEN);

View file

@ -9,16 +9,16 @@ This is a Bridge made to tie Discord Channels and IRC Channels. It works by usin
build l'image:
```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é):
```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:
- DISCORD_TOKEN,
- IRC_HOST, l'adresse du serveur irc

View file

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