【问题标题】:Why is my Discord command unresponsive, although online and no detected errors?为什么我的 Discord 命令没有响应,虽然在线并且没有检测到错误?
【发布时间】:2021-01-31 14:01:30
【问题描述】:

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

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

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

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

这里是 index.js 代码(幸好只有 46 行),第二个 sn-p 代码是 test.js 文件,接近末尾的 if 语句是预期的。 p>

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");
    }
}

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

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

【问题讨论】:

    标签: javascript node.js discord.js bots


    【解决方案1】:

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

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

    应该是

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

    【讨论】:

    • 你太棒了。我爱你。 +1
    猜你喜欢
    • 2021-03-13
    • 2022-10-24
    • 2022-01-04
    • 2021-04-08
    • 2022-12-15
    • 2021-03-11
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多