diff --git a/Bridge/class/Bridge.js b/Bridge/class/Bridge.js index b66d598..cba7d21 100644 --- a/Bridge/class/Bridge.js +++ b/Bridge/class/Bridge.js @@ -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; diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index fadb3f4..2b4c83e 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -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); diff --git a/README.md b/README.md index c61beaa..0166070 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.json b/config.json index 1a76350..6183823 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,6 @@ { "verbose": true, - "discordChannels": ["1161739009614151751", "727212463988146206"], - "ircChannels": ["#urlab"] + "discordChannels": ["1335942797726846989", "1335962777063264326"], + "ircChannels": ["#vincentdev"], + "discordAppId": "866317638337953792" }