You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何让Discord机器人在指定频道自动添加消息反应(无前缀)

Auto-React to Messages in a Specific Discord Channel (No Prefix Needed)

Got it, let's break down how to build this auto-reaction feature for your Discord bot—specifically targeting the Suggestion channel with no prefix required. I'll use discord.js v14 (the current stable version) since it's the most widely used library for this kind of thing.

Step 1: Set Up Your Bot Basics

First, make sure you have a working bot skeleton. If you haven't already:

  • Initialize a Node.js project (npm init -y)
  • Install discord.js (npm install discord.js)
  • Grab your bot token from the Discord Developer Portal
  • Enable the necessary intents in the Developer Portal (Guilds, Guild Messages, Message Content)

Step 2: Core Auto-Reaction Logic

Here's the full code with comments explaining each part. You can copy this into your bot file (e.g., index.js):

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent // Critical for reading message content
  ]
});

// Confirm bot is online
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}! 🚀`);
});

// Listen for new messages
client.on('messageCreate', async (message) => {
  // Skip messages from bots to avoid infinite loops
  if (message.author.bot) return;

  // Option 1: Target channel by name (simple but prone to break if name changes)
  if (message.channel.name.toLowerCase() === 'suggestion') { // Case-insensitive match
    try {
      // Add multiple reactions one after another
      await message.react('👍');
      await message.react('👎');
      await message.react('🤔');
    } catch (error) {
      console.error('Oops, failed to add reactions:', error);
    }
  }

  // Option 2: Target channel by ID (more reliable—recommended!)
  // Replace with your actual Suggestion channel ID
  // const suggestionChannelId = '123456789012345678';
  // if (message.channel.id === suggestionChannelId) {
  //   try {
  //     await message.react('👍');
  //     await message.react('👎');
  //   } catch (error) {
  //     console.error('Failed to react:', error);
  //   }
  // }
});

// Log in with your bot token
client.login('YOUR_BOT_TOKEN_HERE');

Key Notes & Troubleshooting

  • Intents Matter: Don't forget to enable the MessageContent intent in both your code and the Discord Developer Portal (under Bot > Privileged Gateway Intents). Without this, your bot won't detect new messages at all.
  • Permissions: Ensure your bot has the Add Reactions permission in the server, and that it's allowed to interact with the Suggestion channel (double-check channel-specific permissions if reactions aren't showing up).
  • Custom Emojis: If you want to use custom server emojis, use their full format like <:emojiName:123456789> or fetch them via client.emojis.cache.get('emojiId').
  • Avoid Loops: The if (message.author.bot) return line is non-negotiable—it stops your bot from reacting to its own reactions, which would cause an infinite loop.

Quick Tip

Using channel IDs instead of names is always better because channel names can be changed by server admins, breaking your bot's logic. To get a channel ID: enable Developer Mode in Discord (User Settings > Advanced > Developer Mode), then right-click the Suggestion channel and select "Copy ID".

内容的提问来源于stack exchange,提问作者Warline

火山引擎 最新活动