Hi I'm having an issue checking through a list codes that are stored in a temp dictionary and matching ones that exist and if one doesn't exist then print/await a different result. I'm doing this by comparing the uses for each invite before the user joined and after they joined.
It seems as if the if statement if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
is being ignored and the else statement is being printed/awaited instead for both expired and none expired code. I'm not too sure why this is happening. Help would be appreciated.
I have commented parts of the code for a better understanding.
Here is my code:
invites = {} #temporary dict to store the codes
# A function to find an invite in a guilds.invites() list
def find_invite_by_code(invite_list, code):
# Simply looping through each invite in an
# invite list which we will get using guild.invites()
for inv in invite_list:
# Check if the invite code in this element
# of the list is the one we're looking for
if inv.code == code:
# If it is, we return it.
return inv
@commands.Cog.listener()
async def on_member_join(self, member):
time = datetime.utcnow()
users = len([e.name for e in member.guild.members])
name = member
name = " ~ ".join((name.name, name.nick)) if name.nick else name.name
# Getting the invites before the user joining
# from our cache for this specific guild
invites_before_join = invites[member.guild.id]
# Getting the invites after the user joining
# so we can compare it with the first one, and
# see which invite uses number increased
invites_after_join = await member.guild.invites()
# Loops for each invite we have for the guild
# the user joined.
for invite in invites_before_join:
# Now, we're using the function we created just
# before to check which invite count is bigger
# than it was before the user joined.
if invite.uses < find_invite_by_code(invites_after_join, invite.code).uses:
# Now that we found which link was used we'll start the embed
joinmsg = discord.Embed(title=f'{member.name}#{member.discriminator} has joined', timestamp=time, colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
joinmsg.add_field(name="**Account Created:**", value=member.created_at.strftime("%#d %b %Y, at %I:%M%p"), inline=True)
joinmsg.add_field(name="**Invite:**", value=f"||{invite.code}||", inline=True)
joinmsg.add_field(name="**Inviter:**", value=f"{invite.inviter}", inline=False)
joinmsg.set_footer(text=f"Member {users} • User ID: {member.id}")
joinmsg.set_thumbnail(url=member.avatar_url)
await self.bot.send_log_message(member.guild, embed=joinmsg)
#We use an else statement if a NoneType is returned this would happen if the invite link has expired.
else:
# Create the embed to be sent
joinmsg = discord.Embed(title=f'{member.name}#{member.discriminator} has joined', timestamp=time, colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
joinmsg.add_field(name="**Account Created:**", value=member.created_at.strftime("%#d %b %Y, at %I:%M%p"), inline=True)
joinmsg.add_field(name="**Invite:**", value=f"Expired", inline=True)
joinmsg.set_footer(text=f"Member {users} • User ID: {member.id}")
joinmsg.set_thumbnail(url=member.avatar_url)
await self.bot.send_log_message(member.guild, embed=joinmsg)
# We will now update our cache so it's ready
# for the next user that joins the guild
invites[member.guild.id] = invites_after_join
# return to prevent looping when we already got what we want
return
It looks like something isn't happening quite the way you want it to, I would add a print
function before it and print the values of invite.uses
and find_invite_by_code(invites_after_join, invite.code).uses
and then print the type
of them too, if you still can't spot the error, then we'll have a problem
print(invite.uses, find_invite_by_code(invites_after_join, invite.code).uses)
print(type(invite.uses), type(find_invite_by_code(invites_after_join, invite.code).uses))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.