2

Using discord.py and python:

Ok so basically I have this bot that updates the best prices for a certain game every minute. However, while I am doing that, other people cannot access the bot. For example, lets just say I have a command called "hello" that when called prints hello out in the chat. Since the code always runs, the user cant call the command hello because the code is too busy running the code that updates every minute. Is there any way to like make it so that the updateminute code runs while others can input commands as well?

import discord
import asyncio
import bazaar
from discord.ext import commands, tasks

client = commands.Bot(command_prefix = '.')


@client.command()
async def calculate(ctx):
    while True:
        await ctx.send(file2.calculate())
        await asyncio.sleep(210)

@client.command()
async def hello(ctx):
    await ctx.send("Hello")

client.run(token)

In file2.py:

def updateminute():
    for product in product_list:
       #Grab Api and stuff
       #check to see whether it is profitable
       time.sleep(0.3) #cause if i don't i will get a key error

    #calculate the stuff
    #return the result

To sum up, since the bot is too busy calculating updateminute and waiting, other people cannot access the bot. Is there any way I can try to fix this so that the bot calculates its stuff and so people can use the bots commands? Thanks!

2 Answers 2

1

You can look into threading! Basically, run two separate threads: one for taking requests and one for updating the prices.

Sign up to request clarification or add additional context in comments.

Comments

1

You could also look into turning it into an async function, essentially making it easier to run things concurrently. So your standard def will become async def and then to call the function you simply add an await before it so await file2.calculate()

Hope it helps and is also somewhat easier to understand

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.