0
votes
import discord
from discord.ext import commands, tasks
import datetime
import requests
import time
from bs4 import BeautifulSoup


client = discord.Client()

r = requests.get("https://www.worldometers.info/coronavirus/country/italy/")
s = BeautifulSoup(r.text, "html.parser")
data = s.find_all("div",class_ = "maincounter-number")

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))




@tasks.loop(seconds=50.0)
async def covid():
    x = datetime.datetime.now()
    d = x.strftime("%M")
    if d == "21":
        channel = bot.get_guild(guild).get_channel(channel)
        await message.channel.send("casi di coronavirus in italia: \ncasi totali: " + 
                           data[0].text.strip()
                           + "\nmorti totali: " + data[1].text.strip()
                           + "\nguariti totali: " + data[2].text.strip())

@covid.before_loop
async def before_printer(self):
    print('waiting...')
    await self.bot.wait_until_ready()

@covid.after_loop
async def post_loop(self):
  if self.covid.failed():
    import traceback
    error = self.covid.get_task().exception()
    traceback.print_exception(type(error), error, error.__traceback__)


client.run('token)

basically this code checks if it is a specified time and if it is it sends a message with the covid data of italy but it doesn't work and doesn't return anything i tried even adding an error handler (traceback) and doesn't do anything the only output i have is from async def on_ready()

so i tried this:

data = ''
@tasks.loop(seconds=50.0)
async def covid(ctx):
    global data
    x = datetime.datetime.now()
    d = x.strftime("%M")
    if d == "21":
        data = "casi di coronavirus in italia: \ncasi totali: " +                             
        data[0].text.strip() + "\nmorti totali: " + data[1].text.strip()
        return data

@covid.before_loop
async def before_printer(self):
    print('waiting...')
    await self.bot.wait_until_ready()

@client.command()
async def get_covid(ctx):
    global data
    await ctx.send(data)

but i don't get any output and if i add @client.command() before async def covid it gives me this error: Traceback (most recent call last): File "C:\Users\danie\OneDrive\Desktop\test.py", line 26, in async def covid(ctx): File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site- packages\discord\ext\commands\core.py", line 1162, in decorator result = command(*args, **kwargs)(func) File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 1317, in decorator return cls(func, name=name, **attrs) File "C:\Users\danie\AppData\Local\Programs\Python\Python38\lib\site- packages\discord\ext\commands\core.py", line 210, in init raise TypeError('Callback must be a coroutine.') TypeError: Callback must be a coroutine. >>>

1
Pizza Pasta MandolinoGhostino

1 Answers

0
votes

You need to add a context in the function and @bot.command() decorator followed by your instance.

Likewise:

@client.command()
async def test(ctx):
    await ctx.send('PASSED')

a message is an event and you need to add a proper listener for it to work. You don't need to use this event unless you need the text data.

In your case, you are sending the data directly from your COVID-19 function which doesn't do anything since the message is not defined. It would be as:

#STORE THE VALUE OF COVID INSIDE DATA VARIABLE.
data = ''
@tasks.loop(seconds=50.0)
async def covid():
    global data
    x = datetime.datetime.now()
    d = x.strftime("%M")
    if d == "21":
        #Assign the value to DATA VARIABLE.
        data = "casi di coronavirus in italia: \ncasi totali: " + data[0].text.strip() + "\nmorti totali: " + data[1].text.strip()
        #Return the MODIFIED Data
        return data

Now send the data with this function.

@client.command()
async def get_covid(ctx):
    global data
    await ctx.send(data)

Also, You defined the client wrongly. client = discord.Client() It should be as:

# change '!' to any prefix you would like to use.
client = commands.Bot(command_prefix ='!')