So I am making a discord bot that will be recording a certain "cred" for each person in my server and we can increase or decrease certain people's cred with a simple command. The only issue that I have run into so far is that I don't want someone to be able to give themselves cred. Think of cred as how much someone respects you. It's purely a joke thing, but my friend and I felt that this would be a cool little project to hone our python skills.
Here is our code:
import discord
import os
from discord.ext import commands
from replit import
dblist = ["NMShoe#xxxx", "Nathan"], ["Jerlopo#xxxx", "Shawn"],["Flinters#xxxx", "Kaylan"] #x's are numbers
@bot.command(name="givecred", description="Gives credit to a certain person.")
async def givecred(ctx, name: str, cred: int): #takes (str)name and (int)cred into function
if ctx.author == bot.user: #if the bot reads a message from itself, it ignores
return
else:
if name in db.keys(): #checks if the name they entered is in the database
await ctx.send("Added {} cred to {}!".format(cred, name))
db[name] = db[name] + cred #adds and updates database value
await ctx.send("{}'s at {} cred now!".format(name, db[name]))
return
else:
await ctx.send("Did not enter the correct variables. Please enter: '.givecred {name} {#of cred}'.")
return
#Database (key, value) --> <Shawn, 0>, <Nathan, 0> This is how I have the database set up at the moment
But I basically need an if statement at the beginning of the first else statement to ensure that if I were to say ".givecred Nathan 10" it would recognize that I am trying to give myself cred and throw a message instead of adding the cred to my database value. The issue is that I don't want everyone to have to memorize each other's full username since it involves characters and a random 4 numbers and some of the members of the server have nicknames. That is why I have our first names in the database so that we can compare it to the name string that is passed into the command. I tried to use a 2D array and have it check my ctx.author which would spit out my "NMShoe#xxxx" but I can't figure out how to basically compare all three variables to each other in an if statement.
This only includes this specific method and the variables that coincide.
FIX:
name_to_username = {
"Nathan": "NMShoe#xxxx",
"Shawn": "Jerlopo#xxxx",
"Kaylan": "Flinters#xxxx"}
@bot.command(name="givecred", description="Gives credit to a certain person.")
async def givecred(ctx, name: str, cred: int):
if name in name_to_username:
username = name_to_username[name] #this is the answer given by the below answer THANK YOU mackorone!!!
if ctx.author == bot.user:
return
else:
if name in db.keys():
if str(ctx.author) != username: #MUST MAKE cxt.author a string for some random reason
await ctx.send("Added {} cred to {}!".format(cred, name))
db[name] = db[name] + cred
await ctx.send("{}'s at {} cred now!".format(name, db[name]))
return
else:
await ctx.send("Can't give cred to yourself.")
return
else:
await ctx.send("Did not enter the correct variables. Please enter: '.givecred {name} {# of cred}'.")
return