【问题标题】:AttributeError: 'NoneType' object has no attribute 'mention'AttributeError:“NoneType”对象没有“提及”属性
【发布时间】:2019-02-24 02:42:45
【问题描述】:

遇到一个奇怪的错误。这段代码在我所在的 1 台服务器上运行,但在其他服务器上尝试会出现此错误:

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

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 或者它是否为无。
  • 这种情况发生在用户对消息做出反应时,还是机器人对消息做出反应时?你可以检查check=lambda reaction, user: user and user != client.user
  • 当用户对消息做出反应时会发生这种情况。我添加了用户和用户!=客户端用户,它消除了错误,但它不会删除消息并添加新消息
  • 您是否将 Patrick 的建议添加到 wait_for_reaction 检查 lambda(这与我的回答不同,因为它应该阻止它认为它有反应,直到它成为有效用户)。
  • 嘿,我尝试了这两个建议。帕特里克之前帮助过我,因为它一直认为机器人正在对消息做出反应,所以他建议添加用户!= client.user,但是当我添加他的新建议时,它消除了错误,但它没有继续删除消息并添加新消息以及向 json 文件添加值。

标签: python python-3.x discord discord.py nonetype


【解决方案1】:

您检查是否定义了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)
    ...

【讨论】:

  • 感谢您的回复。我在发布之前尝试过,但是当消息得到反应时,它只是吐出另一个带有反应的“emojimsg”消息。
猜你喜欢
  • 1970-01-01
  • 2020-04-07
  • 2021-02-05
  • 2019-01-01
  • 2021-12-26
  • 2019-07-23
  • 2018-05-13
  • 2020-09-07
  • 2017-05-03
相关资源
最近更新 更多