-1

I have a function that gets the top 10 cryptocurrencies by market cap

import os 
import json
from requests import Session
def get_top10():
    CMC_API_KEY=os.environ.get('CMC')
    parameters={
                
                'start':1,
                'limit':5000
                }
    headers={
                'Accepts':'application/json',
                'X-CMC_PRO_API_KEY':CMC_API_KEY
                }
    session=Session()
    session.headers.update(headers)
        
    url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/map'
    response=session.get(url,params=parameters)
    json_file=json.loads(response.text)['data']
    top10_rank_to_slug = {d['rank']: d['slug'] for d in json_file}
    top_10 = sorted(top10_rank_to_slug.items())[:10]
    for rank,name in top_10:
        print(rank,name)

but in order to pass it to my discord bot the function has to return a value not print it how can I do that (I want the bot to loop through the list then return the items one by one I dont want the bot to just return the list )

@client.command()
@commands.check(check_channel)
async def top10(ctx):
    await ctx.send(get_top10())
client.run(token)

how do you suggest I do that??

w4ss
  • 45
  • 4

1 Answers1

0

The first thing that comes to mind for me is to return the dictionary, then loop through the dictionary when sending the messages.

import os 
import json
from requests import Session
def get_top10():
    CMC_API_KEY=os.environ.get('CMC')
    parameters={'start':1,
                'limit':5000
               }
    headers={'Accepts':'application/json',
             'X-CMC_PRO_API_KEY':CMC_API_KEY
            }
    session=Session()
    session.headers.update(headers)
    
    url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/map'
    response=session.get(url,params=parameters)
    json_file=json.loads(response.text)['data']
    top10_rank_to_slug = {d['rank']: d['slug'] for d in json_file}
    top_10 = sorted(top10_rank_to_slug.items())[:10]
    return top_10

Then you can await ctx.send the dictionary by each key:value pair:

@client.command()
@commands.check(check_channel)
async def top10(ctx):
top_10 = get_top_10()
for key in top_10:
    await ctx.send(f"{key} is the key and {top_10[key]} is the value.")
           
client.run(token)

There are MANY other solutions and ways to do this, but this is frankly the least intrusive to your code and one of the easier to understand ones.

I want the bot to loop through the list then return the items one by one I dont want the bot to just return the list

I do not necessarily know why you want the above specifically, but if you share that, I may be able to help in a more suitable manner. Another simple solution is to turn the dictionary into a string in the function, then return the string already preformatted for the bot to send.

tkomasa
  • 26
  • 3
  • thank you it worked , but the bot is displaying the result one by one is there any way to store the file then display it directly like not sending 10 messages I want the bot to send one message that contains everything – w4ss Oct 28 '21 at 09:16
  • I would recommend taking a look [here](https://stackoverflow.com/questions/4547274/convert-a-python-dict-to-a-string-and-back) at this post. The other option is simply converting the dictionary it produces directly into a string, with each new `key : value` pair making a new line. Replace every comma with a `\n`. – tkomasa Oct 31 '21 at 18:26