import discord
from discord.ext import commands
import aiofiles
from discord import app_commands
import pathlib
class JoinLogger(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_member_join(self, member):
async with aiofiles.open(pathlib.Path(__file__).parent / "pointslog.txt", mode="a") as file:
await file.write(f"User: {member.id}, Points: 3, Reason: Invited User\n")
await member.send(f"{member.mention}, you have been given 3 points for inviting someone! To check your points head to #1223230886862917662")
@commands.Cog.listener()
async def on_member_leave(self, member):
async with aiofiles.open(pathlib.Path(__file__).parent / "pointslog.txt", mode="a") as file:
await file.write(f"User: {member.id}, Points: -3, Reason: Invited User Left\n")
await member.send("{member.mention} someone has left from your invite, you have been removed 3 points. To check your points head to #1223230886862917662")
async def setup(client):
await client.add_cog(JoinLogger(client))
#๐ Why does this just randomly give other members poitns and doesnt really do what its ment to
27 messages ยท Page 1 of 1 (latest)
@inner spire
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
this gives a new member that joins 3 points, not the member who invited them. In this listener you have a member object as an argument, but this represents the new memeber.
so instead of
await member.send(f"{member.mention}, you have been given 3 points for inviting someone! To check your points head to #1223230886862917662")
it's actually
await member.send(f"Welcome {member.mention}! You have been given 3 points for joining the server! To check your points head to #1223230886862917662")
hold on but they look the same?
meber.mention?
all that different is welcome
yes, this was to show you who that member is, it's just visualization
from the context of your code, it looked like you were expecting it to be member who sent the invite instead of the member who actaully joined the server
yes
well, that im not sure of.. i've never tried doing something like this, let me look for something online
ok thank you
https://discordpy.readthedocs.io/en/stable/api.html#discord.Guild.invites
looks like you can get active invites for specific guild, after getting a list of invites, you can get an inviter from each invite which is the User object that represents member who sent the invite https://discordpy.readthedocs.io/en/stable/api.html#discord.Invite.inviter
but to use this, you would have to create your own event, could get a bit tricky, but i recommend using task decorator that is provided by discord module
could i try this?
total_invites = 0
for i in await interaction.guild.invites():
if i.inviter == user:
total_invites += i.uses
await interaction.send_message(f"{user.name} has invited {totalInvites} member{'' if totalInvites == 1 else 's'}!")```
the issue here is that you have to execute that in an interval probably, and i suppose invites can be active for longer period of time (haven't actually checked).. you have to make sure, user won't be awarded points multiple times for the same invite..
self.known_invites = []
@tasks.loop(seconds=20.0)
async def invites_task(self):
get invites
check new invites
add points to user + add invite to known invites
but is there not a way to like log it then in a file bc running every second would cause the boit to be slower and im sure theres a easyier way
like can we not log invites to a file so we know what invites weve done or smth?
you dont need to run it every second, maybe 10-20, try some iterations.. ofc there is a way to log, just add the logging inside the function
you have to tweak it a bit because some invites expire, keeping a list in memory like i drafted above is not the best way..
is there not just a way to check for the person joiing and then check there invite code?
no idea, try to create an invite and call guild.invites() in on_member_join event - then try to check if there is active invite created by you ..
you will need a second account or a friend to test this
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.