How can I make a commmand that goes through a txt file and sends the text that i need?

Advertisements

Im trying to make an api key thingy in discord but i don’t know how to go through txt files one by one for ex

  1. test
  2. test2
  3. test3
  4. test4

If the first user sends !key it would send test if the 2nd user sends !key it would send test2.And so on.

this is what i’ve made so far:

@bot.command()
async def key(ctx):
  f = open("keys.txt")
  keys = f.readlines()
  f.close()

  await ctx.send(keys)

But im getting errors such as these:

Ignoring exception in command key:
Traceback (most recent call last):
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 42, in key
    await ctx.send(keys)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/abc.py", line 1065, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/http.py", line 254, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In content: Must be 4000 or fewer in length.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body

And the keys are in a form like this
Ux9wMkTXDtmrgdJfLSHvzF74jsA3qp
uWSj76hBF9taRQG4VEnyJfbkmsLMPe
BJ5fPsV9N8kbRqdpTcx2geHwCGFUDr
ect

>Solution :

I guess this … maybe

with open("keys.txt") as f:
   keys = iter(f.readlines())
@bot.command()
async def key(ctx):
  await ctx.send(next(keys))

Leave a Reply Cancel reply