Discord.JS功能错误,“欢迎留言”

时间:2017-10-22 14:08:16

标签: javascript node.js discord.js

我正在使用“Discord.JS”为Discord制作机器人 我正在尝试制作一个介绍消息,但我收到错误“无法读取未定义的属性'sendMessage'”

欢迎辞的我的代码:

var bot = new Discord.Client();

bot.on("guildMemberAdd", member => {
    let mem = member.guild
    mem.defaultChannel.sendMessage(member.user + " welcome to the server!"); });

任何帮助?

4 个答案:

答案 0 :(得分:2)

我认为正确的方法是通过ID或名称来获取频道,因为#gene可以是未定义的,正如安德烈指出的那样。

这样做的一个简单方法是加入&离去:

bot.on('guildMemberAdd', member => {
    member.guild.channels.get('channelID').send('**' + member.user.username + '**, has joined the server!'); 
});

bot.on('guildMemberRemove', member => {
    member.guild.channels.get('channelID').send('**' + member.user.username + '**, has left the server');
    //
});

转到用户设置>打开开发者模式;外观>开发者模式,然后右键单击频道并单击“复制ID”

答案 1 :(得分:0)

Reading about how Discord.js worksdefaultChannel似乎是用词不当,因为Discord或its API中没有这样的概念:

  

公会的#general TextChannel

实际上,#general频道可以重命名和删除,因此defaultChannel可以是undefined。您需要保护您对sendMessage

的来电
var bot = new Discord.Client();

bot.on("guildMemberAdd", member => {
    let mem = member.guild;

    if (mem.defaultChannel) {
        mem.defaultChannel.sendMessage(member.user + " welcome to the server!"); 
    } else {
        // do something if the #general channel isn't available
    }
});

答案 2 :(得分:0)

如果我没记错,{@ 1}}和guild#defaultChannel已被弃用。 (与client#setGame相同)但是,可以通过轻松找到频道来绕过它!

channel#sendMessage

然后你的代码最终会是这样的:

var defaultChannel = member.guild.channels.find( "name", "CHANNEL_NAME" );

答案 3 :(得分:0)

在我的机器人程序中,我使欢迎消息更加简单。也许这项工作:

 const Discord = require('discord.js');

 var bot = new Discord.Client();

 bot.on("guildMemberAdd", (member) => {

    let channel = bot.channels.get('*CHANNEL_ID*');

    channel.send(`Hey ${member.user}, welcome to the server!`); 
});