【问题标题】:My Discord bot does not respond to my messages我的 Discord 机器人不回复我的消息
【发布时间】:2021-07-01 08:59:51
【问题描述】:

我正在使用 Python 制作一个机器人。我对使用 Python 进行编码非常陌生,而且我不懂很多东西,我只是在学习一个教程(https://youtu.be/j_sD9udZnCk),但我的机器人无法响应我的消息。它按意图在线和离线,但它不响应我的消息。它也是我的 Discord 服务器的管理员。这是我的代码:

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

const client = new Discord.Client();

const prefix = '-';

client.once('ready', () =>{ 
    console.log('Money Farmer 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.sendMessage('pong!');
    }
});


client.login('My token');

【问题讨论】:

  • 这是 NodeJS 而不是 Python
  • 尝试console.log() args 和命令,让我们知道输出是什么
  • 我添加了 console.log(args);什么也没发生,机器人没有回应
  • 机器人正在运行吗?控制台中是否有“Money Farmer 在线!”?如果是,问题出在 messageEvent (client.on('message'....) 之后的第一行
  • “钱农上线了!”控制台中弹出消息,机器人上线。它只是不响应消息“-ping”

标签: javascript node.js discord discord.js bots


【解决方案1】:

尝试使用以下代码解决您的问题:

const args = message.content.split(' ').slice(1);
const command = message.content.split(' ')[0].slice(prefix.length).toLowerCase();

首先split() 消息,然后slice() args 变量的第一个元素。 要获取命令,您拆分 message.content 并获取数组的第一个元素。然后从 Array 中切出前缀并 toLowerCase() 命令。

【讨论】:

  • 你做到了!你这个疯子,你做到了。你真的修好了。我知道这可能是一个简单的解决方法,但我在网上找不到有同样问题的人。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2021-03-06
  • 2022-01-09
  • 2021-11-24
  • 2021-09-19
  • 2019-09-16
  • 2022-11-11
相关资源
最近更新 更多