我嘗試使用 discord.js 命令向 mongoodb 提交資料,為什么這段代碼不起作用?
const subregis = "!reg ign: ";
client.on("message", msg => {
if (msg.content.includes(subregis)){
const user = new User({
_id: mongoose.Types.ObjectId(),
userID: msg.author.id,
Nickname: msg.content.text.substr(9)
});
user.save().then(result => console.log(result)).catch(err => console.log(err));
msg.reply("Data already submited")
}
})
uj5u.com熱心網友回復:
我嘗試了不同的方法來找到你的問題,我想我找到了問題。這是我更改后的代碼:
// you could also do rather than .includes, you could do if (message.content === "!reg ign: ") rather than having to make it more complicated
// maybe change substr to substring although they both work also there is no message.content.text so I fixed that
// you could create a const text = args[0]; where it will fetch the users first text displayed example: "!reg ing hello" and I will have a username called hello
client.on("message", msg => {
if (msg.content === "!reg ign: "){
const user = new User({
_id: mongoose.Types.ObjectId(),
userID: msg.author.id,
Nickname: msg.content.substring(msg.content.indexOf(":" 1) // so basically anything after the : will be the username
});
user.save().then(result => console.log(result)).catch(err => console.log(err));
msg.reply("Data has been submitted successfully")
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/489445.html
