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

Discord.js中无法结合Node.js Readline使用的技术问题

解决Discord机器人与Readline输入事件不触发的问题

看起来你遇到的核心问题是Readline的question事件完全没执行,而Discord机器人的事件正常触发。我帮你梳理下代码里的几个关键问题,修复后就能同时使用两者了:

1. 语法错误导致Readline无法正常初始化

你的rl.question调用存在字符串未闭合的语法问题,这会直接导致代码解析失败,Readline根本无法正常工作。比如第一行:

rl.question(Chalk.yellow("Enter guild representative's ID: ", (answer) => {

这里Chalk.yellow()的括号里的字符串没有闭合,正确写法应该是:

rl.question(Chalk.yellow("Enter guild representative's ID: "), (answer) => {

后面两个rl.question都存在同样的问题,必须把每个Chalk.yellow/magenta/blueBright的字符串括号补全,再传入回调函数。

2. 异步流程问题:Readline的question是串行异步操作

你连续调用三次rl.question,但question是异步的,这样会导致三个输入提示同时弹出,逻辑混乱。正确的做法是嵌套调用或者用Promise封装,保证输入依次进行:

比如嵌套写法:

rl.question(Chalk.yellow("Enter guild representative's ID: "), (answer) => {
  const rep = answer;
  console.log(Chalk.green("ID Recorded: " + rep));

  rl.question(Chalk.magenta("Enter embed color: "), (answer) => {
    const color = answer;
    console.log(Chalk.green("Color recorded: " + color));

    rl.question(Chalk.blueBright("Enter guild description: "), (answer) => {
      const guildDesc = answer;
      console.log(Chalk.green("Guild description recorded: " + guildDesc));
      // 这里可以启动Discord机器人,确保输入完成后再登录
      bot.login(Token);
    });
  });
});

这样用户完成所有输入后,再启动机器人,避免事件循环的冲突。

3. 其他需要修复的代码问题

  • 未声明变量:rep、color这些变量没有用let/const声明,会变成全局变量,建议加上声明。
  • 逻辑错误:第三个rl.question里把answer赋值给color,覆盖了之前的颜色值,应该改成新的变量比如guildDesc。
  • Discord.js API过时:message.guild.channels.find()在新版discord.js中已经被废弃,应该使用message.guild.channels.cache.find()。
  • 未定义的embed变量:你在发送消息时用到了embed,但代码里没有定义这个变量,需要补充创建Embed的逻辑,比如:
    const embed = new Discord.MessageEmbed()
      .setColor(color)
      .setDescription(guildDesc)
      .setFooter(`Representative ID: ${rep}`);
    
  • 多余的注释符号:代码里有个多余的*/,需要删除。

修复后的完整示例片段

'use strict';
const Discord = require('discord.js');
const Chalk = require('chalk');
const Readline = require('readline');
const Token = '你的机器人Token'; // 这里要填入实际的Token
const Script = require('./data.js');

const rl = Readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const bot = new Discord.Client();

bot.on("ready", () => {
  console.log("Bot is ready!");
});

bot.on("message", (message) => {
  if (message.content === ":addPartner") {
    const embed = new Discord.MessageEmbed()
      .setColor(color)
      .setDescription(guildDesc)
      .setFooter(`Representative ID: ${rep}`);
    const affiliateChannel = message.guild.channels.cache.find(channel => channel.name === "affiliates");
    if (affiliateChannel) {
      affiliateChannel.send({ embed });
      message.channel.send(":white_check_mark: Affiliate message sent in #affiliates!");
    } else {
      message.channel.send(":x: Couldn't find #affiliates channel!");
    }
  }
});

// 串行处理Readline输入,完成后启动机器人
rl.question(Chalk.yellow("Enter guild representative's ID: "), (answer) => {
  const rep = answer;
  console.log(Chalk.green("ID Recorded: " + rep));

  rl.question(Chalk.magenta("Enter embed color: "), (answer) => {
    const color = answer;
    console.log(Chalk.green("Color recorded: " + color));

    rl.question(Chalk.blueBright("Enter guild description: "), (answer) => {
      const guildDesc = answer;
      console.log(Chalk.green("Guild description recorded: " + guildDesc));
      rl.close(); // 完成输入后关闭Readline接口
      bot.login(Token); // 登录机器人
    });
  });
});

为什么之前Readline事件不执行?

最直接的原因是语法错误导致Readline的代码根本没有被正确解析执行,Node.js会抛出语法错误(你可以检查控制台的错误信息),所以question事件根本不会触发。修复语法错误后,再调整异步流程,就能让Readline和Discord机器人正常协同工作了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 03:53:13