2017-05-29 138 views
0

所以这样做是什么时候,只要有人键入命令!test进入聊天室频道,它会打印出适当的下面的字符串到聊天频道。不过,我想将在同一时间只能使用一次的命令,所以我要锁定的命令,直到它完成:不知道为什么`asyncio.Lock`不工作

import discord, asyncio, time 

client = discord.Client() 

@client.event 
async def on_message(message): 
    lock = Lock() # define Lock 
    if message.content.lower().startswith("!test") and not lock.locked(): 
     lock.acquire() # Lock the !test command so it can't be used now 
     await client.send_message(message.channel,'test1rgews') 
     await asyncio.sleep(1) 
     await client.send_message(message.channel,'test2thewf') 
     await asyncio.sleep(1) 
     await client.send_message(message.channel,'test3rhtvw') 
     await asyncio.sleep(1) 
     await client.send_message(message.channel,'test4trjyr') 
     await asyncio.sleep(1) 
     await client.send_message(message.channel,'test5dmuye') 
     await asyncio.sleep(10) 
     lock.release() # Unlock the !test command now 

client.run('clienttokenhere') 

,但我得到的是说NameError: name 'Lock' is not defined,即使我没有把它定义为一个错误lock = Lock()

+0

请不要破坏你的帖子。一旦你发布了一个问题,你已经将内容授权给了Stack Overflow社区(在CC-by-SA许可下)。如果您想取消关联此帐户与您的帐户关联,请参阅[解除请求的正确途径是什么?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-换一个 - 解离 - 请求)。 – FelixSFD

回答

0

导入模块以使用它们的名称是不够的。您必须使用asyncio.Lockfrom asyncio import Lock

相关问题