Logic 如何在每次调用类时停止重置变量?

Logic 如何在每次调用类时停止重置变量?,logic,discord.py,Logic,Discord.py,对不起,标题不太清楚。总之,基本上我正在制作一个discord机器人,它保存每封邮件,并将这些邮件进行比较,以确定是否是垃圾邮件。这是我的密码: import discord from discord.ext import commands client = commands.Bot(command_prefix="!") @client.event async def on_ready(): print('Logged in as {0.user}'.for

对不起,标题不太清楚。总之,基本上我正在制作一个discord机器人,它保存每封邮件,并将这些邮件进行比较,以确定是否是垃圾邮件。这是我的密码:

import discord
from discord.ext import commands
 
client = commands.Bot(command_prefix="!")


@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))
    print("-"*16)

    game = discord.Game("Discord")
    await client.change_presence(status=discord.Status.online, activity=game)

# Spam Stopper
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    message1 = message
    spam_num = 1
    
    if spam_num == 1 and message == message1:
        message2 = message
        spam_num += 1

    elif spam_num == 2 and message == message1 and message == message2:
        message3 = message
        spam_num += 1
    
    elif spam_num >= 3 and message == message1 and message == message2 and message == message3:
        message.delete()
    
    else:
        spam_num = 0

client.run('token here')

现在,问题是我不能在这个函数之外设置变量,因为我得到一个错误,说变量在局部循环之外。那么,我该怎么做呢?

我建议你读一下。要解决您的问题,您应该将变量放在全局范围内,而不是局部范围内。当您想要重用它们时,请将局部变量全球化,以便您可以使用它来比较下一条消息。

我刚刚尝试过这样做,但现在它不起作用,也没有错误。我将spam_num移动到这里
client=commands.Bot(command_prefix=“!”).spam_num=0
。我忘了提到您需要全球化您的本地变量Show我会这样做吗?对不起,我已经有一段时间没有编写python了。请阅读我提供的链接。使用函数顶部的“全局变量”。