#Welp
1 messages · Page 1 of 1 (latest)
Why you should not use the requests library for your bot
requests is a popular HTTP library for Python. It is however not a good option for Discord bots, since it is not async and blocking.
This essentially means that your bot will not be able to execute any code at all while a request is happening. Since requests usually take a few seconds to complete, this can have a detrimental effect on your bot's performance. E.g if a user executes a command that performs a request taking 5 seconds to complete, no one else will be able to use your bot for those 5 seconds.
Please look at using a HTTP library that has async support, such as aiohttp or httpx
What do you mean with %7D?
.close
Done with your help thread?
Please close your own help thread by using </close:1009144375709814897> with @slate scaffold.
Backup bot: </solved:1109625445990793246> (or
.solved) with @fiery spade.
you should use a library and just decode the url
@bot.slash_command(name="test", description="test")
async def get(ctx):
API_KEY = 'X'
API_URL = 'X'
ENDPOINT = 'X'
headers = {
'Authorization': API_KEY,
'Accept': 'application/json',
'Content-Type': 'application/json',
}
url = f'{API_URL}/{ENDPOINT}' # Make sure there is a '/' between API_URL and ENDPOINT
response = requests.get(url, headers=headers)
if response.status_code < 200 or response.status_code >= 300:
raise Exception(f'Request failed: {response.text}')
else:
result = response.json()
data = [{'id': item['id'], 'promoImageUrl': item['promoImageUrl']} for item in result]
await ctx.respond(data)
check this
dont use requests
?tag requests
Why you should not use the requests library for your bot
requests is a popular HTTP library for Python. It is however not a good option for Discord bots, since it is not async and blocking.
This essentially means that your bot will not be able to execute any code at all while a request is happening. Since requests usually take a few seconds to complete, this can have a detrimental effect on your bot's performance. E.g if a user executes a command that performs a request taking 5 seconds to complete, no one else will be able to use your bot for those 5 seconds.
Please look at using a HTTP library that has async support, such as aiohttp or httpx
ok
@bot.slash_command(name="test", description="test")
async def get(ctx):
API_KEY = 'X'
API_URL = 'X'
ENDPOINT = 'X'
headers = {
'Authorization': API_KEY,
'Accept': 'application/json',
'Content-Type': 'application/json',
}
url = f'{API_URL}/{ENDPOINT}'
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status < 200 or response.status >= 300:
raise Exception(f'Request failed: {await response.text()}')
else:
result = await response.json()
data = [{'id': item['id'], 'promoImageUrl': item['promoImageUrl']} for item in result]
await ctx.respond(data)
ya