I want to show in game stats when the player puts in $stats (username)
JavaScript
x
30
30
1
import Keep_alive
2
import tkinter as tk
3
import discord
4
import os
5
import asyncio
6
from discord import Embed
7
from discord.ext import commands
8
from discord.ext.commands import bot
9
import asyncio
10
import datetime as dt
11
12
bot = commands.Bot(command_prefix='$')
13
14
@bot.command()
15
async def test(ctx, arg):
16
await ctx.send(arg)
17
@bot.command()
18
async def stats(ctx, arg):
19
await ctx.send(arg)
20
21
@bot.command()
22
async def embed(ctx):
23
embed=discord.Embed(
24
title="Hello!",
25
description="Im a embed text!")
26
await ctx.send(embed=embed)
27
28
Keep_alive.Keep_alive()
29
bot.run("token")
30
Advertisement
Answer
First, you need to request the api which you need the requests module.
JavaScript
1
2
1
import requests
2
Next, you need to know how to get the data from the requested api.
JavaScript
1
10
10
1
@bot.command()
2
async def stats(ctx, *, username):
3
# This is how you request data from the api
4
data = requests.get(f"https://ev.io/stats-by-un/{username}").json()
5
# This is how you get certain stats from the data
6
# I don't know what does this api request, so I'm just going to use kills as an example.
7
# Different apis have different methods to get certain data, so it's better to take a look at their documentation.
8
kills = data["kills"]
9
await ctx.send(f"{username} has {kills} kills in total.")
10
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:
JavaScript
1
5
1
async with aiohttp.ClientSession() as cs:
2
async with cs.get(f"https://ev.io/stats-by-un/{username}") as r:
3
response = await r.json()
4
await ctx.send(f"{username} has {response["kills"]} kills in total.)
5