首页 > 解决方案 > 处理的PromiseRejectionWarning:TypeError:无法读取未定义的属性“添加”(Discord Bot)

问题描述

我一直在处理我的不和谐机器人验证码的问题。验证码工作得非常好,但是如果他们验证了添加一个角色,这会让我大吃一惊

处理的PromiseRejectionWarning:TypeError:无法读取未定义的属性“添加”

这是我的代码:

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.');

任何帮助表示赞赏!^^

错误:

标签: javascriptdiscord.jsundefined

解决方案


我在您的代码中看到两个问题。

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

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

祝你有美好的一天 :)


推荐阅读