BlueFire02
BlueFire02

Reputation: 45

Can't get the total amount of invites a user did

I am trying to get the invites of the author of the command to the one server [Not multiple servers just one] I have tried many solutions but many seem outdated or wrong. Here is what I came up with

exports.run = async (client, message, args) => {
    var user = message.author;
    message.guild.invites.fetch()
        .then(invites => {
             const userInvites = invites.filter(o => o.inviter.id === user.id);
             var userInviteCount = 0;
             console.log(userInvites['uses']);
             for (var i=0; i < userInvites.length; i++) {
                 var invite = userInvites[i];
                 userInviteCount += invite['uses'];
             }
             message.reply(`You have ${userInviteCount} invites.`);
         } )
}

This is in an invite.js file so when a user runs !invites it runs this command after exports.run

Upvotes: 2

Views: 81

Answers (1)

Ruben Restrepo
Ruben Restrepo

Reputation: 1206

Here is a working implementation from what you want to accomplish, a few notes:

  1. A Slash command called invites was created to demo this functionality.
  2. If you want to try the command on your own you can join the demo server here
  3. See attached screenshot of the working slash command
  4. If you run the example, you will get all the created invites
  5. If you want to filter by user id, you need to execute the slash command in Discord
Responding to a slash command                                                                                 Run in Fusebit
// Respond to a Slash command
integration.event.on('/:componentName/webhook/:eventType', async (ctx) => {
  const {
    data: { application_id, token, member },
  } = ctx.req.body;

  // Store the user id that triggered the bot command.
  const commandTriggeredByUser = member.user.id;
  const discordClient = await integration.service.getSdk(ctx, connectorName, ctx.req.body.installIds[0]);
  const guildInvites = await discordClient.bot.get(`guilds/${DISCORD_DEMO_GUILD_ID}/invites`);

  // Filter the user created invites only
  const userCreatedInvites = guildInvites.filter(invite => invite.inviter.id === commandTriggeredByUser);

  // Reply to the slash command with the number of created invites
  await superagent.post(`https://discord.com/api/v8/webhooks/${application_id}/${token}`).send({
    content: `You have created ${userCreatedInvites.length} invites`,
  });
});

Upvotes: 2

Related Questions