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


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:

async def key(ctx):
  f = open("keys.txt")
  keys = f.readlines()

  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/", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "", line 42, in key
    await ctx.send(keys)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/", line 1065, in send
    data = await state.http.send_message(, content, tts=tts, embed=embed,
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/", 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/", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/djopasjopdapjaopds/venv/lib/python3.8/site-packages/discord/ext/commands/", 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

>Solution :

I guess this … maybe

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

Leave a Reply Cancel reply