【发布时间】:2021-11-13 05:15:28
【问题描述】:
我正在努力在 discord.js 的 V12 中检索 user.id 和 user.name 以获取他们也做出反应的消息。 这个想法是一条消息被固定,用户每天在 1-7 之间选择,它会在他们的总数中增加一个点。如果他们取消选择任何反应,它也会从他们的总数中减去。但我还没有写出来。 目前它因 TypeError 失败:尝试获取 user.id 时无法读取 const Character 上未定义的属性 'guild'
config.json
{
"prefix":"!",
"channel": "xxx",
"channelID": "xxx",
}
SQLite
client.getdayCount = DB.prepare("SELECT dayCount from Table WHERE id = ?;");
client.setdayCount = DB.prepare("INSERT OR REPLACE INTO Table (id, dayCount, guild) VALUES (@id, @dayCount @guild);");
主代码
client.on('messageReactionAdd', addRole);
client.on('messageReactionRemove', removeRole);
async function addRole(message, reaction, user) {
if (message.partial) {
try {
await message.fetch();
} catch (err) {
console.error('Error fetching message', err);
return;
}
}
client.channels.cache.get(config.channelID).messages.fetch({
limit: 1
}).then(messages => {
const lastMessage = messages.first()
const character = reaction.message.guild.member(user.id);
//Filter the reaction
const addition = +1;
if (lastMessage.reactions.cache.emojis === "1️⃣" || "2️⃣" || "3️⃣" || "4️⃣" || "5️⃣" || "6️⃣" || "7️⃣") {
// Define the emoji user add
//const character = lastMessage.guild.member(user.id);
//reaction.message.guild.member(user.id);
if (character) //if author has sent before
{
character.setdayCount += addition;
client.setdayCount.run(character);
return;
} else //else add new author to collection
{
character = {
id: user.id,
setdayCount: addition,
guild: reaction.guild.id,
}
}
} else {
};
}).catch(err => {
console.error(err)
})
}
【问题讨论】:
标签: javascript node.js discord discord.js