Discord bot is sending multiple replies

Question:

I created this command for my discord bot to list the statistics of the covid-19 pandemic by country.
I use this package: https://pypi.org/project/covid/.
The code works in a way that it replies the stats. But my problem is that it does it 9 times.
A screenshot of the replies

import discord
import os
from discord.ext.commands import Bot
from discord.ext import commands
import json
import requests
import asyncio
from sys import path as syspath
from os import path as ospath
from covid import Covid

client = discord.Client()
#some stuff

@client.command()
async def covid(ctx, loc):
    covid = Covid()
    cases = covid.get_status_by_country_name(loc)

    for i in cases:

        await ctx.reply(f"{cases['country']}nActive cases: {cases['active']}nConfirmed cases: {cases['confirmed']}nDeaths: {cases['deaths']}")

I also tried this code:

@client.command()
async def covid(ctx, loc):
    covid = Covid()
    cases = covid.get_status_by_country_name(loc)

    for i in cases:

        await ctx.reply(f"{i['country']}nActive cases: {i['active']}nConfirmed cases: {i['confirmed']}nDeaths: {i['deaths']}")

But when I try the command it returns the error:

string indices must be integers

I would be grateful for any help! Thanks a lot!

Asked By: GergÅ‘ Novák

||

Answers:

According to covid’s documentation, looks like you dont have to loop through the dictionary keys!
that’s why it sent it 9 times, the dictionary has 9 keys!

to fix this, please change your code to this:

@client.command()
async def covid(ctx, loc):
    covid = Covid()
    cases = covid.get_status_by_country_name(loc)

    await ctx.reply(f"{cases['country']}nActive cases: {cases['active']}nConfirmed cases: {cases['confirmed']}nDeaths: {cases['deaths']}")
Answered By: Adam75752