【问题标题】:handledPromiseRejectionWarning: TypeError: Cannot read property 'add' of undefined (Discord Bot)处理的PromiseRejectionWarning:TypeError:无法读取未定义的属性“添加”(Discord Bot)
【发布时间】:2021-10-23 18:27:03
【问题描述】:

我一直在处理我的不和谐机器人验证码的问题。验证码的效果非常好,但是如果他们验证了添加角色,我会感到很震惊

handledPromiseRejectionWarning: TypeError: Cannot read property 'add' of undefined

这是我的代码:

const Discord = require('discord.js-12');

const client = new Discord.Client();

const prefix = 'ri-';

const Captcha = require("@haileybot/captcha-generator");

client.once('ready', () => {
    console.log('Ready!');
});
let captcha = new Captcha();
console.log(captcha.value);
 

const path = require("path"),
    fs = require("fs")
 
client.on('message', async message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);

    const command = args.shift().toLowerCase();

    if (command === 'verification') {
 
            let captcha = new Captcha();
            message.channel.send(
                "**Enter the text shown in the image below:**",
                new Discord.MessageAttachment(captcha.JPEGStream, "captcha.jpeg")
            );
            let collector = message.channel.createMessageCollector(m => m.author.id === message.author.id);
            collector.on("collect", m => {
                if (m.content.toUpperCase() === captcha.value){ message.channel.send("Verified Successfully!");
                let role = message.guild.roles.cache.find(r => r.id === "Verified");
                message.author.roles.add(role);
                }else{ message.channel.send("Failed Verification!");}
                collector.stop();
    });
        

    }
});
        

client.login('you don't need this.');

感谢任何帮助! ^^

错误:

【问题讨论】:

  • message.author 返回不具有角色的不和谐用户。你想要一个 Discord 会员,让会员使用 message.member 代替
  • TypeError [INVALID_TYPE]:提供的角色不是角色、雪花或数组或角色或雪花的集合。当我尝试时它给了我这个错误。

标签: javascript discord.js undefined


【解决方案1】:

我发现您的代码中有两个问题。

  1. message.author 指的是一个不持有角色的用户。改用 message.member

  2. 将 r.id 替换为 r.name,id 不是字符串。

祝你有美好的一天:)

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 2020-06-20
    • 1970-01-01
    • 2021-03-31
    • 2020-10-15
    • 2021-09-14
    • 2021-02-01
    • 2021-02-22
    • 2021-01-08
    相关资源
    最近更新 更多