【发布时间】:2021-03-25 08:24:52
【问题描述】:
我正在尝试创建一个允许我的机器人向人们发送消息的系统,但是在尝试向自上次重新启动以来尚未向机器人发送 dm 的用户发送消息时似乎总是出现错误。
这是我不断收到的错误:
TypeError: Cannot read property 'send' of undefined
这是重要的代码:
message.channel.send({ embed: dmattempt }).then(sentMessage => {
try {
// Tries to send a message to the user
const themsguser = client.users.cache.get(dmId);
themsguser.send(`${sentence}`);
这是整个命令的代码:
client.on('message', async message => {
function directMsgUser(dmUser, dmCommand, dmId) {
if (message.content.startsWith(`${prefix}${dmCommand}`)) {
let sentence = message.content.split(' ');
sentence.shift();
sentence = sentence.join(' ');
const dmattempt = {
color: [0, 0, 255],
title: `trying to direct message ${dmUser}`,
description: 'please wait',
footer: {
text: `message sending from ${message.author.tag}`,
icon_url: `${message.author.avatarURL()}`,
},
};
const msgsent = {
color: [0, 255, 0],
title: `message sent to ${dmUser}`,
description: `message content: ${sentence}`,
footer: {
text: `message sent by ${message.author.tag}`,
icon_url: `${message.author.avatarURL()}`,
},
};
// Sends a message confirming that the message is trying to be sent
console.log(`Trying to direct message ${dmUser}`);
message.channel.send({ embed: dmattempt }).then(sentMessage => {
try {
// Tries to send a message to the user
const themsguser = client.users.cache.get(dmId);
themsguser.send(`${sentence}`);
// Edits the message to verify it was sent
sentMessage.edit({ embed: msgsent });
console.log('Message sent!');
}
catch (error) {
// Edits the message to verify something went wrong
const errorembed = {
color: [255, 0, 0],
title: 'error!',
description: `There was an error messaging ${dmUser}`,
footer: {
text: `message sent by ${message.author.tag}`,
icon_url: `${message.author.avatarURL()}`,
},
};
sentMessage.edit({ embed: errorembed });
console.error(error);
console.error('There was an error sending a direct message.');
}
});
}
}
if (message.content.startsWith(`${prefix}dm`)) {
directMsgUser(theUser, theCommand, idforuser);
}
});
请注意,如果要向其发送消息的用户在机器人重新启动后已向机器人发送了 dm,则此命令有效。
提前感谢您的帮助。
【问题讨论】:
标签: javascript discord.js