Skip to content
Advertisement

How to get/request the api: https://ev.io/stats-by-un/(username)

I want to show in game stats when the player puts in $stats (username)

import Keep_alive
import tkinter as tk
import discord
import os
import asyncio
from discord import Embed
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt

bot = commands.Bot(command_prefix='$')

@bot.command()
async def test(ctx, arg):
  await ctx.send(arg)
@bot.command()
async def stats(ctx, arg):
  await ctx.send(arg)

@bot.command()
async def embed(ctx):
  embed=discord.Embed(
  title="Hello!",
  description="Im a embed text!")
  await ctx.send(embed=embed)

Keep_alive.Keep_alive()  
bot.run("token")

Advertisement

Answer

First, you need to request the api which you need the requests module.

import requests

Next, you need to know how to get the data from the requested api.

@bot.command()
async def stats(ctx, *, username):
    # This is how you request data from the api
    data = requests.get(f"https://ev.io/stats-by-un/{username}").json()
    # This is how you get certain stats from the data
    # I don't know what does this api request, so I'm just going to use kills as an example.
    # Different apis have different methods to get certain data, so it's better to take a look at their documentation.
    kills = data["kills"]
    await ctx.send(f"{username} has {kills} kills in total.")

I wouldn’t suggest using requests within asynchronous code since it’s blocking. discord.py uses aiohttp, so it should already be installed.

Here’s an example code using aiohttp and discord.py:

async with aiohttp.ClientSession() as cs:
    async with cs.get(f"https://ev.io/stats-by-un/{username}") as r:
        response = await r.json()
        await ctx.send(f"{username} has {response["kills"]} kills in total.)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement