Discord.js:TypeError:无法读取未定义的属性“get”
Posted
技术标签:
【中文标题】Discord.js:TypeError:无法读取未定义的属性“get”【英文标题】:Discord.js: TypeError: Cannot read property 'get' of undefined 【发布时间】:2020-09-04 18:15:35 【问题描述】:谁能告诉我我做错了什么?
/*
bot.on('message', message =>
if(message.author.bot) return;
const TicketToolEmbed = new Discord.MessageEmbed()
.setTitle("Creat a support Ticket")
.setDescription("React to this message to open a support ticket with the Server Administrators")
.setColor("#B900FF")
.setTimestamp()
.setFooter("Ticket Bot System")
message.channel.send(TicketToolEmbed)
.then(message => message.react('✅'))
.catch(err => console.log(err));
); */
bot.on('raw', async payload =>
let eventname = payload.t;
if (eventname === 'MESSAGE_REACTION_ADD')
let msgId = payload.d.message_id;
if (msgId === '712016328189411348')
let channelId = payload.d.channelId;
let channel = Client.channels.get(channelId);
if (channel)
if (channel.message.has(msgId))
return;
else
try
let msg = await channel.fetchMessage(msgId);
let reaction = msg.reaction.get('✅');
let user = Client.user.get(payload.d.user_id);
Client.emit('messageReactionAss', reaction, user);
catch (ex)
console.log(ex);
return;
);
bot.on('messageReactionAdd', (reaction, user) =>
if(reaction.emoji.name === '✅')
console.log("Creating ticket...");
else
console.log("Wrong emoji");
);
控制台:
(node:11924) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of undefined
at Client.<anonymous> (C:\Users\sande\Desktop\discbot\index.js:65:43)
at Client.emit (events.js:310:20)
at WebSocketShard.onMessage (C:\Users\sande\Desktop\discbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:287:27)
at WebSocket.onMessage (C:\Users\sande\Desktop\discbot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:310:20)
at Receiver.receiverOnMessage (C:\Users\sande\Desktop\discbot\node_modules\ws\lib\websocket.js:800:20)
at Receiver.emit (events.js:310:20)
at Receiver.dataMessage (C:\Users\sande\Desktop\discbot\node_modules\ws\lib\receiver.js:436:14)
at Receiver.getData (C:\Users\sande\Desktop\discbot\node_modules\ws\lib\receiver.js:366:17)
at Receiver.startLoop (C:\Users\sande\Desktop\discbot\node_modules\ws\lib\receiver.js:142:22)
(node:11924) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict
(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1)
(节点:11924)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
【问题讨论】:
【参考方案1】:我相信您在所有代码示例中都使用了Client
而不是bot
。
尝试以下方法:
bot.on('raw', async payload =>
let eventname = payload.t;
if (eventname === 'MESSAGE_REACTION_ADD')
let msgId = payload.d.message_id;
if (msgId === '712016328189411348')
let channelId = payload.d.channelId;
let channel = bot.channels.get(channelId);
if (channel)
if (channel.message.has(msgId))
return;
else
try
let msg = await channel.fetchMessage(msgId);
let reaction = msg.reaction.get('✅');
let user = bot.user.get(payload.d.user_id);
bot.emit('messageReactionAdd', reaction, user); // Also corrected "messageReactionAss" to "messageReactionAdd".
catch (ex)
console.log(ex);
return;
);
bot.on('messageReactionAdd', (reaction, user) =>
if(reaction.emoji.name === '✅')
console.log("Creating ticket...");
else
console.log("Wrong emoji");
);
【讨论】:
以上是关于Discord.js:TypeError:无法读取未定义的属性“get”的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:无法读取未定义的“获取”函数(discord.js)
Discord.js:TypeError:无法读取未定义的属性“删除”
(Discord.js)TypeError:无法读取未定义的属性“添加”
Discord.js:TypeError:无法读取未定义的属性“get”