Fix Discord bot connection and channel messaging#1
Fix Discord bot connection and channel messaging#1railway-app[bot] wants to merge 1 commit intomasterfrom
Conversation
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates the sendToChannel function to utilize the channel cache and includes a check to ensure the channel is text-based. It also corrects the client event listener name from clientReady to ready. A review comment suggests using client.channels.fetch instead of client.channels.cache.get to prevent failures when the channel is not yet present in the local cache.
| await client.api.channels[channelId].messages.post({ | ||
| body: { content: message }, | ||
| }); | ||
| const channel = client.channels.cache.get(channelId); |
There was a problem hiding this comment.
Using client.channels.cache.get(channelId) only retrieves channels already stored in the local memory cache. If the bot has just restarted or hasn't interacted with the channel recently, this will return undefined, causing the message to fail. It is safer to use client.channels.fetch(channelId) to ensure the channel is retrieved from the Discord API if it's not in the cache.
const channel = await client.channels.fetch(channelId);
Problem
The bot container starts but never connects to Discord due to two bugs in
src/main.ts. First,client.on("clientReady", ...)uses a non-existent event name — discord.js v14 emits"ready", so the handler never fires, the bot never logs its online status, and the daily verse scheduler never starts. Second,sendToChannelcallsclient.api.channels[channelId].messages.post(), which is not a valid discord.js v14 API and throws at runtime whenever a message send is attempted.Solution
Changed the event name from
"clientReady"to"ready"so the ready handler fires correctly on login. Replaced the invalidclient.apicall insendToChannelwith the proper discord.js v14 pattern:client.channels.cache.get(channelId)followed by anisTextBased()guard andchannel.send(message). Both changes align with the discord.js v14 public API.Changes
src/main.tsGenerated by Railway