discord.js 事件处理程序欢迎消息

时间:2021-07-14 22:21:02

标签: node.js discord discord.js bots

所以我为我的 discord bot 制作了一个事件处理程序,以便 index.js 文件整洁。但是由于某种原因,我在有人加入服务器时发出的欢迎信息不起作用。

这是我的事件处理程序代码:

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args, Discord, client));
    } else {
        client.on(event.name, (...args) => event.execute(...args, Discord, client));
    }
}

这是我的欢迎信息代码:

module.exports =  {
    name: 'welcome',
    once: false,
    execute(Discord, client) {

    const welcomechannelId = '753484351882133507' //Channel You Want to Send The Welcome Message
    const targetChannelId = `846341557992292362` //Channel For Rules

        client.on('guildMemberAdd', (member) => {
            let welcomeRole = member.guild.roles.cache.find(role => role.name === 'Umay');
            member.roles.add(welcomeRole);

            const channel = member.guild.channels.cache.get(welcomechannelId)

            const WelcomeEmbed = new Discord.MessageEmbed()
            .setTitle(`Welcome To ${member.guild.name}`)
            .setThumbnail(member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setDescription(`Hello <@${member.user.id}>, Welcome to **${member.guild.name}**. Thanks For Joining Our Server.
Please Read ${member.guild.channels.cache.get(targetChannelId).toString()}, and assign yourself some roles at <#846341532520153088>. You can chat in <#753484351882133507> and talk with other people.`)
         // You Can Add More Fields If You Want
            .setFooter(`Welcome ${member.user.username}#${member.user.discriminator}`,member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setColor('RANDOM')
        member.guild.channels.cache.get(welcomechannelId).send(WelcomeEmbed)
        
    })

    }
}

我没有发现任何错误,但是每当有人加入服务器时,他/她都不会被授予角色,也不会出现欢迎消息。我将欢迎消息代码放在事件处理程序正在处理的事件文件夹中。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

问题出在欢迎代码中。

在处理程序代码中,您有以下行:

client.on(event.name, (...args) => event.execute(...args, Discord, client));

这会在欢迎代码中设置的 name 属性上触发客户端。

您当前已将其设置为 welcome,这不是有效事件。 机器人现在正在侦听永远不会发生的欢迎事件。

第一步是将 name 属性设置为 guildMemberAdd,如下所示:

module.exports =  {
    name: 'guildMemberAdd',
//the rest of the code

那么你还有另一个问题。 在欢迎代码中,您client.on() 再次

这永远不会奏效,除非有非常幸运的机会 2 个人在一毫秒内加入,但即便如此,您也只会收到 1 条欢迎信息。

删除以下内容:

client.on('guildMemberAdd', (member) => {
//code
})

将解决该问题。

那么最后要做的就是正确导入成员值。 我们通过更改以下行来做到这一点:

execute(Discord, client) {

到:

execute(member, Discord, client) {
//code

生成的代码如下所示:

module.exports =  {
    name: 'guildMemberAdd',
    once: false,
    execute(member, Discord, client) {

    const welcomechannelId = '753484351882133507' //Channel You Want to Send The Welcome Message
    const targetChannelId = `846341557992292362` //Channel For Rules

            let welcomeRole = member.guild.roles.cache.find(role => role.name === 'Umay');
            member.roles.add(welcomeRole);

            const channel = member.guild.channels.cache.get(welcomechannelId)

            const WelcomeEmbed = new Discord.MessageEmbed()
            .setTitle(`Welcome To ${member.guild.name}`)
            .setThumbnail(member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setDescription(`Hello <@${member.user.id}>, Welcome to **${member.guild.name}**. Thanks For Joining Our Server.
Please Read ${member.guild.channels.cache.get(targetChannelId).toString()}, and assign yourself some roles at <#846341532520153088>. You can chat in <#753484351882133507> and talk with other people.`)
         // You Can Add More Fields If You Want
            .setFooter(`Welcome ${member.user.username}#${member.user.discriminator}`,member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setColor('RANDOM')
        member.guild.channels.cache.get(welcomechannelId).send(WelcomeEmbed)

    }
}

快乐编码!