The place where you are trying to change the status of the bot is correct, the problem is the way of trying to create this loop is stopping your bot from working, as time.sleep
is not suitable for an asynchronous library such as discord.py. Instead you can use asyncio.sleep()
.
You could do something like this:
import asyncio
@client.event
async def on_ready():
# ...
# Here goes your code for on_ready()
# ...
while True:
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
asyncio.sleep(600)
Another way of doing this would be using discord.ext.tasks
, which are functions defined to be running background tasks like the one you want to achieve.
from discord.ext import commands, tasks
import asyncio
# Your code here
@loop(seconds=600)
async def status_change():
# ...
# Some code to define playingStatus and watchingStatus arrays
# ...
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
status_change.before_loop(client.wait_until_ready())
status_change.start()
client.run("TOKEN")
References:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…