尝试将给定的用户名与二维数组进行比较以确保它们不匹配

Trying to compare a given username to a 2D array to make sure that they don't match

所以我正在制作一个 discord 机器人,它将为我的服务器中的每个人记录一定的“信誉”,我们可以通过一个简单的命令来增加或减少某些人的信誉。到目前为止,我遇到的唯一问题 运行 是我不希望有人能够给自己带来信誉。将信誉视为某人对您的尊重程度。这纯粹是个玩笑,但我和我的朋友觉得这将是一个很酷的小项目,可以磨练我们的 python 技能。

这是我们的代码:

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 

但我基本上需要在第一个 else 语句的开头添加一个 if 语句,以确保如果我说“.givecred Nathan 10”,它会识别出我是在试图给自己信誉并抛出一条消息将信用添加到我的数据库值。问题是我不希望每个人都必须记住彼此的完整用户名,因为它涉及字符和随机的 4 个数字,并且服务器的一些成员有昵称。这就是为什么我在数据库中有我们的名字,以便我们可以将它与传递到命令中的名称字符串进行比较。我尝试使用二维数组并让它检查我的 ctx.author,这会吐出我的“NMShoe#xxxx”,但我无法弄清楚如何在 if 语句中基本上将所有三个变量相互比较。

这仅包括此特定方法和重合的变量。

修复:

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

很棒的项目创意!我认为您正在寻找 dict 数据结构,它允许您定义从一个值到另一个值的映射。而不是这个:

dblist = ["NMShoe#xxxx", "Nathan"], ["Jerlopo#xxxx", "Shawn"],["Flinters#xxxx", "Kaylan"] #x's are numbers

你应该这样写:

name_to_username = {
    "Nathan": "NMShoe#xxxx",
    "Shawn": "Jerlopo#xxxx",
    "Kaylan": "Flinters#xxxx",
}

然后可以这样检查名字是否正确:

if name in name_to_username:
    username = name_to_username[name]
    # ... other stuff here

有帮助吗?