<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vibebot.gg</title>
    <description>The latest articles on DEV Community by Vibebot.gg (@vibebot).</description>
    <link>https://dev.to/vibebot</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F13408%2Fe14e2ea9-39be-4ece-b554-83b586ffa296.png</url>
      <title>DEV Community: Vibebot.gg</title>
      <link>https://dev.to/vibebot</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vibebot"/>
    <language>en</language>
    <item>
      <title>The Discord.js gotchas that cost me a week each (so they don't have to cost you one)</title>
      <dc:creator>FADI MAMAR</dc:creator>
      <pubDate>Thu, 21 May 2026 02:15:25 +0000</pubDate>
      <link>https://dev.to/vibebot/the-discordjs-gotchas-that-cost-me-a-week-each-so-they-dont-have-to-cost-you-one-2ic7</link>
      <guid>https://dev.to/vibebot/the-discordjs-gotchas-that-cost-me-a-week-each-so-they-dont-have-to-cost-you-one-2ic7</guid>
      <description>&lt;p&gt;I run an &lt;a href="https://vibebot.gg" rel="noopener noreferrer"&gt;AI Discord bot builder&lt;/a&gt; that's deployed 500+ live bots over the last year. The service takes a plain-English description, generates discord.js code, and ships the container to production in under 30 seconds.&lt;/p&gt;

&lt;p&gt;That sounds clean. It is not clean.&lt;/p&gt;

&lt;p&gt;Most of what I learned came from logs at 3am after someone's bot stopped responding to messages, or a community went silent because of an undocumented Discord behavior. Here's the list I wish I'd had on day one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Missing Permissions (50013) is usually about role position, not permissions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first time I hit this I spent two hours auditing the bot's permission integer. It was correct. The bot still couldn't kick anyone.&lt;/p&gt;

&lt;p&gt;The thing nobody puts in big letters: discord.js permissions are gated by role hierarchy. A bot can have the KickMembers permission and still fail to kick anyone whose top role sits above the bot's top role.&lt;/p&gt;

&lt;p&gt;client.on('interactionCreate', async (interaction) =&amp;gt; {&lt;br&gt;
if (!interaction.isChatInputCommand()) return;&lt;br&gt;
if (interaction.commandName !== 'kick') return;&lt;/p&gt;

&lt;p&gt;const target = interaction.options.getMember('user');&lt;br&gt;
const botMember = interaction.guild.members.me;&lt;/p&gt;

&lt;p&gt;if (target.roles.highest.position &amp;gt;= botMember.roles.highest.position) {&lt;br&gt;
      return interaction.reply({&lt;br&gt;
        content: "I can't kick that user — their role is at or above mine.",&lt;br&gt;
        ephemeral: true,&lt;br&gt;
      });&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await target.kick(interaction.options.getString('reason') ?? 'No reason');
await interaction.reply(`Kicked ${target.user.tag}.`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;Fix in Discord: drag the bot's role above the roles it needs to manage in Server Settings → Roles. This is the single most common reason a "working" bot doesn't work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unknown Interaction (10062) is a 3-second race&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Slash commands give you exactly 3 seconds to respond. If you call any awaited DB query, third-party API, or LLM before.reply(), you'll lose that race.&lt;/p&gt;

&lt;p&gt;Wrong:&lt;br&gt;
  const result = await callLLM(prompt);   // 2.4s&lt;br&gt;
  await interaction.reply(result);        // 10062&lt;/p&gt;

&lt;p&gt;Right:&lt;br&gt;
  await interaction.deferReply();         // buys you 15 minutes&lt;br&gt;
  const result = await callLLM(prompt);&lt;br&gt;
  await interaction.editReply(result);&lt;/p&gt;

&lt;p&gt;The deferred reply pattern is one of those things you need to internalize once and then never get bit by again.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Intent flags are silent killers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This one is brutal because there's no error. Your bot connects, shows as online, registers commands, and never reads a message.&lt;/p&gt;

&lt;p&gt;Two parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the Discord Developer Portal, toggle "Message Content Intent" on.&lt;/li&gt;
&lt;li&gt;In your client config, list it:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;import { Client, GatewayIntentBits } from 'discord.js';&lt;/p&gt;

&lt;p&gt;const client = new Client({&lt;br&gt;
    intents: [&lt;br&gt;
      GatewayIntentBits.Guilds,&lt;br&gt;
      GatewayIntentBits.GuildMessages,&lt;br&gt;
      GatewayIntentBits.MessageContent,   // ← without this, message.content is empty&lt;br&gt;
      GatewayIntentBits.GuildMembers,&lt;br&gt;
    ],&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;If you skip either step, message.content arrives as an empty string and your prefix commands look broken for no apparent&lt;br&gt;
   reason.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reactions and edits on old messages are partial&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If someone reacts to a message your bot didn't observe being created (anything older than the bot's session), the event payload is partial. Touching .content returns null until you fetch.&lt;/p&gt;

&lt;p&gt;client.on('messageReactionAdd', async (reaction, user) =&amp;gt; {&lt;br&gt;
    if (reaction.partial) {&lt;br&gt;
      try { await reaction.fetch(); } catch { return; }&lt;br&gt;
    }&lt;br&gt;
    if (reaction.message.partial) {&lt;br&gt;
      try { await reaction.message.fetch(); } catch { return; }&lt;br&gt;
    }&lt;br&gt;
    // safe to use reaction.message.content now&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;Add the Partials config too:&lt;/p&gt;

&lt;p&gt;import { Client, Partials } from 'discord.js';&lt;br&gt;
  new Client({&lt;br&gt;
    intents: [...],&lt;br&gt;
    partials: [Partials.Message, Partials.Channel, Partials.Reaction],&lt;br&gt;
  });&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ephemeral replies don't behave like normal messages&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can't fetch them with channel.messages.fetch(). You can't add reactions. If you want to update one later, you need the original interaction token, not a message ID:&lt;/p&gt;

&lt;p&gt;await interaction.reply({ content: 'Loading…', ephemeral: true });&lt;br&gt;
  // 10 seconds later&lt;br&gt;
  await interaction.editReply({ content: 'Done.' });&lt;/p&gt;

&lt;p&gt;If you serialize the interaction across processes (queue, webhook handler, etc.), serialize the token, not the message&lt;br&gt;
reference.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Process crashes leave gateway sessions hanging&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If your bot crashes hard without closing the websocket, Discord keeps that session alive on their end for a few minutes.&lt;br&gt;
New deploys then count against the 1000/day session start limit, and on cold restarts you'll see a brief window where two instances of your bot post duplicate messages.&lt;/p&gt;

&lt;p&gt;async function shutdown(signal) {&lt;br&gt;
    console.log(&lt;code&gt;[bot] ${signal} received, closing gateway&lt;/code&gt;);&lt;br&gt;
    try { await client.destroy(); } catch {}&lt;br&gt;
    process.exit(0);&lt;br&gt;
  }&lt;br&gt;
  process.on('SIGTERM', () =&amp;gt; shutdown('SIGTERM'));&lt;br&gt;
  process.on('SIGINT',  () =&amp;gt; shutdown('SIGINT'));&lt;/p&gt;

&lt;p&gt;process.on('unhandledRejection', (err) =&amp;gt; {&lt;br&gt;
    console.error('[bot] unhandled rejection:', err);&lt;br&gt;
    // don't crash — log and continue&lt;br&gt;
  });&lt;br&gt;
  process.on('uncaughtException', (err) =&amp;gt; {&lt;br&gt;
    console.error('[bot] uncaught:', err);&lt;br&gt;
    // do crash, but cleanly&lt;br&gt;
    shutdown('uncaughtException');&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;If you're running on a container platform that sends SIGTERM before forcibly killing, this single block saves you a&lt;br&gt;
  class of bugs you'd never spot.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rate limits aren't always 429s&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The REST client emits a rateLimited event for the soft cases (Discord's per-route bucket warnings). If you log it from&lt;br&gt;
  day one, you get a free monitoring signal:&lt;/p&gt;

&lt;p&gt;client.rest.on('rateLimited', (info) =&amp;gt; {&lt;br&gt;
    console.warn('[ratelimit]', {&lt;br&gt;
      route: info.route,&lt;br&gt;
      method: info.method,&lt;br&gt;
      timeToReset: info.timeToReset,&lt;br&gt;
      limit: info.limit,&lt;br&gt;
    });&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;In production these almost always trace back to one bot in a 10k-member server doing something dumb in a loop. They Never show up in your error monitoring otherwise.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The biggest one: most production failures aren't bugs in your code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you run a few hundred bots, the failure distribution shifts. Probably 30% of "the bot is broken" tickets I've seen trace back to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server admin moved roles around&lt;/li&gt;
&lt;li&gt;The user revoked the bot's invite&lt;/li&gt;
&lt;li&gt;The bot was rate-limited because someone scripted 1000 reactions&lt;/li&gt;
&lt;li&gt;Discord shipped a behavior change&lt;/li&gt;
&lt;li&gt;A channel was deleted that the bot was scheduled to post in&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your bot needs to log enough that you can prove which of these happened quickly. A single structured log per important event (action attempted, guild ID, user ID, outcome, latency) gets you 80% of the way there.&lt;/p&gt;

&lt;p&gt;I learned most of this the hard way running vibebot.gg, where users describe a Discord bot in English and the system generates and deploys discord.js code for them. The interesting failure mode in that setup is that the LLM-generated code has to handle all eight of these gotchas correctly the first time, because the user never sees the code. The patterns above are exactly what we now bake into every generated bot at the template level.&lt;/p&gt;

&lt;p&gt;If you're building a Discord bot from scratch, copy the snippets above directly. They're worth a week of debugging each.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>discord</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
