How do I create a custom cycling status in discord.py?

finalerock44

I have a discord bot that I'm using to learn how to use the API and to practise my python knowledge. I'm trying to create a system that randomly chooses a status and applies it then waits a short time, currently 10 minutes, then continue like that infinitely until the bot is shutdown. Currently, I have the loop in my on_ready() function but the bot shuts down after a short time (less than 10 minutes, around 2-3) and I can't figure out why. The current code is this:

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))
  infiniteTime = 1
  while infiniteTime != 20:
    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]))
    time.sleep(600)

watchingStatus and listeningStatus are 1D arrays with 11 strings in. Any assistance/feedback is appreciated as I'm new to the API. Cheers!

ETA: Should probably clarify that I've realised that having the code in the on_ready() function means that it never ends and so can't do anything else. I'm asking because I don't know why the bot shuts-down on Discord's end without me stopping the code and I don't know where else I should put this loop as I'm new to this async/await system and the way the API functions.

Shunya

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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Discord.py Custom Status

From Dev

Discord.js How would I create a bot status announcement?

From Dev

How do I make an ordered rotating status for a discord bot?

From Dev

How do i set my discord bot's status and presence?

From Java

How do I stop the execution of a command in discord.py

From Dev

Discord.py How do I make my function loop?

From Dev

How do I create a custom element in dart?

From Dev

How do I create a custom nonlinear filter?

From Dev

How do I create a custom array in powershell?

From Dev

How do I move a user to a specific channel on discord using the discord.py API

From Dev

How do I write custom renderers with web.py

From Dev

How can I check the role of a message author + How do I DM specific roles on Discord.py?

From Dev

How do I create a custom cursor with a custom hotspot in wxPython?

From Dev

How do I create an instance of a model in views.py?

From Dev

How to set number of servers that the bot is on as status (discord.py rewrite)?

From Dev

discord.py status in a cog

From Dev

How do I change the color only once upon mousePressed (stop it from cycling)?

From Java

discord.py (rewrite) How do I make a command to a specific channel

From Dev

How do i make it so if theres no one mentioned it returns with a value discord.py

From Dev

How do I check if the same user replies to my discord.py bot

From Dev

How do I use discord.py to send a greeting message in the System Messages Channel

From Dev

How do I send a direct message to someone when they join a server with discord.py

From Dev

How do I get a counter to work within a discord.py command function?

From Dev

In discord.py, How do I use add_roles to add multiple roles to one person?

From Dev

How to create a custom order status in woocommerce

From Dev

How to log custom status updates using discord.js?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I properly create custom text codecs?

From Dev

How do I create a custom control that accepts <Run/> text?

Related Related

  1. 1

    Discord.py Custom Status

  2. 2

    Discord.js How would I create a bot status announcement?

  3. 3

    How do I make an ordered rotating status for a discord bot?

  4. 4

    How do i set my discord bot's status and presence?

  5. 5

    How do I stop the execution of a command in discord.py

  6. 6

    Discord.py How do I make my function loop?

  7. 7

    How do I create a custom element in dart?

  8. 8

    How do I create a custom nonlinear filter?

  9. 9

    How do I create a custom array in powershell?

  10. 10

    How do I move a user to a specific channel on discord using the discord.py API

  11. 11

    How do I write custom renderers with web.py

  12. 12

    How can I check the role of a message author + How do I DM specific roles on Discord.py?

  13. 13

    How do I create a custom cursor with a custom hotspot in wxPython?

  14. 14

    How do I create an instance of a model in views.py?

  15. 15

    How to set number of servers that the bot is on as status (discord.py rewrite)?

  16. 16

    discord.py status in a cog

  17. 17

    How do I change the color only once upon mousePressed (stop it from cycling)?

  18. 18

    discord.py (rewrite) How do I make a command to a specific channel

  19. 19

    How do i make it so if theres no one mentioned it returns with a value discord.py

  20. 20

    How do I check if the same user replies to my discord.py bot

  21. 21

    How do I use discord.py to send a greeting message in the System Messages Channel

  22. 22

    How do I send a direct message to someone when they join a server with discord.py

  23. 23

    How do I get a counter to work within a discord.py command function?

  24. 24

    In discord.py, How do I use add_roles to add multiple roles to one person?

  25. 25

    How to create a custom order status in woocommerce

  26. 26

    How to log custom status updates using discord.js?

  27. 27

    Laravel: How do I create a custom pivot model?

  28. 28

    How do I properly create custom text codecs?

  29. 29

    How do I create a custom control that accepts <Run/> text?

HotTag

Archive