Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有一个不和谐的问题。随着时间的推移,睡眠停止了一切_Python - Fatal编程技术网

Python 我有一个不和谐的问题。随着时间的推移,睡眠停止了一切

Python 我有一个不和谐的问题。随着时间的推移,睡眠停止了一切,python,Python,我最近制作了一个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

我最近制作了一个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)
   
#bot.py
导入操作系统
导入时间
进口不和
从dotenv导入加载\u dotenv
加载_dotenv()
令牌=('Bot_ID')
帮会={'GUILD_Name'}
饥饿=100#在24小时内达到0(-1/864秒)
快乐=100#在6小时内命中0(-1/216秒)
口渴=100#在12小时内达到0(-432秒1次)
clean=100#在48小时内命中0(-1/1728秒)
健康=100#健康不让它达到0。。。
活着还是死
p='p'
client=discord.client()
@客户端事件
_ready()上的异步定义:
对于client.guilds中的帮会:
如果guild.name==帮会:
打破
印刷品(
f'{client.user}已连接到以下公会:\n'
f'{guild.name}(id:{guild.id})'
)
@客户端事件
异步def on_消息(消息):
全球饥饿
全球快乐
全球渴望
全球清洁
全球卫生
全球生活
全局p
如果message.author==client.user:
返回
if message.author.bot:返回
如果message.content==(p+'Start'):
你好
等待消息。频道。发送(重新)
尽管如此:
时间。睡眠(1728)
清洁-=1
如果清洁请使用asyncio.sleep()


这正是时间。睡眠应该做的。这回答了你的问题吗?为此,我设想
asyncio
threading
模块将是最好的,因为我想将其全部保存在一个异步定义下,我不确定您将如何做到这一点。我的意思是你可以计算每个动作应该运行的时间,然后当这个时间到来或过去时(以防万一),你触发这个动作并计算一个新的时间?
import asyncio

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