Skip to content

Fix Discord bot connection and channel messaging#1

Open
railway-app[bot] wants to merge 1 commit intomasterfrom
railway/code-change-XMfwR6
Open

Fix Discord bot connection and channel messaging#1
railway-app[bot] wants to merge 1 commit intomasterfrom
railway/code-change-XMfwR6

Conversation

@railway-app
Copy link
Copy Markdown

@railway-app railway-app bot commented Apr 1, 2026

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, sendToChannel calls client.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 invalid client.api call in sendToChannel with the proper discord.js v14 pattern: client.channels.cache.get(channelId) followed by an isTextBased() guard and channel.send(message). Both changes align with the discord.js v14 public API.

Changes

  • Modified src/main.ts

Generated by Railway

@euxaristia
Copy link
Copy Markdown
Owner

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/main.ts
await client.api.channels[channelId].messages.post({
body: { content: message },
});
const channel = client.channels.cache.get(channelId);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant