首页 > 解决方案 > 为什么我的 Discord 命令没有响应,虽然在线并且没有检测到错误?

问题描述

我是 JavaScript 的初学者,但我之前用 C# 编写过一个项目。对不起,如果我犯了菜鸟的错误!

我最近开始为我碰巧主持的服务器编写一个 Discord 机器人,并且我创建了一个频道,它在垃圾邮件聊天中标记用户的垃圾邮件。只有一个小问题,我无法终生解决。我做的命令不起作用,我不确定代码哪里出错了。VS Code 也没有检测到任何错误。代码本地托管在我的笔记本电脑上。

我一直在关注 CodeLyon 的教程,他们的代码运行良好。我只是不确定为什么我的代码不能工作,尽管它遵循类似的格式和通用代码。

我没有通过搜索查询进行研究。多种解决方案都没有奏效,包括我console.log在终端中放置一行的解决方案,表明机器人检测到我发送的消息中的前缀。该解决方案不起作用,所以我迷路了。我的所有代码看起来都可以运行,而且很明显,该机器人是在线的,因为它能够在某个频道中发送垃圾邮件。是否有更新改变了 discord.js 检测消息的方式?

这是 index.js 代码(幸好只有 46 行),第二段代码是 test.js 文件,if接近结尾的语句所期望的。

const Discord = require('discord.js');
const client = new Discord.Client();
const commandPrefix = 'j!';
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
 
// (client.login token would go here, but removed for censorship's sake)
client.on('ready', () => {
    console.log('"jaka-bot!!~" is now online!');
 
    var spamChannel = client.channels.cache.get('(are channel tags meant to be censored?)');
    setInterval(() => {
        spamChannel.send("||(user id removed for censorship)|| Hi there! I'm **jaka-bot**, a bot made by jakanz! ( Or as you may know them as *'jaka-chan'* or simply *'jaka.'* )\n\n**Make sure to DM jaka-chan if these messages ever stop!** It means that his code must've stopped due to lost internet connection or his laptop crapped out.");
    }, 1)
})
 
client.commands = new Discord.Collection();
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}
 
// bot cmds
client.on('message', message => {
    // if the message content does not start with the prefix or the message author is the bot, ignore
    if(!message.content.startsWith(commandPrefix) || message.author.bot) return;
 
    // variables for cmds
    const args = message.content.slice(commandPrefix.Length).split("/ +/");
    const command = args.shift().toLowerCase();
 
    // socials
    if(command == "test"){
        client.commands.get("test").execute(message, args);
    }
})
module.exports = {
    name: 'test',
    description: "test",
    execute(message, args){
        message.channel.send("test test test");
    }
}

(我提供了所有我能提供的代码,因为我不想给出随机的代码块,即使问题出在另一个问题上。)

所有代码看起来都可以接受,但我不确定代码在哪里丢失,这会使机器人对我的命令没有响应!如果您能提供帮助,将不胜感激。祝你今天过得愉快!

标签: javascriptnode.jsdiscord.jsbots

解决方案


要获取字符串的长度,请使用.length而不是.Length.

const args = message.content.slice(commandPrefix.Length).split("/ +/");

应该

const args = message.content.slice(commandPrefix.length).split("/ +/");

推荐阅读