首页 > 解决方案 > 我有一个 discord.py 问题,因为 time.sleep 停止了整个过程

问题描述

我最近一直在制作一个 discord.py 宠物机器人,但遇到了一个问题,time.sleep()即让整个机器人停止工作。

# bot.py
import os
import time
import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = ('Bot_ID')
GUILD={'Guild_Name'}
hunger=100#hits 0 in 24 hrs(-1 every 864 secs)
happy=100#hits 0 in 6 hrs(-1 every 216 secs)
thirst=100#hits 0 in 12 hrs(-1 every 432 secs)
clean=100#hits 0 in 48 hrs (-1 every 1728 secs)
health=100#the health dont let it hit 0...
life='alive'#Alive or Dead
p='p'

client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name==GUILD:
            break
        
    print(
        f'{client.user} is connected to thr following guild:\n'
        f'{guild.name}(id:{guild.id})'
    )

@client.event
async def on_message(message):
    global hunger
    global happy
    global thirst
    global clean
    global health
    global life
    global p
    if message.author==client.user:
        return
    if message.author.bot: return
    if message.content==(p+'Start'):
        re='Hello'
        await message.channel.send(re)
        while True:
            time.sleep(1728)
            clean-=1
            if clean<0:
                clean=0
                health-=1
            elif clean<26:
                await message.channel.send('im getting dirty')
            if health<26:
                await message.channel.send('<@&773151702827794463> Help Im Dying')
    if message.content==(p+'Start'):
        while True:
            time.sleep(864)
            hunger-=1
            if hunger<0:
                hunger=0
                health-=1
            elif hunger<26:
                await message.channel.send('im getting hungry')
            if health<26:
                await message.channel.send('<@&773151702827794463> Help Im Dying')
    if message.content==(p+'Start'):
        while True:
            time.sleep(432)
            thirst-=1
            if thirst<0:
                thirst=0
                health-=1
            elif thirst<26:
                await message.channel.send('im getting thirsty')
            if health<26:
                await message.channel.send('<@&773151702827794463> Help Im Dying')
    if message.content==(p+'Start'):
        while True:
            time.sleep(261)
            happy-=1
            if happy<0:
                happy=0
                health-=1
            elif happy<26:
                await message.channel.send('im getting sad')
            if health<26:
                await message.channel.send('<@&773151702827794463> Help Im Dying')
    if message.content==(p+'feed'):
        await message.channel.send('Thanks for the food')
        hunger=100
    if message.content==(p+'drink'):
        await message.channel.send('Thanks for the water')
        thirst=100
    if message.content==(p+'play'):
        await message.channel.send('Yay fun')
        happy=100
    if message.content==(p+'clean'):
        await message.channel.send('Nice and Clean')
        clean=100
    if message.content==(p+'pstats'):
        await message.channel.send('State:',alive)
        await message.channel.send('Health:',health)
        await message.channel.send('Hunger:',hunger)
        await message.channel.send('Thirst:',thirst)
        await message.channel.send('Cleanliness:',clean)
        await message.channel.send('Happiness:',happy)
    if health<0:
        await message.channel.send('<@&773151702827794463> Your Pet Has Died')
        health=0
        life='Dead'
        
    
            
            
        
client.run(TOKEN)
   

标签: python

解决方案


使用asyncio.sleep()

import asyncio

....
if message.content==(p+'Start'):
    re='Hello'
    await message.channel.send(re)
    while True:
        #time.sleep(1728)
        await asyncio.sleep(1728)
        ....

推荐阅读