亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python Discord.py `time.sleep()` 協程

Python Discord.py `time.sleep()` 協程

拉風的咖菲貓 2022-10-05 17:07:19
import discordimport osimport randomimport timeimport mathclient = discord.Client()with open('admins.conf', 'r') as f:    for line in f.readlines():        exec(line)with open('bans.conf', 'r') as f:    for line in f.readlines():        exec(line)with open('coins.conf', 'r') as f:    for line in f.readlines():        exec(line)random.seed(os.urandom(32))searchusers = []@client.eventasync def on_ready():    '''Notification on ready.'''    print('Logged in! Bot running.')    await client.change_presence(activity=discord.Game(name='/help'))def getcoins(uid):    '''Get the amount of coins, if nonexistent set to 0.'''    try:        return coins[uid][0]    except Exception:        coins[uid] = [0, time.time()+20]        return 0def mention_to_uid(mention):    '''Extract UID from a mention'''    uid = mention[2:-1]    if uid[0] == '!':        uid = uid[1:]    return uiddef setcoins(uid, value):    '''Set the amount of coins someone has.'''    try:        coins[uid][0] = value    except Exception:        coins[uid] = [value, time.time()+20]    with open('coins.conf','w') as f:        f.write('coins = '+repr(coins))@client.eventasync def on_message(message):    '''Main bot code running on message.'''    if message.author == client.user:        return    if message.author.id in bans:        return    if message.content.startswith('/') or message.content.startswith('&'):        user = message.author.id        text = message.content[1:].strip()        command = text.split(' ')[0]        subcommand = text.split(' ')[1:]        if message.author.id in searchusers:            await message.channel.send('<@'+str(message.author.id)+'>, you cannot use bot commands while you are searching.')            return在 time.sleep() 事件期間,其他機器人命令在 sleep 函數通過之前不會注冊。例如,在執行 /search 后立即執行 /help 之類的命令,機器人將在 1 分鐘后才會響應,此時它會響應 /help 并且消息還剩 1 分鐘。我嘗試在每個睡眠功能之前堅持“等待”,但它只是向我吐出運行時警告并停止執行(它只是說你開始搜索然后什么也沒發生)。
查看完整描述

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

time.sleep()停止程序的整個執行。如果您只想延遲一個異步響應線程,請使用asyncio.sleep


例子:


import asyncio


async def wait():

    await asyncio.sleep(5)

    print('I waited 5 seconds')


查看完整回答
反對 回復 2022-10-05
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號