【问题标题】:Cannot read property 'roles' of undefined discord.js v12无法读取未定义 discord.js v12 的属性“角色”
【发布时间】:2020-12-29 01:19:42
【问题描述】:

我希望机器人给反应器一个角色。但它不起作用。有谁知道问题出在哪里?我在 discord.js v12 中编码

if (user.bot) return;

if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();

if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.guild.id !== "753289194738024632") return;

if (reaction.message.channel.id === "753290581412937832") {
    const cs = bot.emojis.cache.find(emoji => emoji.name === "csgo");
    const vl = bot.emojis.cache.find(emoji => emoji.name === "valorant");
    const fn = bot.emojis.cache.find(emoji => emoji.name === "fortnite");

    let role = reaction.guild.roles.cache.find(role => role.id === '753290549548679229');
    member.roles.add(role.id);

【问题讨论】:

  • 确保memberGuildMember 属性的成员,而不是User 属性的成员

标签: discord.js


【解决方案1】:

我会改成这样:

if(message.channel.type === 'dm') return message.reply('This command does not work in DMs') //checks if the command wasnt executed in DMs
let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');

【讨论】:

    【解决方案2】:

    尝试将reaction 更改为message

    let role = message.guild.roles.cache.find(role => role.id === '753290549548679229');
    

    我认为reaction.guild 不是一个有效的函数

    【讨论】:

    • 不是它仍然无法正常工作。控制台给我这个错误:“ReferenceError:消息未定义”
    • 您没有定义消息,您使用的是命令处理程序,还是client.on('message')...
    • 确保你在消息事件中传递了message
    • 那么这个错误就在这里:Cannot read property 'guild' of undefined
    • 使用reaction.message.guild
    猜你喜欢
    • 2020-08-16
    • 2019-01-25
    • 2021-11-01
    • 1970-01-01
    • 2021-04-01
    • 2022-01-13
    • 2021-04-12
    • 2021-06-27
    • 2021-04-17
    相关资源
    最近更新 更多