我正在制作的该机器人的任务是在用户收到角色以及用户删除角色时进行日志记录。它将打印到所需的频道。另外,我希望机器人在用户获得特定角色时将其发送到数据库的数据。这部分正在工作。当机器人是一个添加或删除角色时,我希望代码忽略数据库的添加/删除。这部分工作不正常。我设置它的方式是检查审核日志,以查看该操作的执行者是谁。预期的效果是,如果执行者的用户名与我的机器人的用户名匹配,则代码不会运行。但是,即使自动程序添加或删除了用户的角色,并且审核日志在视觉上将自动程序显示为已完成操作的自动程序,将条目记录到控制台时,它也会显示我自己而不是自动程序作为执行者。
client.on("guildMemberUpdate", async function(oldMember, newMember){ //When a user has their roles changed (added or removed)
const entry = await oldMember.guild.fetchAuditLogs({type: 'MEMBER_UPDATE'}).then(audit => audit.entries.first()) //Find audit logs pertaining to member update
console.log(entry) //Print the data to the console
if (entry.executor.username == "PolarBot") { //If the executor of the role addition is the bot then do nothing
return;
}
else { //Otherwise execute the code
if (oldMember.author == client.user) { // Prevent bot from responding to its own messages
return
}
if (oldMember.roles.size < newMember.roles.size) { //If the old member has less roles than the new member
currency.add(oldMember.user.id, 1); //Establish user in the database
const target = oldMember.user.id //Establish target ID for the database
for (const role of newMember.roles.map(x => x.id)) { //Find the new role
const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: `${oldMember.guild.roles.get(role).name}` } } }); //Set item to be the role added
const user = await Users.findOne({ where: { user_id: target } }); //Find the target location in the database and name it user
const items = await user.getItems(); //Ignore this
user.addItem(item); //Add role to the database for that uesr
if (!oldMember.roles.has(role)) { //Check to see if the old member does not have the role
oldMember.guild.channels.find(channel => channel.name === "change-logging").send({embed: { //Send log message to the correct channel
color: 16723502,
title: "**User Roles Updated:**",
description: oldMember.user.tag,
fields: [
{
name: "**New Role(s)**",
value: `${oldMember.guild.roles.get(role).name}`,
inline: true
}
],
timestamp: new Date(),
}})
}
}
}
if (oldMember.roles.size > newMember.roles.size) {
currency.add(oldMember.user.id, 1);
const target = oldMember.user.id
for (const role of oldMember.roles.map(x => x.id)) {
const item = await CurrencyShop.findOne({ where: { name: { [Op.like]: `${oldMember.guild.roles.get(role).name}` } } });
const user = await Users.findOne({ where: { user_id: target } });
const items = await user.getItems();
user.removeItem(item); //Subtract role from the data base
if (!newMember.roles.has(role)) {
oldMember.guild.channels.find(channel => channel.name === "change-logging").send({embed: {
color: 16723502,
title: "**User Roles Updated:**",
description: oldMember.user.tag,
fields: [
{
name: "**Removed Role(s)**",
value: `${oldMember.guild.roles.get(role).name}`,
inline: true
}
],
timestamp: new Date(),
}})
}
}
}
}
});
预期结果:将“条目”登录到控制台时,执行者部分应读取我的机器人的用户名。 实际结果:我被列为执行人。
链接到控制台输出:
答案 0 :(得分:0)
结果我有点傻,我想致电的实际审核日志事件是MEMBER_ROLE_UPDATE而不是MEMBER_UPDATE