#๐Ÿ”’ 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
#
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))

hard thicketBOT
#

@inner spire

Python help channel opened

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.

silent pollen
# inner spire ```py import discord from discord.ext import commands import aiofiles from disco...

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")
inner spire
#

meber.mention?

#

all that different is welcome

silent pollen
#

yes, this was to show you who that member is, it's just visualization

inner spire
#

ohh

#

how do i sent it to the inviter

#

the person that invited that member

silent pollen
#

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

inner spire
#

yes

silent pollen
#

well, that im not sure of.. i've never tried doing something like this, let me look for something online

inner spire
#

ok thank you

silent pollen
#

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

inner spire
#

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'}!")```
silent pollen
#

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
inner spire
#

like can we not log invites to a file so we know what invites weve done or smth?

silent pollen
#

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..

inner spire
#

is there not just a way to check for the person joiing and then check there invite code?

silent pollen
#

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

hard thicketBOT
#
Python help channel closed

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.