Discord机器人接受对消息的反应

我仍在学习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'

这很奇怪,我将重新启动bot并执行命令使用。我将以竖起大拇指的表情符号做出反应,并回复“很棒!”不会产生任何错误。我将再次运行它,并以不赞成或不赞成的方式答复,并发生上述错误。它似乎是第一次工作,但是第二次却出错了。即使我在不​​久后重新启动bot,它也会失败。如果我在重新启动之前稍等片刻,然后再试一次,则该漫游器将运行一次,然后每次都失败,并出现相同的问题。我不确定是什么问题。我查看了其他一些似乎无法解决该问题的线程。

非常感谢您提前提供的帮助!

yjy611 回答:Discord机器人接受对消息的反应

发生错误的原因是该漫游器正在尝试DM本身。

Discord.py的wait_for函数接受check kwarg,该kwarg允许您过滤特定事件。如果您不包括此检查,则库将一直等到从任何地方出现下一个该类型的事件(在这种情况下为reaction_add)。在您的特定情况下,碰巧是机器人添加了反应:

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)
本文链接:https://www.f2er.com/3084197.html

大家都在问