我有一个欢迎消息机器人,每次有人加入时都会发送欢迎消息,但有时会在控制台中引发错误,提示:
member.guild.channels.cache.find(ch => ch.name === "・????").send(exampleEmbed).catch(e => co nsole.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was
le to catch it.**"));
^
TypeError: Cannot read property 'send' of undefined
代码:
client.on('guildMemberAdd', member => {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(`Welcome to lovell <$, **${member.user.username}**`)
.setImage('https://images-ext-1.discordapp.net/external/Gy-micqoX7qrlivnytbmFgfTbxPx_CSvgChFLE3qC7g/https/media.discordapp.net/attachments/755425128635957348/758525058934767617/HaxzUWa46sK5CXKzIR.gif')
.setFooter('Boost Us');
member.guild.channels.cache.find(ch => ch.name === "・????").send(exampleEmbed).catch(e => console.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was unable to catch it.**"));
})
我该如何解决?
答案 0 :(得分:1)
有一种更简单的方法可以执行您要执行的操作。复制该频道的ID,然后像这样在您的代码中获取该频道:
bot.on('guildMemberAdd', (member) => {
const channel = member.guild.channels.cache.get('channel-id');
channel.send('Welcome');
});
答案 1 :(得分:0)
我的猜测是,如果找不到频道,则会发生该错误。找不到的通道将返回未定义的。 DiscordJS website中的这段代码将检查通道是否存在,如果不存在,则不会运行该函数的其余部分。
答案 2 :(得分:0)
也许尝试一下?:
client.on('guildMemberAdd', member => {
let welChannel = member.guild.channels.cache.find(ch => ch.name === "・????")
const exampleEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(`Welcome to lovell <$, **${member.user.username}**`)
.setImage('https://images-ext-1.discordapp.net/external/Gy-micqoX7qrlivnytbmFgfTbxPx_CSvgChFLE3qC7g/https/media.discordapp.net/attachments/755425128635957348/758525058934767617/HaxzUWa46sK5CXKzIR.gif')
.setFooter('Boost Us');
welChannel.send(exampleEmbed).catch(e => console.error(`Welcome Error in ${message.guild.name}`) | message.channel.send("__**ERROR:**__ ** Someone joined the server but I was unable to catch it.**"));
})
如果这不起作用,则可能是该漫游器找不到・????
频道。