From 527ba4df2211f8787a4ccee707861dd0959eae37 Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Sat, 4 Dec 2021 00:11:06 -0500 Subject: check if channel is valid on launch --- src/Discord.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Discord.ts b/src/Discord.ts index a938f09..0c96131 100644 --- a/src/Discord.ts +++ b/src/Discord.ts @@ -11,7 +11,7 @@ class Discord { config: Config client: Client - channel: Snowflake + channel: TextChannel | null uuidCache: Map @@ -22,7 +22,7 @@ class Discord { if (onReady) this.client.once('ready', () => onReady()) this.client.on('message', (message: Message) => this.onMessage(message)) - this.channel = config.DISCORD_CHANNEL_ID || '' + this.channel = null this.uuidCache = new Map() } @@ -30,8 +30,16 @@ class Discord { public async init () { try { await this.client.login(this.config.DISCORD_TOKEN) - if (this.config.DISCORD_CHANNEL_NAME && !this.config.DISCORD_CHANNEL_ID) + if (this.config.DISCORD_CHANNEL_NAME && !this.config.DISCORD_CHANNEL_ID) { this.getChannelIdFromName(this.config.DISCORD_CHANNEL_NAME) + } else if (this.config.DISCORD_CHANNEL_ID) { + const channel = this.client.channels.find((ch) => ch.id === this.config.DISCORD_CHANNEL_ID && ch.type === 'text') as TextChannel + if (!channel) { + console.log(`[INFO] Could not find channel with ID ${this.config.DISCORD_CHANNEL_ID}. Please check that the ID is correct and that the bot has access to it.`) + process.exit(1) + } + this.channel = channel + } } catch (e) { console.log('[ERROR] Could not authenticate with Discord: ' + e) if (this.config.DEBUG) console.error(e) @@ -44,7 +52,7 @@ class Discord { // @ts-ignore const channel: TextChannel = this.client.channels.find((c: TextChannel) => c.type === 'text' && c.name === name && !c.deleted) if (channel) { - this.channel = channel.id + this.channel = channel console.log(`[INFO] Found channel #${channel.name} (id: ${channel.id}) in the server "${channel.guild.name}"`) } else { console.log(`[INFO] Could not find channel ${name}! Check that the name is correct or use the ID of the channel instead (DISCORD_CHANNEL_ID)!`) @@ -77,7 +85,7 @@ class Discord { // no channel, done if (!this.channel) return // don't want to check other channels - if (message.channel.id !== this.channel || message.channel.type !== 'text') return + if (message.channel.id !== this.channel.id || message.channel.type !== 'text') return // if using webhooks, ignore this! if (message.webhookID) { // backwards compatability with older config @@ -262,12 +270,11 @@ class Discord { if (this.config.DEBUG) console.log(e) } } else { - // find the channel - const channel = this.client.channels.find((ch) => ch.id === this.config.DISCORD_CHANNEL_ID && ch.type === 'text') as TextChannel - if (channel) { - await channel.send(this.makeDiscordMessage(username, message)) - } else { - console.log(`[ERROR] Could not find channel with ID ${this.config.DISCORD_CHANNEL_ID}!`) + try { + await this.channel!.send(this.makeDiscordMessage(username, message)) + } catch (e) { + console.log('[ERROR] Could not send Discord message through bot!') + process.exit(1) } } } -- cgit 1.4.1 href='/src/commit/home/.config/nvim/colors/trivial.vim?id=3e4a16d4af12b6acfcc8f07367221d6abb56c882&follow=1'>Set nvim Directory back to blueJune McEnroeeb8&follow=1'>Fix adding /usr/games to PATHJune McEnroe 2021-02-09Show battery level while charging, time while dischargingJune McEnroe 2021-02-09Install sctJune McEnroe 2021-02-09Show minutes left instead of battery percentageJune McEnroe 2021-02-09Set antialiasing and unhinting globallyJune McEnroe 2021-02-09Open youtube and twitch with mpvJune McEnroe 2021-02-09Add volume control bindings to cwmJune McEnroe 2021-02-09Use w3m to open other URLsJune McEnroe 2021-02-09Disable mouse mode in xtermJune McEnroe 2021-02-09Add pbcopy, pbpaste, open dispatch scriptsJune McEnroe 2021-02-09Use flags for pbd client behaviourJune McEnroe 2021-02-08Add macOS-like copy and paste to xtermJune McEnroe 2021-02-08Only update clock script every minuteJune McEnroe 2021-02-08Use 4M- for window resizing in cwmJune McEnroe 2021-02-08Tweak trackpad scaling, mouse accelerationJune McEnroe 2021-02-08Use xsel in up and add it do install.shJune McEnroe 2021-02-07Swap root window coloursJune McEnroe 2021-02-07Add -X flag to install X stuff on OpenBSDJune McEnroe 2021-02-07Adjust brightness by smaller incrementsJune McEnroe 2021-02-07Fix cwm window cycling, move big by defaultJune McEnroe 2021-02-07Use class names for Foreground, Background, BorderColorJune McEnroe 2021-02-07Add simple battery status and clock to xsessionJune McEnroe 2021-02-07Set cursor theme and sizeJune McEnroe 2021-02-07Use scrot for up -s if no screencaptureJune McEnroe 2021-02-07Enable mouse acceleration in XJune McEnroe 2021-02-07Set colours for Xt and cwmJune McEnroe 2021-02-07Set urgency on bell in xtermJune McEnroe 2021-02-07Add bindings for brightness controlJune McEnroe 2021-02-07Set X key repeat rateJune McEnroe 2021-02-07Bump font size to 12June McEnroe 2021-02-07Fully configure and rebind cwmJune McEnroe 2021-02-07Add BintiJune McEnroe 2021-02-07Finish configuring xtermJune McEnroe 2021-02-06Enable tapping, reverse scroll, set scaling in wsconsctlJune McEnroe 2021-02-06Set root window to black on purple snowJune McEnroe 2021-02-06Add xmodmap configurationJune McEnroe 2021-02-06Add initial OpenBSD X configurationJune McEnroe 2021-02-06Add xterm output to schemeJune McEnroe