Add invite data to user joined event



  • espen


    This is a viable implementation. Looking at the solutions you provided, I'd opt for the last one mentioned. Associating all GuildMemberAdd (GMA) objects with an Invite Code enables more accurate tracking of invites & voids the issue where a bot has to essentially guess which GMA is associated to an invite.

    Supposing you want to track invities associated with a user, you only need to GET Invite Object once. This is required to associate an inviter with their respective Invite Codes. You do not have to do a REST upon all GMA events, so I don't consider it a huge deal. Just link the inviter with the invite codes ONCE, and you're done - no excessive load:


    "invites": [
    "code_one": 54,
    "code_two": 3

    Psst... This is the appropriate GMA link- yours is broken! (




  • B R

    Very well stated and laid out! To add to that, I'd love to see the Guild Name or ID of where that link was clicked - if it was sent via Discord and the option to block or report invites from that Guild. That won't always be an option since the invite could be from anywhere on the web, but any tools to help server admins are appreciated!

  • moein

    I'm thinking about building a tool that allows a bot you invite to your channel to grant roles and invite the user to channels based on the invitation link they join with
    It's precise and in edge cases adds a little work to the invitee
    If anyone interestedin helping me with the beta testing please reach out to me on discord (you will get a year of free access as a thank you gift)

    My discord id: moein#0371

  • Sindrah

    Just having an inviteCode property set, even if just for GMA events, would guarantee that bots would never fail to properly identify the correct invite that is used, which also allows bots to notate who invited that user when they join - the biggest reason for tracking invites in the first place.

    As it stands right now, in order to even glint an idea of who invited someone, the bot has to fetch all invites at startup (and on each restart), store them along with their counted uses and the inviter id, and then wait for a GMA event to occur, during which the bot has to refetch EVERY invite per user joining AND fetch the stored invites and hope only one invite use has occured between the GMA event being caught and the two fetch operations for comparison.

    What I have experienced in a large server is that from time to time, just 2 users joining within a few milliseconds of each other can cause the bot to miss an invite usage entirely, which can throw the counts out of whack. This gets especially more convoluted when a raid occurs, where dozens, if not hundreds, of spam joins could cause all sorts of confusion with how they joined, and can cause the botcode to encounter errors due to incorrect counts on invite uses stored vs actual invite uses...not to mention what user joined with what code.

    All of this disarray and lag vulnerability with missed/improperly timed/caught events is easily remedied if GMA events passed the GuildMember Object with a property of their invite code (then only a 2 column table would be needed for invite storage: invite.code and, which would never require a full guild.invites.fetch() every time a member is added.

  • Sindrah

    just to highlight the current issue:

    I manage and moderate a public community server of nearly 10k members, and we see almost weekly raids of anywhere from 20 - 200 spam accounts, and roughly 20% of those accounts joining have phone-verified somehow and manage to engage with the server. We see about 150 - 250 users join per week outside of raids, and most occur on Fridays. This means our in-house bot (which I maintain) has to, among other requests to the API, hit the API for upwards of 200+ invites each time a user joins.

    If each GMA event gave me a property of what invite code was used at that moment, I could just perform a select query on my DB for that invite code to see if it was stored, otherwise if it isn't yet (unlikely since I am listening to inviteCreate events and storing them when they are made), I only need to fetch the one invite instead of all of them (and also not have to waste tons of time trying to discover which one invite had a use in a huge for loop checking against some stored "last known" number...which has issues if the bot gets restarted).

    Theoretically, my bot would never request an invite during a GMA if the invite code is known, because the bot would have done the fetchAllInvites at startup, and would add/make invites in the DB as invite events get triggered.