【问题标题】:error message in discord bot? (discord.js)不和谐机器人中的错误消息? (discord.js)
【发布时间】:2021-07-13 04:18:40
【问题描述】:

我正在使事件和命令处理程序看起来不错,但是当我运行 ,hello 命令时出现错误,这是错误:

if(!message.content.startsWith(Prefix) || message.author.bot) 返回; ^

TypeError: 无法读取未定义的属性“startsWith”

代码如下:

module.exports = (Discord, Client, message) => {
    const Prefix = ',';
    if(!message.content.startsWith(Prefix) || message.author.bot) return;

    const args = message.content.slice(Prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const Command = Client.commands.get(cmd);

    if(Command) Command.execute(Client, message, args, Discord, Random, RadnomPuppy);
}

我没有发现任何问题

【问题讨论】:

  • ,hello 命令是我目前唯一的命令

标签: javascript discord discord.js


【解决方案1】:

你的函数有很多参数,所以消息是未定义的。 应该是这样的:

client.on("message", (msg)=>{
    const Prefix = ',';
    if(!msg.content.startsWith(Prefix) || msg.author.bot) return;

    const args = msg.content.slice(Prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const Command = Client.commands.get(cmd); // Have the commands part in file scope, to access it here.

    if(Command) Command.execute(Client, msg, args, Discord, Random, RadnomPuppy);

})

【讨论】:

  • 所以我应该用消息替换每个味精
  • 这不起作用我得到一个新的错误,说客户端没有在 Client.on("message", (message)=>{
【解决方案2】:
module.exports = (Discord, Client, message) => {
    const Prefix = ',';
    if(!message.content.startsWith(Prefix) || message.author.bot) return;

    const args = message.content.slice(Prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const Command = Client.commands.get(cmd);

   if(Command) Command.execute(Client, message, args, Discord);
}

我必须从if(Command) Command.execute(Client, message, args, Discord); 行中删除随机和随机小狗

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 2021-08-04
    • 2018-11-19
    • 2021-07-30
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多