javascript - 当我对此进行更改时,我的 Discord.js 机器人没有响应,我还是新人,但我看不出出了什么问题

标签 javascript bots discord

这是代码,

const Discord = require('discord.js')
const Keyv = require('keyv');
const client = new Discord.Client()
const prefixes = new Keyv();
const prefix = '/';
const eris = require('eris')

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`)
})

client.on('message',async message => {
    if (message.author.bot) return;

    let args;
    // handle messages in a guild
    if (message.guild) {
        let prefix;

        // if we found a prefix, setup args; otherwise, this isn't a command
        if (!prefix) return;
        args = message.content.slice(prefix.length).split(/ +/);
    } else {
        // handle DMs
        const slice = message.content.startsWith(prefix) ? prefix.length : 0;
        args = message.content.slice(slice).split(/ +/);
    }

    // get the first space-delimited argument after the prefix as the command
    const command = args.shift().toLowerCase();
    if (command === 'args-info') {
        if (!args.length) {
            return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
        }

        message.channel.send(`Command name: ${command}\nArguments: ${args}`);
    }
  
    if (command === 'ping') {
        return message.channel.send('Pong!')
    }
    if ( command === 'help'){
        return message.channel.send('Sorry! I have no commands yet. You can use /ping to make me reply "Pong!"')
    }
    if(message === '(╯°□°)╯︵ ┻━┻'){
        return message.channel.send('(╯°□°)╯︵ ┻━┻ yo face.')  
    }
    if(message === '┬─┬ ノ( ゜-゜ノ)') return message.reply('thanks for returning the table.')
  
    if(command === 'about') return message.channel.send('Creator: mudkip989, Version: 1.0.0, Name: ' +  client.user.tag)
})

client.login(process.env.TOKEN)

我已经测试过并且机器人在线,并且我使用了有效的更简单的代码,但是当我有了这个时,它就不起作用了。我不明白为什么。我已经做出了更改,也许我需要了解更多“规则”或不和谐机器人代码不起作用的东西。

最佳答案

if (message.guild) {
        let prefix;

        // if we found a prefix, setup args; otherwise, this isn't a command
        if (!prefix) return;

看起来您在这里所做的就是声明一个名为 prefix 的变量,然后检查它的值是否为假。由于 prefix 的值未定义,因此其计算结果始终为 true。

还需要指出的是,您的 let 前缀; 正在使用相同的名称隐藏您的全局常量。

关于javascript - 当我对此进行更改时,我的 Discord.js 机器人没有响应,我还是新人,但我看不出出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58083699/

相关文章:

javascript - webpack4 lodash 大小太大

javascript - Web 上的桌面 IDE 样式布局

node.js - Bot 框架 Directline - 使用 node.js 更改 Bot 名称运行时

javascript - Discord JS 嵌入 react 导致另一个嵌入

python - 如何处理 `MissingRequiredArgument`错误

javascript - Primefaces饼图重叠数据标签

JavaScript 异步返回

azure - 机器人如何发现我的 Azure 子域?

javascript - 我将如何使用 .addField 遍历数组

javascript - 如何在对象构造函数中命名某个对象的 Discord 用户名?