Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ const MIN_ACCOUNT_AGE = 2 * 24 * 60 * 60 * 1000;
*/
async function sendToChannel(channelId: string, message: string): Promise<void> {
try {
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);

if (channel && channel.isTextBased()) {
await channel.send(message);
} else {
console.error(`Channel ${channelId} not found or is not a text-based channel`);
}
} catch (error) {
console.error(`Failed to send to channel ${channelId}:`, error);
throw error;
Expand All @@ -113,7 +116,7 @@ console.log(` Default Version: ${DEFAULT_VERSION}`);
console.log(` Daily Verse Schedule: ${DAILY_VERSE_SCHEDULE} (${TIMEZONE})`);

// Event: Client Ready
client.on("clientReady", () => {
client.on("ready", () => {
console.log(`✅ Logged in as ${client.user?.username}#${client.user?.discriminator}`);
console.log(` ID: ${client.user?.id}`);
console.log(` Servers: ${client.guilds.cache.size}`);
Expand Down