Проблема с кодом Discord ботом: AttributeError: 'coroutine' object has no attribute 'json'

Рейтинг: 0Ответов: 1Опубликовано: 31.03.2023

Очень нужна помощь, пытался сделать прогноз погоды, но компилятор игнорирует команду и выдает ошибку.

Ошибка в консоли:

discord.ext.commands.bot Ignoring exception in command weather
Traceback (most recent call last):
  File "/home/runner/GlaringSteepArchives/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 229, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 33, in weather
    response = requests.get(url).json()
AttributeError: 'coroutine' object has no attribute 'json'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/GlaringSteepArchives/venv/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/GlaringSteepArchives/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 1023, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "/home/runner/GlaringSteepArchives/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 238, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'coroutine' object has no attribute 'json'
/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/events.py:80: RuntimeWarning: coroutine 'ClientSession._request' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Сам код:

   @bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

@bot.command()
async def weather(ctx, *, city: str):
    url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_API_KEY}&units=metric'
    response = requests.get(url).json()
    if response['cod'] == '404':
        await ctx.send(f'City "{city}" not found.')
    else:
        weather_desc = response['weather'][0]['description']
        temp = response['main']['temp']
        feels_like = response['main']['feels_like']
        humidity = response['main']['humidity']
        wind_speed = response['wind']['speed']
        weather_info = f'Weather in {city}: {weather_desc}\nTemperature: {temp}°C\nFeels like: {feels_like}°C\nHumidity: {humidity}%\nWind speed: {wind_speed} m/s'
        await ctx.send(weather_info)

Ответы

▲ 0

Попробуйте изменить

response = requests.get(url).json()

На

response = requests.get(url)