【发布时间】:2017-12-06 13:26:42
【问题描述】:
我正在尝试为我的机器人创建一个函数来保存引号。基本上是通过用户调用命令quote然后保存引用,例如:
/quote @test 我是最棒的
该程序设法保存引号没有问题,但是当它遇到 client.send_message() 或 client.say() (我都尝试过)时,它似乎完全忽略它们并继续其余的代码不会引发错误或发送消息。我已经尝试在命令的开头同时添加一个 send_message 和一个仅包含一个字符串的 say,但它们仍然不发送任何内容(而其他命令完全没有问题)。
命令:
@bot.command()
async def quote(user : discord.Member, *words):#gets the user and then makes a list of all other words
print(user.id)#a string of numbers
print(testserver.get_member(user.id) == user)#evaluates to True, this is the reason i use user.id, so that its possible to create a member some other time.
quoteshelf = shelve.open('quotes')
quote = ''
for i in range(len(words)):
quote = quote + ' ' + words[i]
bot.say('hi')#tests which have not been working
bot.send_message(testserver, 'bye')
if user.id in quoteshelf.keys():#checks if the user already exists in the file
tempshelf = quoteshelf[user.id]#it isnt possible to append directly into the shelf (or at least it seems so)
tempshelf.append({quote : getdate()})
quoteshelf[user.id] = tempshelf
bot.say('work pls')
bot.send_message(user.server, 'Added: ' + ' to')
print(1)
print(quoteshelf[user.id])
else:#if it isnt in the file then it will create a new one
quoteshelf[user.id] = [{quote : getdate()}]
print(2)
print(quoteshelf[user.id])
print(user.server)
print(user.server == testserver)
bot.say('ok sure')
bot.send_message(user.server, 'Created directory for and added ')
print(2)
quoteshelf.close()
这些打印主要用于调试(我想我会保留它们,因为它们可能有助于理解这个命令在做什么。
我希望这可以保存带有货架的报价,然后向频道发送简短的确认,但是,第二部分我似乎无法完成。
所以,是的,我很欣赏任何正确方向的指示,如果您需要了解更多信息,请告诉我。谢谢!
(编辑) 清理后的版本:
@bot.command()
async def quote(user : discord.Member, *words):
quoteshelf = shelve.open('quotes')
quote = ''
for i in range(len(words)):
quote = quote + ' ' + words[i]
if user.id in quoteshelf.keys():
tempshelf = quoteshelf[user.id]
tempshelf.append({quote : getdate()})
quoteshelf[user.id] = tempshelf
bot.say(user.server, 'Added: ' + quote + ' to ' + user.name)
else:
quoteshelf[user.id] = [{quote : getdate()}]
bot.say('Created directory for ' + user.name + ' and added ' + quote)
quoteshelf.close()
【问题讨论】:
标签: python python-3.x discord.py