【问题标题】:Discord bot accepting reaction to messageDiscord 机器人接受对消息的反应
【发布时间】:2020-03-13 08:20:24
【问题描述】:

我仍在学习 discord.py 库,因此对于任何新手错误,我深表歉意。

下面的代码是执行 3 个动作的函数的一部分:

  1. 连接到sqlite3 数据库
  2. 询问要创建的套件名称并将新套件名称插入sqlite3 数据库
  3. 询问用户是否想在他们的套件名称中添加卡片。
 #To create a new kit
@client.command(name="createkit")
async def createkit(message):
    author = message.author
    await author.send("What name would you like to give the new kit?")
    msg = await client.wait_for('message')
    kitName = msg.content #name of kit user wants to make
    userID = msg.author.id #User ID of the author of the reply message
    userName = msg.author.name #Username of the author who wrote the reply message
    db = sqlite3.connect('kits.sqlite')
    cursor = db.cursor()
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS kits(
    DeckID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
    User TEXT NOT NULL,
    UserID INTEGER NOT NULL,
    Deckname TEXT NOT NULL
    )
    ''')
    print("Connected to Kits")
    cursor.execute(f"SELECT * FROM kits WHERE UserID = {userID}")
    sql = ("INSERT INTO kits(User, UserID, Deckname) VALUES(?,?,?)")
    val = (userName, userID, kitName)
    cursor.execute(sql, val)
    db.commit()
    await author.send(f"{kitName} has been created!")
    addCards = await author.send(f"Would you like to add cards to {kitName}?")
    await addCards.add_reaction('????')
    await addCards.add_reaction('????')
    reaction, user = await client.wait_for('reaction_add')
    if user == client.user:
        return
    elif str(reaction.emoji) == '????':
        print(user)
        await user.send('Great!') #<-- error
        print("Replied with thumbs up!")
    elif str(reaction.emoji) == '????':
        await user.send('Too bad...') #<-- error
        print("Replied with thumbs down!")
    cursor.close()
    db.close()```

第 1 部分和第 2 部分可以正常运行。第 3 部分要求用户用竖起大拇指或不喜欢表情符号做出反应,引发以下错误:

discord.ext.commands.errors.CommandInvokeError: 
Command raised an exception: AttributeError: 
'ClientUser' object has no attribute 'send'

奇怪的是我将重新启动机器人并完成命令使用。我会用竖起大拇指的表情做出反应,它会回复“太棒了!”不产生任何错误。我将第二次运行它并以大拇指向下或大拇指向上回复,并发生上述错误。它似乎第一次工作,但第二次出错了。即使我不久之后重新启动机器人,它也会失败。如果我在重新启动之前等待一段时间然后再试一次,机器人将工作一次,然后每次都失败,出现同样的问题。我不确定问题是什么。我查看了其他一些似乎无济于事的问题。

非常感谢您提前提供任何帮助!

【问题讨论】:

  • 尝试在wait_for 之后打印出user 对象,看看它是什么类型。也许它并不总是相同的类型,正如错误所说,ClientUser 对象没有send 方法
  • @HymnsForDisco 看起来它正在以用户身份打印机器人,这可以解释为什么它不能发送,我想......我在elif str(reaction.emoji)... 行之前添加了if user == client.user: return,我仍然得到同样的问题。它工作了一次,然后尝试失败。也许是支票?我对那些完全陌生。
  • 请包含整个函数。
  • @PatrickHaugh 已编辑以显示整个函数。提前感谢您的帮助。

标签: python discord.py discord.py-rewrite


【解决方案1】:

您的错误的原因是机器人正在尝试 DM 自己。

Discord.py 的 wait_for 函数接受 check kwarg,允许您过滤特定事件。如果您不包括此检查,则库将等待来自任何地方的该类型的下一个事件(在本例中为反应添加)。在您的特定情况下,它恰好是添加反应的机器人:

await addCards.add_reaction('?')
await addCards.add_reaction('?')

一个简单的解决方法是编写一个检查函数,如果其他人添加了该反应,它将只返回True

def check(reaction,user):
    return not user.bot and reaction.message.id == addCards.id and str(reaction.emoji) in ['?','?']

reaction, user = await client.wait_for('reaction_add',check=check)

【讨论】:

  • 太棒了!这就是我预期的问题。我对支票还不是很熟悉。学习的好时机。谢谢!
猜你喜欢
  • 2017-11-01
  • 2020-08-12
  • 1970-01-01
  • 2020-07-29
  • 2019-09-16
  • 1970-01-01
  • 2020-02-23
  • 2019-09-26
  • 2020-07-28
相关资源
最近更新 更多