我正在尝试为我的机器人创建一个函数以保存报价。基本上通过向用户调用命令引用然后保存要保存的引用,例如:
/ quote @test我是最好的
该程序设法保存引号没有问题,但是当它遇到client.send_message()或client.say()(我已经尝试过两者)时,它似乎完全忽略它们并继续其余的代码没有引发错误或发送消息。我已经尝试将send_message放在一个只有一个字符串的说法的开头,但它们仍然没有发送任何东西(而其他命令根本没有问题)。
命令:
@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()
答案 0 :(得分:0)
男孩我觉得愚蠢的atm,我才意识到我已经忘记等待每一个人之前。所以,是的,如果有人有类似的问题,请确保你没有忘记等待......