当人们加入我的服务器时,这种会招呼人们的不和谐机器人无法正常工作吗?

时间:2020-05-21 23:58:20

标签: javascript discord.js

我想向任何使用此bot加入我的不和谐服务器的人致欢迎词,但是当有人加入时,什么也没有发生。我收到一个错误:ReferenceError:通道名未定义 在C:\ Users \ Vir \ Desktop \ DiscBot \ index.js:13:71 在Map.find(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ util \ Collection.js:506:11) 在客户处。 (C:\ Users \ Vir \ Desktop \ DiscBot \ index.js:13:43) 在Client.emit(events.js:223:5) 在Guild._addMember(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ structures \ Guild.js:1298:19) 在GuildMemberAddHandler.handle(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ client \ websocket \ packets \ handlers \ GuildMemberAdd.js:12:13) 在WebSocketPacketManager.handle(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ client \ websocket \ packets \ WebSocketPacketManager.js:105:65) 位于WebSocketConnection.onPacket(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketConnection.js:333:35) 在WebSocketConnection.onMessage(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketConnection.js:296:17) 在WebSocket.onMessage(C:\ Users \ Vir \ Desktop \ DiscBot \ node_modules \ ws \ lib \ event-target.js:120:16) PS C:\ Users \ Vir \ Desktop \ DiscBot> 但是我不知道这意味着什么。我尝试搜索它,却一无所获。 代码是

const Discord = require('discord.js');
const bot = new Discord.Client();

const token = "NzEzMTcwNjc4NjYwMjAyNTA2.XscOxQ.0YxwpbBEITN0DIwGFwYIdRxCOu0";

const PREFIX = ";";

bot.on('ready', () =>{
    console.log('This bot is online!');
})

bot.on('guildMemberAdd', member =>{
    const channel = member.guild.channels.find(channel => channelname === "welcome");
    if(!channel) return;

    channel.send('Welcome, ${member}, make sure to read the rules and verfiy.') 
});


bot.on('message', message=>{

    let args = message.content.substring(PREFIX.length).split(" ")

    switch(args[0]){
        case 'Version':
            message.reply('Version 1.0.0');
        break;
        case 'Commands':
            message.reply(';Version ;Commands');
        break;


    }
})

bot.login(token);

3 个答案:

答案 0 :(得分:1)

这是我为此使用的代码

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

    const channel = member.guild.channels.cache.get('CHANNEL_ID'); 
    channel.send(`**Hey ${member.user}, welcome to the server!\nMake sure to read the rules in <#CHANNEL_ID>**`); 
});

答案 1 :(得分:0)

他们已对其进行了更改,因此您现在需要使用cache。因此,在这种情况下:

bot.on('guildMemberAdd', member =>{
    const channel = member.guild.channels.cache.find(channel => channel.name === "welcome");
    if(!channel) return;

    channel.send('Welcome, ${member}, make sure to read the rules and verfiy.') 
});

希望这会有所帮助!

答案 2 :(得分:0)

该错误表明未定义channelname。我相信您应该改用channel.name,只是一个简单的错字。

在OP的评论之后,我检查了docs,您必须访问cache属性以获取频道列表,如下所示:

bot.on('guildMemberAdd', member => {
    const channel = member.guild.channels.cache.find(channel => channel.name === "welcome");

    if(!channel) {
        return;
    }

    channel.send(`Welcome, ${member}, make sure to read the rules and verfiy.`) 
});