【发布时间】:2021-08-18 05:26:28
【问题描述】:
我正在尝试为我的 Discord Bot 创建一个命令,用于将 reaction 添加到链接到 role 的特定 message 中,user 在与指定的表情符号做出反应时应该收到。
语法可能有助于更好地理解它:
!addRoleByReaction <messageID> <emoji you want to add as reaction> <role the user should receive>
示例:
!addRoleByReaction 123456789 ???? 987654321
如果用户的反应是“??????”到
message和ID'123456789' 他将收到role和ID '987654321'
问题:
目前这是我的数据库架构:
_id: reqString,
message: [{
msgId: reqString,
reactionRole: [{
reaction: reqString,
role: reqString,
}],
}],
(我正在使用 mongoDB 和 npm package 'mongoose')
添加第一个reaction 后,我的database document 看起来像这样:
但如果我添加另一个reaction,它看起来像这样:
我真正想要的……
...是推送(如果此guild 和此messageID 的条目已经存在)一个新条目(包含第二个或 第三个, ... reaction & role) 到 reactionRole 数组中。
但是现在,如您所见,它在 message 数组中添加了一个带有第二个 reaction 的新条目。总的来说,它并没有那么糟糕,但它只是丑陋..
差点忘了:这就是我向数据库添加新条目的方式:
// 'targetMessage' contains the message ID
// 'targetReaction' contains the reaction to be added
// 'role' contains the ID of the role, the user should receive
// 'mongo()' simply connects to the database
await mongo().then(async mongoose => {
try {
await messageReactionSchema.findOneAndUpdate({
_id: guild.id,
message: {
$elemMatch: {
msgId: targetMessage,
},
},
}, {
_id: guild.id,
$push: {
message: [{
msgId: targetMessage,
reactionRole: [{
reaction: targetReaction,
role,
}],
}],
},
}, {
upsert: true,
});
}
finally {
mongoose.connection.close();
}
});
【问题讨论】:
标签: javascript node.js mongoose discord.js