user20310213
user20310213

Reputation:

delete warn on discord.py

im making a bot for my discord server but i have problem and my problem is this: There is a command for the warning which is $warn and a command to remove the warnings is $clrw and another command to show the warnings is $warns but there is a problem. When the user uses the $clrw command, it means that the variable that is accumulated during the warning becomes 0, but for all users, it means when the user use $clrw @moonboyInstead of only the desired person's warren being deleted, all people's warrens are deleted and this is a big problem!
my code:

@app.command()
# @commands.has_permissions(Warning = True)
async def warn(ctx,member: discord.Member,*,reason = None):
    global num
    if reason == None:
        await ctx.send(f"hey {ctx.author.mention} pls enter a reason!")
    else:
        num += 1 
        # await member.send(f"hey {ctx.author.mention} you have been warned bequase {reason} from {ctx.guild.name} pls dont do it again!")
        await ctx.send(f"{member} has been warned bequase {reason} ! ")
        return num
@app.command()
async def clrw(ctx,member:discord.Member):
    global num
    num = 0 
    await ctx.send(f"{member} warns has been deleted!")
@app.command()
async def warns(ctx,member: discord.Member):
    if num <= 0:
        await ctx.send(f"{member} has no warns!")
    else:
        await ctx.send(f"{member} has {num} warns!")

Upvotes: 0

Views: 269

Answers (2)

MrEdinLaw
MrEdinLaw

Reputation: 76

You're lacking understanding of the variable num.

The Problem: Atm what you are doing is, just adding and removing to the same variable or number in this case. The code cannot know if u meant a diff user this way.

The Fix: You should convert ur variable num to a dictionary. If the bot is gonna be for just one server then a simple dictionary should be enough.

Example:

  • When doing the current num +=1 replace it with num[member.id] +=1
  • For reset instead of num = 0 do num[member.id] = 0
  • For the getting of warnings, instead of num do num[member.id]

In this case u will mention who exactly u want to reset the warns for, get it from or add to.

Also don't forget to convert the current num value where u set it the first time to num = []

Hope this helps!

PS. The amount of warns will reset on bot restart, as this is a variable in the code its not saved anywhere outside. I would recommend saving it in a file or redis server.

Example code for the error in the comment: Use this for the command where u add the warning. The reason u got that error is that u cannot add a +1 warning to an user who is not added in the array itself.

if member.id not in num:
   num[member.id] = 1
else:
   num[member.id] +=1

So u don't get a similar error with the getting of the warn. Example:

if member.id not in the num or num[member.id] == 0:
   await ctx.send(f"{member} has no warns!")
else:
   await ctx.send(f"{member} has {num[member.id]} warns!")

Upvotes: 1

Matthew Miles
Matthew Miles

Reputation: 737

the global variable num is not related to any particular user. If you wanted to do it per user, you could use a dictionary of their user ids.

warns = dict()

# add a warning
userid = member.id # not sure if this is exactly how to get id, check docs
if userid not in warns:
  warns[userid] = 1
else:
  warns[userid] += 1

# clear warnings
warns[userid] = 0

# display warnings
if warns[userid] == 0:
  await ctx.send(f"{member} has no warnings")
else:
  await ctx.send(f"{member} has {warns[userid]} warnings")

(This is all pseudocode to show how dicts work. This will obviously not work when pasted directly into your code)

Upvotes: 1

Related Questions