It seems you forgot to check if your invite author is correct or not.
You loop over the invites, but don't have a `if i.inviter == member`.
The fixed code would be:
```python
@client.command()
async def invites(ctx, user = None):
if user == None:
totalInvites = 0
for i in await ctx.guild.invites():
if i.inviter == ctx.author:
totalInvites += i.uses
await ctx.send(f"You've invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the server!")
else:
totalInvites = 0
for i in await ctx.guild.invites():
member = ctx.message.guild.get_member_named(user)
if i.inviter == member:
totalInvites += i.uses
await ctx.send(f"{member} has invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the server!")
```
Also, the reason it spammed is because you had your `await ctx.send` statement inside of your for-loop.
You've almost got the right idea!
___
# on_message event usage:
```python
@bot.event
async def on_message(message):
if message.content.startswith('!invites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You've invited {totalInvites}
member{'' if totalInvites == 1 else 's'} to the server!")
```
___
# Command decorator usage:
```python
@bot.command()
async def invites(ctx):
totalInvites = 0
for i in await ctx.guild.invites():
if i.inviter == ctx.author:
totalInvites += i.uses
await ctx.send(f"You've invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the server!")
```
___
First I'm iterating through each invite in the guild, checking who created each one. If the creator of the invite matches the user that executed the command, it then adds the number of times that invite has been used, to a running total.
You don't need to include the `{'' if totalInvites == 1 else 's'}`, that's just for the odd case that they've invited 1 person (turns `member` into the plural - `members`).
___
**References:**
- [`Guild.invites`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.invites) - the code originally didn't work because I forgot this was a coroutine (had to be called `()` and `await`ed).
- [`Invite.uses`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Invite.uses)
- [`Invite.inviter`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Invite.inviter)
- [`commands.command()`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.command)
- [F-strings](https://realpython.com/python-f-strings/) Python 3.6+