【发布时间】:2020-03-13 08:20:24
【问题描述】:
我仍在学习 discord.py 库,因此对于任何新手错误,我深表歉意。
下面的代码是执行 3 个动作的函数的一部分:
- 连接到
sqlite3数据库 - 询问要创建的套件名称并将新套件名称插入
sqlite3数据库 - 询问用户是否想在他们的套件名称中添加卡片。
#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