【发布时间】:2021-05-02 21:18:23
【问题描述】:
[更新] 我现在选择使用数据库并为每个命令创建一个新集合,然后它将收集所有使用该命令的用户。
有谁知道如何在我的 Json 文件中获取多个数组,我在不和谐中有 3 个斜杠命令,我试图从中收集数据,但是当我使用命令时,它只会更新 Json 文件中的当前数组,我想获得每个命令的所有用途。这样我就可以将 Json 文件导入到 Mongodb。
我是 JavaScript 新手并使用 Mongodb,所以我还在学习,所以我非常感谢我能在下面获得的所有帮助。我将提供我的代码示例,以便您查看我是否做错了什么。
我想要达到的目标示例
{
"id": "281185483717869569",
"command": "architect",
"name": "UKzs",
"description": [
{
"value": "569",
"name": "x"
},
{
"value": "663",
"name": "y"
},
{
"id": "281185483717869569",
"command": "duke",
"name": "UKzs",
"description": [
{
"value": "123",
"name": "x"
},
{
"value": "123",
"name": "y"
}
]
}
]
}
JavaScipt 文件
Client.ws.on("INTERACTION_CREATE", async (interaction, msg) => {
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
console.log(args);
const title = {
id: interaction.member.user.id,
command: command,
name: interaction.member.user.username,
description: args,
};
fs.writeFile("title.json", JSON.stringify(title, null, 2), (err) => {
if (err) {
console.log(err);
} else {
console.log("File has been written");
}
});
if (command == "duke") {
const description = args.map((opt) => {
return opt.value;
});
const embed = new Discord.MessageEmbed()
.setTitle(`Would like the duke title!`)
.setColor("RANDOM")
.setDescription(`These are my Coordinates \n ${description}`)
.setTimestamp()
.setAuthor(interaction.member.user.username);
Client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: await createAPIMessage(interaction, embed),
},
});
}
if (command == "architect") {
const description = args.map((opt) => {
return opt.value;
});
const embed = new Discord.MessageEmbed()
.setTitle(`Would like the architect title!`)
.setColor("RANDOM")
.setDescription(`These are my Coordinates \n ${description}`)
.setTimestamp()
.setAuthor(interaction.member.user.username);
Client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: await createAPIMessage(interaction, embed),
},
});
}
if (command == "scientist") {
const description = args.map((opt) => {
return opt.value;
});
const embed = new Discord.MessageEmbed()
.setTitle(`Would like the scientist title!`)
.setColor("RANDOM")
.setDescription(`These are my Coordinates \n ${description}`)
.setTimestamp()
.setAuthor(interaction.member.user.username);
Client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: await createAPIMessage(interaction, embed),
},
});
}
});
Json 文件
[
{
"id": "281185483717869569",
"command": "architect",
"name": "UKzs",
"description": [
{
"value": "123",
"name": "x"
},
{
"value": "123",
"name": "y"
}
]
}
]
如您所见,该命令只有一个输入,如果我使用 /duke 之类的另一个命令然后在 discord 上输入我的数据,那么它只会更新上面的 Json 文件。
如果有人能指出我如何将其保存到数据库的正确方向,那就更好了,我将不胜感激,我不是 100% 确定该怎么做,但我认为这与模式有关.
【问题讨论】:
标签: javascript json discord