【问题标题】:Node.js (discord.js) code randomly stops working in the middle of nothingNode.js (discord.js) 代码在空无一人的情况下随机停止工作
【发布时间】:2022-11-20 07:39:07
【问题描述】:

所以目前,我正在 Replit discord.js 中制作一个 discord 机器人。我想做经济系统,代码中间被“截断”了,好像写了“return”,但是没有。

  else if (type === "cashadd") {
    let target = message.mentions.users.first();
    const amount = args.join(" ");
    if (!target) return message.channel.send("Please metion someone!")
______________________ <- here the code is "cutted"
    if (!amount) return message.channel.send("Please specify the amount of money you want to send!")
    if(isNaN(amount)) return message.channel.send("please enter a real number")
    let userBalance = await db.get(`wallet_${target}`)
    await db.set(`wallet_${target}`, userBalance + amount)
    message.channel.send(`You sent ${amount} money to ${target}`)
  }

完整代码:https://pastebin.com/eaStV20P (如果有用的话,我正在使用从 Imagine gaming play 处理的命令)

我试着放一个额外的代码,说“它有效”,我在想它是否这样说。它没有。

【问题讨论】:

  • 大概找不到targetmessage.channel.send调用是否导致消息被发送到频道?

标签: node.js discord discord.js


【解决方案1】:

我会假设这是因为你从未拼接参数const type = args.splice(integer).join(" ");

只需使用 const type = args.join(" "); 即可在每个参数之间添加一个空格。这是一个例子:

let args = ["Testing","Lol","Xd"](这只是 args 的一个示例,在您的情况下,它将是整个命令参数)

使用const type = args.join(" "); 将返回字符串"Testing Lol Xd",如您使用console.log(type) 所见

使用const type = args.splice(integer).join(" ") 将删除提供的整数之前的所有参数,因此如果您使用const type = args.splice(1).join(" ");,这将返回为"Lol Xd",因为0 是字符串中的第一个参数,“Lol”将是第二个参数在字符串中,因此它将是整数 1,依此类推。

所以,如果你想让它按照你想要的方式工作,请尝试使用args.splice(integer).join(" ");

您的代码的一个示例是,假设您的命令是“-eco”,并且“balance”是数组内的所有其他内容, 如果您在频道中输入"-eco balance",args 将类似于;

args = ["-eco", "balance"]; "balance" 字符串是您要查找的 type 常量/变量,

此示例的实际代码:

const type = args.splice(1).join(" ");
if(type.toLowerCase === "balance") {
...
} else if(...) { ...

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2021-07-30
    • 2014-04-11
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多