Discord 机器人接受对消息的反应
Discord bot accepting reaction to message
我仍在学习 discord.py 库,所以对于任何菜鸟错误我深表歉意。
下面的代码是执行 3 个操作的函数的一部分:
- 连接到
sqlite3
数据库
- 询问要创建什么 kitname 并将新的 kitname 插入
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'
奇怪的是我将重启机器人并完成命令使用。我会用竖起大拇指的表情符号做出反应,它会用 "Great!" 回复而不会产生任何错误。我将 运行 第二次通过它并以不赞成或赞成的方式回复,然后出现上述错误。它似乎第一次工作,但第二次就出错了。即使我在不久之后重新启动机器人,它也会失败。如果我在重新启动之前等待一段时间然后再试一次,机器人将工作一次,然后每次都因同样的问题而失败。我不确定是什么问题。我查看了其他一些似乎可以解决该问题但无济于事的线程。
非常感谢任何提前的帮助!
你的错误的原因是机器人试图自己私信。
Discord.py 的 wait_for
函数接受一个 check kwarg,允许您过滤特定事件。如果您不包括此检查,那么库将等到来自任何地方的该类型的下一个事件(在本例中为 reaction_add)。在您的具体情况下,恰好是机器人添加了反应:
await addCards.add_reaction('')
await addCards.add_reaction('')
对此的一个简单解决方法是编写一个检查函数,它只会 return 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)
我仍在学习 discord.py 库,所以对于任何菜鸟错误我深表歉意。
下面的代码是执行 3 个操作的函数的一部分:
- 连接到
sqlite3
数据库 - 询问要创建什么 kitname 并将新的 kitname 插入
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'
奇怪的是我将重启机器人并完成命令使用。我会用竖起大拇指的表情符号做出反应,它会用 "Great!" 回复而不会产生任何错误。我将 运行 第二次通过它并以不赞成或赞成的方式回复,然后出现上述错误。它似乎第一次工作,但第二次就出错了。即使我在不久之后重新启动机器人,它也会失败。如果我在重新启动之前等待一段时间然后再试一次,机器人将工作一次,然后每次都因同样的问题而失败。我不确定是什么问题。我查看了其他一些似乎可以解决该问题但无济于事的线程。
非常感谢任何提前的帮助!
你的错误的原因是机器人试图自己私信。
Discord.py 的 wait_for
函数接受一个 check kwarg,允许您过滤特定事件。如果您不包括此检查,那么库将等到来自任何地方的该类型的下一个事件(在本例中为 reaction_add)。在您的具体情况下,恰好是机器人添加了反应:
await addCards.add_reaction('')
await addCards.add_reaction('')
对此的一个简单解决方法是编写一个检查函数,它只会 return 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)