【问题标题】:why aren't my discord bot commands working为什么我的不和谐机器人命令不起作用
【发布时间】:2021-02-25 23:56:53
【问题描述】:

我是代码新手,我只是想制作一个不和谐的机器人,但我发出了命令,我让我的机器人的状态可以说些什么,但没有一个起作用。它上线了,它并没有告诉我我的代码有什么问题,但它都不起作用。

代码:

const Discord = require("discord.js");
const bot = new Discord.Client();

let prefix=".";

bot.on("ready", () => {
  bot.user.setStatus("idle")
console.log("lets gooo")
});

const update = () => {
    bot.user.setActivity(".help | residing on " + bot.guilds.size + " Servers", { type: 'WATCHING' }); 
};

bot.on('ready', update);
bot.on('guildCreate', update);
bot.on('guildRemove', update);
bot.on("message", message => {
    
    if(message.author.bot) return;
    if(message.channel.type === "dm") return;

    let messageArray = message.content.split(" ");
    let command = messageArray[0];
    let args = messageArray.slice(1);

   if(!command.startsWith(prefix)) return;

});

    if(command === `${prefix}userinfo`) {
        let embed = new Discord.RichEmbed()
        .setAuthor(message.author.username)
        .setColor("#5ED315")
        .setThumbnail( `${message.author.avatarURL}`)
        .addField("Name", `${message.author.username}#${message.author.discriminator}`)
        .addField("ID", message.author.id)
       
        message.reply("check dms");
        message.channel.send({embed}); 

    }; 


    if("command" === `${prefix}help`) {
        let embed = new Discord.RichEmbed()
        .addField(".help", "gives you this current information")
        .setTitle("help")
        .setColor("#5ED315")
        .addField(".user", "gives you info about a user(currently being worked on)")
        .addField(".server","gives you info about a server(currently working on it)") 
        .addField("link to support server","https://discord.gg/cRJk74kDvj")
        .addField("invite link for bot","https://discord.com/api/oauth2/authorize?client_id=771489748651868173&permissions=8&scope=bot")
    };

  message.reply("here's a list of commands that i'm able to do")
        message.channel.send({embed});
    

 
    messageArray = message.content.split("");
    let command = messageArray[0];
        if(command === `${prefix}serverinfo`) {
            let embed = new Discord.RichEmbed()
            .setAuthor(message.author.username)
            .setColor("#5ED315")
            .addField("Name", `${message.guild.name}`)
            .addField("Owner", `${message.guild.owner.user}`)
            .addField("Server ID" , message.guild.id)
            .addField("User Count", `${message.guild.members.filter(m => m.presence.status !== 'offline').size} / ${message.guild.memberCount}`)
            .addField("Roles", `${message.guild.roles.size}`);


            message.channel.send({embed});
         };

bot.login("bot token");

为什么没有任何工作?我真的需要帮助

【问题讨论】:

  • 看到您关闭message 侦听器的方式太快了,您应该在机器人连接之前出现command is not defined 之类的错误。

标签: javascript discord bots


【解决方案1】:
26:
27:     if(!message.startsWith(prefix)) return;
28: 
29: });
    ^^^

你的问题。您太早地关闭了消息处理程序,因此不必费心触摸命令代码。 (嗯,确实如此,但不是您想要的方式。)

您应该将关闭的}); 移到您的命令定义之后。 (在bot.login 之前)

【讨论】:

  • 我是否必须摆脱每次关闭并将其添加到 bot.login 上方?
  • @greened,抱歉回复晚了。删除第 29 行突出显示的右大括号并将它们移动到 bot.login 之前的行。
猜你喜欢
  • 2021-07-26
  • 2021-10-05
  • 2023-03-20
  • 2018-11-10
  • 1970-01-01
  • 2020-09-25
  • 2018-07-21
  • 2021-08-11
  • 2017-10-18
相关资源
最近更新 更多