当人们加入我的服务器时向他们打招呼的这个 discord 机器人不起作用吗?
2020-05-21
728
我试图用这个机器人向加入我的 discord 服务器的人发送欢迎信息,但当有人加入时什么都没有发生。我收到一个错误:
ReferenceError: channelname is not defined at C:\Users\Vir\Desktop\DiscBot\index.js:13:71 at Map.find (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\util\Collection.js:506:11) at Client. (C:\Users\Vir\Desktop\DiscBot\index.js:13:43) at Client.emit (events.js:223:5) at Guild._addMember (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\structures\Guild.js:1298:19) at GuildMemberAddHandler.handle (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildMemberAdd.js:12:13) at WebSocketPacketManager.handle (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65) at WebSocketConnection.onPacket (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35) at WebSocketConnection.onMessage (C:\Users\Vir\Desktop\DiscBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17) at 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个回答
这是我为此使用的代码
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>**`);
});
xZqte
2021-07-03
他们已经更改了它,因此您现在需要使用
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.')
});
希望这有帮助!
Hao C.
2020-05-22
错误表明
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.`)
});
emeraldsanto
2020-05-22