You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Discord Bot检测语句中关键词并触发自动回复?

Discord Bot: Auto-Reply to Keywords & Detect "Ping" in Sentences

Hey there! Let's break down how to implement both of your requested features step by step—super easy to tweak once you see the patterns.

1. Auto-Reply When Messages Contain Specific Keywords (e.g., "server IP")

To make your bot trigger a response whenever someone mentions "server IP" (or any phrase), you'll use the messageCreate event and check if the message content includes your target string. Here's a working example using discord.js (v14+):

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.MessageContent // Required to read message content!
  ] 
});

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', (message) => {
  // Ignore messages from other bots to avoid infinite loops
  if (message.author.bot) return;

  // Check if the message contains "server IP" (case-insensitive)
  if (message.content.toLowerCase().includes('server ip')) {
    message.reply('Our server IP is: 192.168.1.100 (replace this with your actual server IP)');
  }
});

client.login('YOUR_BOT_TOKEN');

Key Notes:

  • toLowerCase() ensures the bot matches phrases like "Server IP", "SERVER ip", or "server Ip" too.
  • Don't forget to enable the MessageContent intent in your Discord Developer Portal under your bot's "Privileged Gateway Intents"—without it, the bot can't read message content.

2. Modify "Ping" Response to Detect "Ping" in Any Sentence

If your current bot only responds when someone sends exactly "ping", you just need to swap out the exact match check for a substring or regex match.

Original (Exact Match Only)

// Old code: Only triggers for the exact word "ping"
client.on('messageCreate', (message) => {
  if (message.content === 'ping') {
    message.reply('Pong!');
  }
});

Modified (Detect "Ping" Anywhere in a Sentence)

Option 1: Simple substring check (matches any occurrence, even inside other words like "pingpong"):

client.on('messageCreate', (message) => {
  if (message.author.bot) return;

  if (message.content.toLowerCase().includes('ping')) {
    message.reply('Pong!');
  }
});

Option 2: Regex match (only triggers for the standalone word "ping", not part of longer words):

client.on('messageCreate', (message) => {
  if (message.author.bot) return;

  // The regex /ping/i matches "ping" as a standalone word, case-insensitive
  if (/ping/i.test(message.content)) {
    message.reply('Pong!');
  }
});

Combine Both Features

Here's how to merge both functionalities into one clean script:

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

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', (message) => {
  if (message.author.bot) return;

  // Respond to "server IP" mentions
  if (message.content.toLowerCase().includes('server ip')) {
    message.reply('Our server IP is: 192.168.1.100 (update this with your real IP)');
  }

  // Respond to any message containing "ping" (standalone word)
  if (/ping/i.test(message.content)) {
    message.reply('Pong!');
  }
});

client.login('YOUR_BOT_TOKEN');

Just replace YOUR_BOT_TOKEN with your actual bot token from the Discord Developer Portal, and you're good to go!

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:59:11