проблема с выводом информации из API
Бот должен парсить информацию с https://api.wynncraft.com/v2/player/PlayRW/stats но вместо PlayRW должен быть ник, который человек впишет после $stats. Бот все время выводит No data avaible.
отрывок из кода main.py:
import discord
from discord.ext import commands
import parsing
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
client = discord.Client(intents=intents)
@client.event
async def on_message(message): #таб есть если что
if message.content.startswith(f'$stats'):
word = message.content.split("$stats", 1)[1]
try:
await message.channel.send(f'**Stats of {word}:** ' + "```" + str(parsing.stats(word))[:1500].replace(',', '\n').replace(']', '\n').replace('}', '').replace("'", "").replace('{', '\n').replace('[', '\n') + "```")
except:
await message.channel.send('```Error 01: type $stats "name"```')
код parsing.py:
import requests
import json
import threading
def stats(argument):
threading.Timer(10.0, f).start()
link = 'https://api.wynncraft.com/v2/player/' + argument + '/stats'
a = requests.get(link)
res = json.loads(a.text)
if res['data']:
user = f'name: {res["data"][0]["username"]}'
is_online = f'online: {res["data"][0]["meta"]["location"]["online"]}'
playtime = f'playtime(bug): {res["data"][0]["meta"]["playtime"]}'
rank = f'rank: {res["data"][0]["meta"]["tag"]["value"]}'
return user, is_online, playtime, rank
else:
return f'No data available.'
Ответы (1 шт):
Автор решения: web developer
→ Ссылка
main.py
import discord
from discord.ext import commands
import parsing
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('$stats'):
word = message.content.split("$stats", 1)[1].strip()
try:
stats_result = parsing.stats(word)
if stats_result != 'No data available.':
formatted_message = f'**Stats of {word}:** ' + "```" + '\n'.join(stats_result) + "```"
else:
formatted_message = 'No data available.'
await message.channel.send(formatted_message)
except Exception as e:
await message.channel.send(f'```Error: {e}```')
if __name__ == '__main__':
bot.run('YOUR_DISCORD_BOT_TOKEN')
parsing.py
import requests
def stats(argument):
link = f'https://api.wynncraft.com/v2/player/{argument}/stats'
response = requests.get(link)
if response.status_code == 200:
res = response.json()
if res['data']:
user = f'name: {res["data"][0]["username"]}'
is_online = f'online: {res["data"][0]["meta"]["location"]["online"]}'
playtime = f'playtime: {res["data"][0]["meta"]["playtime"]}'
rank = f'rank: {res["data"][0]["meta"]["tag"]["value"]}'
return user, is_online, playtime, rank
else:
return 'No data available.'
else:
return f'Error fetching data: HTTP {response.status_code}'