我希望我的机器人将dm发送给执行命令!RHelp
的人。
为此,我编写了以下代码:
if (message.content.startsWith('!RHelp')){
client.users.fetch('<User ID>').then((user) => user.send('Hello World'));
message.reply('I just sent you a DM');}
机器人正在正确执行代码,但是在发送消息后,他因以下错误而崩溃:
const member = message.guild.member(user);
^
TypeError: Cannot read property 'member' of null
我曾试图做message.author.send('Hello World');
,但这是同样的问题...
我要在单独的文件'utiles.js'
中声明用户和成员:
const Discord = require('discord.js');
const Client = new Discord.Client();
module.exports ={
getUtiles: function (message){
const user = message.mentions.users.first();
const member = message.guild.member(user);
const args = message.content.trim().split(/ +/g);
const command = args.shift().toLowerCase();
const usrTag ='<@'+ member + '>'; //get the mentionned user tag
this.user = user;
this.member = member;
this.args = args;
this.usrTag = usrTag;
return user, member, args, usrTag;
}
}
答案 0 :(得分:1)
由于您是在DM中处理事务,因此没有message.guild
,因此没有成员函数。您需要检查message.guild是否为null,然后采取相应措施。尚不清楚getUtiles
函数如何/何时运行,因此具体解决方案取决于您。
答案 1 :(得分:0)
将const member = message.guild.member(user);
更改为const member = message.member
,此代码对我有用,因此将if (command === 'rhelp') {
替换为if (message.content.startsWith('!RHelp')){
以解决您的问题。
if (command === 'rhelp') {
let member = message.member
member.send("Hello")
message.reply('I just sent you a DM');
}