AttributeError: 'NoneType' object has no attribute 'mention'

AttributeError: 'NoneType' object has no attribute 'mention'

出现奇怪的错误。此代码在我所在的 1 个服务器中有效,但在其他服务器中尝试它会给我这个错误:

回溯(最后一次调用): 文件 "c:/Desktop/test bot/cash.py",第 32 行,在 background_loop 中 await client.send_message(channel, "{} 保护袋子。${} 在里面找到。".format(x.user.mention, loot)) AttributeError: 'NoneType' 对象没有属性 'mention'

client = discord.Client()

async def background_loop():
    await client.wait_until_ready()
    channel = client.get_channel("479919577279758340")
    while not client.is_closed:
        loot = random.randint(25,50)
        emojigrab = ''
        emojimsg = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsg, "")
        x = await client.wait_for_reaction(emoji="", message=emojimsg,
                                             check=lambda reaction, user: user != client.user)
        if x:
            await client.delete_message(emojimsg)
            await client.send_message(channel, "{} secures the bag. {} found inside. ".format(x.user.mention, loot))
            add_dollars(x.user, loot)
            await asyncio.sleep(1800)

try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

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()

def add_dollars_stolen(user: discord.User, dollars_stolen: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars_stolen"] = cash[id].get("dollars_stolen", 0) + dollars_stolen
    print("{} stole ${}".format(user.name, cash[id]["dollars_stolen"]))
    save_cash()

def get_dollars_stolen(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars_stolen", 0)
    return 0

def get_dollars(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars", 0)
    return 0

@client.event
async def on_ready():
    print('Bot Online.')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.lower().startswith("?cash"):
            if message.content.lower() == "?cash":
                if get_dollars(message.author) < 0:
                    await client.send_message(message.channel, "You're ${} in the hole!".format(get_dollars(message.author)))
                else:
                    await client.send_message(message.channel, "You've acquired ${}! :dollar:".format(get_dollars(message.author)))


client.loop.create_task(background_loop())

您检查是否定义了 x,但没有定义 x.user 以及它是否是有效的用户对象。你得到的错误告诉你 x.user 当前是 None.

您可以确保用户不是 None,方法是:

if x and x.user is not None:  # or just: if x and x.user
    await client.delete_message(emojimsg)
    ...