简体   繁体   English

我的 Discord 机器人没有回复我的消息

[英]My Discord bot does not respond to my messages

I am making a bot using Python.我正在使用 Python 制作一个机器人。 I am very new to coding with Python and I don't understand a lot of things, I am just following a tutorial( https://youtu.be/j_sD9udZnCk ) but I am stuck with my bot not responding to my messages.我对使用 Python 进行编码非常陌生,而且我不懂很多东西,我只是在学习教程( https://youtu.be/j_sD9udZnCk ),但我的机器人没有回复我的消息。 It goes online and offline as intented but it does not respond to my messages.它按照意图在线和离线,但它不响应我的消息。 Also it is an administrator in my Discord server.它也是我的 Discord 服务器的管理员。 This is my code:这是我的代码:

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');

Try using the following code to fix your problem:尝试使用以下代码来解决您的问题:

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

First you split() the the message and then you slice() the first element for your args variable.首先你split()消息,然后你slice()你的 args 变量的第一个元素。 To get the command you split the message.content and take the first element of the Array.要获取命令,您拆分message.content并获取数组的第一个元素。 Then you slice the prefix from the Array and toLowerCase() the command.然后你从 Array 和toLowerCase()命令分割前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM