javascript - 处理的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.');
任何帮助表示赞赏!^^
解决方案
我在您的代码中看到两个问题。
message.author 指的是一个不持有角色的用户。改用 message.member
将 r.id 替换为 r.name,id 不是字符串。
祝你有美好的一天 :)
推荐阅读
- javascript - 在mysql中插入数组数据并防止节点js中的重复
- react-native - 反应以大写形式出现的本机顶部选项卡导航器标签
- ios - 必须在 uimanager 队列上调用此函数
- visual-studio-code - 如何通过快捷方式将光标向上或向下移动半页?
- python - 如何从单个消息中获得反应,而不是任何具有相同表情符号的消息
- html - 使 div 下拉导航栏元素的大小和位置与超链接导航栏元素相同
- next.js - 为什么 purgeCSS 会删除我的 React-Bootstrap 应用程序使用的样式?
- python - Amazon RDS:OperationalError:(2003,“无法连接到 rds.amazonaws.com (110) 上的 MySQL 服务器”)
- c++ - Initializing multi-dimensional std::vector without knowing dimensions in advance
- javascript - 在屏幕之间传递数据、route.params、React Native 导航