Node.js (discord.js)代码会随机停止工作



所以目前,我正在制作一个不和谐的机器人在Replit, discord.js。我想做一个经济系统,代码被"删减"了。在中间,好像写了&;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(我使用命令处理从想象游戏发挥,如果它是有用的)

我试着放了一个额外的代码说"它工作",我在想如果它说它。没有。

我认为这是因为您从未拼接参数const type = args.splice(integer).join(" ");

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

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

使用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"one_answers"balance"数组内的所有东西,如果您在通道中输入"-eco balance"

,则args将是类似于; 之类的内容。args = ["-eco", "balance"];"balance"字符串是您正在寻找的type常量/变量,

这个例子的实际代码:

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

最新更新