简体   繁体   中英

discord.js New Member Event

module.exports = {
name: 'newmember',
once: true,
run: async(client) => {
    guildMemberAdd, member => {
        try {
        const welcome = new Discord.MessageEmbed()
            .setColor('#57F287')
            .setDescription(`Добро пожаловать, ${member.user} на сервер.
            \nВы можете прочитать наши правила в канале <#849407505661952001>`)
        member.guild.channels.cache.get('847196125756915772').send(welcome)
        } catch(e) {
            console.error(e)
        }
    }
}

}

When i'm trying to test this event, he just doing nothing. No Errors, No Messages or response. What am i doing wrong?

you have problems with your code but since you did not provide a complete code sample it's hard to say what the solution is.

run: async (client) => {
  guildMemberAdd, member => {
    /* ... */
  }
}

this does not do what you think it does. it's equivalent to

run: async function (client) {
  guildMemberAdd,
  function (member) {
    /* ... */
  }
}

so your run function does no work; it just evaluates the expression a, b (where a is whatever guildMemberAdd is, and b is your inner fat-arrow function). this resolves to whatever b is, which in this case is a function, but you're never calling or returning that function either. this is why you are getting no errors.

is guildMemberAdd a function you should be calling? is it the name of an event for which you're attempting to register a listener? is your fat-arrow function something you should be calling? are you attempting to pass two arguments to a fat-arrow function (which requires parentheses around the arguments)? I have no idea how discord works but the exact answer to your question depends on what the API docs say you should be doing.

I assume your guildMemberAdd method either returns a promise of a member or accepts a callback method that needs a member as an argument. Depending on what your guildMemberAdd method does your syntax is a little off.

If it returns a promise of a user

module.exports = {
name: 'newmember',
once: true,
run: async(client) => {
  try {
    const member = await guildMemberAdd()
    const welcome = new Discord.MessageEmbed()
            .setColor('#57F287')
            .setDescription(`Добро пожаловать, ${member.user} на сервер.
            \nВы можете прочитать наши правила в канале <#849407505661952001>`)
        member.guild.channels.cache.get('847196125756915772').send(welcome)
  } catch (error) {
    throw error
  }
}

Alternatively, you could be attempting to call.then() on a promise

guildMemberAdd().then((member) => {
   // rest of your code here
})

If it accepts a callback

I think this is the closest to your code syntax but I also feel its more likely your method returns a promise. The reason I think this is because you don't already have access to anything named member. Therefore, the member must be something that is returned from your guildMemberAdd method.

module.exports = {
name: 'newmember',
once: true,
run: async(client) => {
  try {
    // you'll need to access the current member somehow
    // const member = fetch member here somehow...
    guildMemberAdd(member, () => {
      // your code here
    })
    // OR
    guildMemberAdd((member) => {
      // your code here
    })
  } catch (error) {
    console.log(error)
  }
} 

I think either way we'd need to see an example of what guildMemberAdd is in your code for us to be able to give you a solid answer. I hope this helps though. Good Luck!

Updated GuildMemberAdd Documentation As of discordjs v14.3 the event name is guildMemberAdd and it passes the member as an argument to your event handler. Therefor, you do already have access to it if you pass it into your run method from your event handler. For clarification the...args is whatever the specific discordjs event returns, in your case the guildMember. Then we pass those along to our run method for that event.

Example From My Bot

Please note that I'm using ECMAScript Module System instead of CommonJs. So you wont be able to copy and paste this example into your code directly. You'll need to convert it to a commonJs module first.

export default {
  name: "guildMemberAdd",
  type: "client",
  once: true,
  async run(member, client) {
    // your code here
  },
};

// The event handler I use
// The switch statement and event.type are optional. 
// I plan on having database events that I listen to as well as client events.
import { clientEvents } from "../../events/client/index.mjs";

export async function handleEvents(client) {
  clientEvents.forEach(async (event) => {
    switch (event.type) {
      case "client":
        if (event.once) {
          client.once(event.name, async (...args) => {
            await event.run(...args, client);
          });
        } else {
          client.on(event.name, async (...args) => {
            await event.run(...args, client);
          });
        }
      default:
        break;
    }
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM