【问题标题】:What am I doing wrong with my discord bot我的不和谐机器人做错了什么
【发布时间】:2021-09-24 21:40:31
【问题描述】:

在教程的帮助下,我正在编写我的第一个不和谐机器人。我被卡住了,因为我的机器人无法响应命令,而且我已经多次检查教程和代码。是不是我做错了什么?

const discord = require('discord.js');

const client = new discord.Client();

const prefix = '!';


client.once('ready' , () => {
  console.log('Zach Is Bad is online');
});


client.on('message', message =>{
  if (!message.content.startswith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

if(command === 'ping'){
    message.channel.send('pong!');
  }
});


client.login('redacted')

【问题讨论】:

  • JavaScript 区分大小写; startswith 应该是 startsWith

标签: discord discord.js


【解决方案1】:

client.on('ready', () => ...) 有错字,但这不应该是错误的原因。

您写了if (!message.content.startswith(prefix) || message.author.bot) return;,但我很确定startwith 必须是startWith(大写字母很重要)。尝试修改它,然后重新启动您的机器人。

如果它不起作用,请将console.log("test OK") 放在 ping 命令之前,重新启动并发送!ping。如果您的机器人日志中有“测试正常”,则问题来自 ping 命令。如果您没有看到此日志,请尝试在 if 语句之前移动 console.log 行。这是一种简单而有效的方法来了解问题出在哪里。

【讨论】:

【解决方案2】:

您的代码的唯一问题是message.content.startswith(prefix)

是startsWith,区分大小写。

if (!message.content.startsWith(prefix) || message.author.bot) return;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2015-06-12
    • 2021-05-21
    • 2017-10-18
    • 2021-07-31
    • 2022-01-09
    • 2017-07-25
    相关资源
    最近更新 更多