Python 添加/删除“;XP";基于在特定时间范围内发送的消息

Python 添加/删除“;XP";基于在特定时间范围内发送的消息,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,人们通过滥发信息来获取“XP”(我使用了美元),如果用户不滥发信息,我尝试添加XP(美元),但如果他们滥发信息,则删除更多。现在代码正在增加美元,但似乎每次都增加+2美元,如果用户对频道进行垃圾邮件处理,则不会删除美元。我使用if not message.author.id==“111176943920152603”:来确保它是用户而不是机器人 client = discord.Client() user_timer = {} user_spam_count = {} rdollars = r

人们通过滥发信息来获取“XP”(我使用了美元),如果用户不滥发信息,我尝试添加XP(美元),但如果他们滥发信息,则删除更多。现在代码正在增加美元,但似乎每次都增加+2美元,如果用户对频道进行垃圾邮件处理,则不会删除美元。我使用
if not message.author.id==“111176943920152603”:
来确保它是用户而不是机器人

client = discord.Client()

user_timer = {}
user_spam_count = {}
rdollars = random.randint(0,3)

def save_cash():
with open("cash.json", "w+") as fp:
    json.dump(cash, fp, sort_keys=True, indent=4)

def add_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) + dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

def remove_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) - dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

@client.event
async def on_message(message):
    if not message.author.id == "111176943920152603":
    user_timer[message.author.id] = time.time()
            try:
                if time.time() >= user_timer[message.author.id] + 2:
                    user_spam_count[message.author.id] = 0
                    add_dollars(message.author, rdollars)

                if time.time() < user_timer[message.author.id] + 2:
                    user_spam_count[message.author.id] += 1
                    if user_spam_count[message.author] >= 4:
                        remove_dollars(message.author, 25)
                    if user_spam_count[message.author.id] >= 10:
                        remove_dollars(message.author, 50)

            except KeyError:
                add_dollars(message.author, rdollars)
client=discord.client()
用户\计时器={}
用户\u垃圾邮件\u计数={}
rdollars=random.randint(0,3)
def save_cash():
以open(“cash.json”、“w+”)作为fp:
dump(cash、fp、sort_keys=True、indent=4)
def add_$(用户:discord.user,美元:int):
id=user.id
如果身份证不是现金:
现金[id]={}
现金[id][“美元”]=现金[id]。获取(“美元”,0)+美元
打印(“{}现在有{}美元”。格式(user.name,cash[id][“美元]))
节省现金
def删除美元(用户:discord.user,美元:int):
id=user.id
如果身份证不是现金:
现金[id]={}
现金[id][“美元”]=现金[id]。获取(“美元”,0)-美元
打印(“{}现在有{}美元”。格式(user.name,cash[id][“美元]))
节省现金
@客户端事件
异步def on_消息(消息):
如果不是message.author.id==“111176943920152603”:
user\u timer[message.author.id]=time.time()
尝试:
如果time.time()>=user\u timer[message.author.id]+2:
用户\u垃圾邮件\u计数[message.author.id]=0
添加美元(message.author,rdollars)
如果time.time()=4:
删除美元(message.author,25)
如果用户\u垃圾邮件\u计数[message.author.id]>=10:
删除美元(message.author,50)
除KeyError外:
添加美元(message.author,rdollars)

您在中缺少
.id

if user_spam_count[message.author] >= 4:
    remove_dollars(message.author, 25)
这个

if user_spam_count[message.author.id] >= 4:
    remove_dollars(meesage.author,25) 
应该可以

由于这个原因,它一直捕获一个键错误,并添加钱,而不是进入if语句并删除它

这将解决您遇到的时间问题:

@client.event
async def on_message(message):
  if not message.author.id == "111176943920152603":
    try:
      last_post_time = user_timer[message.author.id]
      user_timer[message.author.id] = time.time()

      if user_timer[message.author.id] - last_post_time >= 2:
        user_spam_count[message.author.id] = 0
        add_dollars(message.author, rdollars)

      elif user_timer[message.author.id] - last_post_time < 2:
        user_spam_count[message.author.id] += 1
        if user_spam_count[message.author.id] >= 4:
          remove_dollars(message.author, 25)
        elif user_spam_count[message.author.id] >= 10:
          remove_dollars(message.author, 50)
        else:
          add_dollars(message.author, rdollars)

    except KeyError:
      user_timer[message.author.id] = time.time()
      user_spam_count[message.author.id]=0
      print(user_timer)
      add_dollars(message.author, rdollars)
@client.event
异步def on_消息(消息):
如果不是message.author.id==“111176943920152603”:
尝试:
上次发布时间=用户计时器[message.author.id]
user\u timer[message.author.id]=time.time()
如果用户计时器[message.author.id]-上次发布时间>=2:
用户\u垃圾邮件\u计数[message.author.id]=0
添加美元(message.author,rdollars)
elif用户计时器[message.author.id]-上次发布时间<2:
用户\u垃圾邮件\u计数[message.author.id]+=1
如果用户\u垃圾邮件\u计数[message.author.id]>=4:
删除美元(message.author,25)
elif用户\u垃圾邮件\u计数[message.author.id]>=10:
删除美元(message.author,50)
其他:
添加美元(message.author,rdollars)
除KeyError外:
user\u timer[message.author.id]=time.time()
用户\u垃圾邮件\u计数[message.author.id]=0
打印(用户计时器)
添加美元(message.author,rdollars)
检测某人是否在最后一条消息之后的某个时间段内发送消息的方法是检查当前消息的时间-最后一条消息的时间<期望的时间量

  • _消息的当前_time_为time.time()
  • _last_消息的time_是{userID:time}中的值 字典
  • 所需的时间量是它必须在2之间经过的时间量 以前的邮件不再被视为垃圾邮件
在本例中,您将其设置为2,而我将其保持原样

您一直在基于相同的值进行检查(您可以将字典时间设置为time.time(),并将其与下面两行的time.time()进行比较)

相反,最好将该值存储在变量中,并使用if/elif进行比较这是完整的代码吗?我看不出
save\u cash
函数是在哪里定义的。我也看不到
user\u timer
在哪里更新,因为它似乎只是你的
if
语句的一部分。哎呀,我确实忘记了user\u timer代码,我没有添加“save\u cash()”,因为我没有试图用代码lol来垃圾发送问题。谢谢你的回复!我把.id添加到
user\u spam\u count[message.author.id]>=4:
中,但现在似乎每次我发送一条消息,它都会增加3美元。当我发垃圾邮件时,它也不会删除任何美元。非常感谢您的解决方案!这是有道理的,我已经测试了一个小时了<代码>时间。时间()?我遇到了另一个问题,它会为用户发送的第一条消息增加美元,但之后的任何消息都不算作XP,它只会一直添加消息,直到用户达到用户\u垃圾邮件\u计数,然后开始扣除XP,而不是每次用户发送消息时在不达到用户\u垃圾邮件\u计数的情况下添加XP。我已经编辑了上面的代码,现在已修复该问题。这是
time.time()
的文档,谢谢!现在这一切都完美无瑕。我感谢你的知识和帮助。