【问题标题】:Trying to create a warning system for my discord.js bot. What have I done wrong?试图为我的 discord.js 机器人创建一个警告系统。我做错了什么?
【发布时间】:2021-05-16 14:22:27
【问题描述】:

我正在为客户开发一个机器人,我正在制作一个可以由(prefix) warn <user mention> 触发的警告系统。我有下面的代码。

我可以运行一次命令,它会添加警告 1 角色,但后续命令不会添加警告 2 或 3。

if (mentionedUser.roles.cache.has(warn1)){
    if (mentionedUser.roles.cache.has(warn2)){
        message.channel.send('Already has 2 warnings.');
        mentionedUser.addRole(warn3);
    }
    else{
        message.channel.send('Already has 1 warning.')
        mentionedUser.addRole(warn2);
    }
}
else{
    mentionedUser.roles.add(warn1);
    message.channel.send('Warned the user.');
}

【问题讨论】:

  • 看起来“提到的User.roles.cache.has(warn1)”总是假的。你确定这是好的验证?我不知道不和谐,但为什么这个角色会在缓存中?
  • 您可以查看这些示例:stackoverflow.com/questions/45317305/… 如果有帮助
  • @Leyffda discord.js v12 使用缓存。在 v11 中应该是 mentionedUser.roles.has(warn1)

标签: javascript node.js discord discord.js


【解决方案1】:

您似乎在混合使用 v11 和 v12 语法。您使用roles.add (v12) 添加warn1,但使用user.addRole (v11) 添加warn2。如果warn1 有效,那么您使用的是discord.js v12,因此您应该在任何地方使用mentionedUser.roles.add

// role IDs
const warn1 = '808376848172974143';
const warn2 = '808376936006418524';
const warn3 = '808377003580457000';
const mute = '808377222884753459';
const beenMuted = '810008640802258975';
const mentionedUser = message.mentions.members.first();

if (mentionedUser.roles.cache.has(warn1)) {
    if (mentionedUser.roles.cache.has(warn2)) {
        message.channel.send('Already has 2 warnings.');
        mentionedUser.roles.add(warn3);
    }
    else {
        message.channel.send('Already has 1 warning.')
        mentionedUser.roles.add(warn2);
    }
}
else {
    mentionedUser.roles.add(warn1);
    message.channel.send('Warned the user.');
}

使用上面的代码可以正确添加角色:

【讨论】:

  • 我试过这个选项,但它对我不起作用。 here's the full thing if you want to check it out。请指出我哪里出错了。
  • @AndenWieseler 你能告诉我你是如何定义角色变量的吗? warn1warn2
  • const warn1 = message.guild.roles.cache.get('808376848172974143') const warn2 = message.guild.roles.cache.get('808376936006418524'); const warn3 = message.guild.roles.cache.get('808377003580457000'); const mute = message.guild.roles.cache.get('808377222884753459'); const beenMuted = message.guild.roles.cache.get('810008640802258975');
  • 哦,我明白了,你只需要角色的ID,所以你需要像const warn1 = '808376848172974143'这样使用它们。我已经更新了我的答案。
  • @AndenWieseler 是否适用于角色 ID?
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 2021-11-19
  • 2021-05-04
  • 2018-01-08
  • 2021-09-24
  • 2020-12-24
  • 2015-06-12
  • 2023-02-05
相关资源
最近更新 更多