0
votes

I'm coding a discord bot with Discord.py, and I don't know how to make this work.

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        for line in jeff_file:
            await ctx.send(line)

The file contains 700 words, but I want it to send 5 or 10. Could anyone help me?

3
How do you choose which of the 700 words to send and which words not to send?Stef
Is the file guaranteed to contain one word per line? Or could there be more than one word per line?Stef

3 Answers

1
votes

You can break a line into words using words = line.split(), count the words, then rejoin the words into a single string using text = ' '.join(words).

Splitting lines into words to send the first n words

The following code will send the first n words of the file:

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        words = []
        while len(words) < n:
            line = next(jeff_file)      # will raise exception StopIteration if fewer than n words in file
            words.append(line.split())
        await ctx.send(' '.join(words[:n]))

Splitting lines into words to send n random words

The following code will read all words from the file, then select n words at random and send those:

import random

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        words = [w for line in jeff_file for w in line.split()]
        n_random_words = random.sample(words, n)   # raises exception ValueError if fewer than n words in file
        # n_random_words = random.choices(words, k=n)  # doesn't raise an exception, but can choose the same word more than once if you're unlucky
        await ctx.send(' '.join(n_random_words))

Sending the first n lines

The following code will read and send the first n lines from the file:

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        for _ in range(n):
            line = next(jeff_file)  # will raise exception StopIteration if fewer than n lines in file
            await ctx.send(line)

Sending the first n lines, or fewer lines if there are fewer than n lines in file

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        try:
            for _ in range(n):
                line = next(jeff_file)
                await ctx.send(line)
        except StopIteration:
            pass

Sending n random lines

The following code will read the whole file and send n random lines:

import random

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        all_lines = [line for line in jeff_file]
        n_lines = random.sample(all_lines, n)  # will raise exception ValueError if fewer than n words in file
        # n_lines = random.choices(all_lines, k = n)  # doesn't raise an exception, but might choose the same line more than once
        await ctx.send(line)
0
votes

The other answers are works but if you want your code simpler and one line, you can use:

n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    await ctx.send(' '.join(open('Available.txt', 'r').read().split(' ')[:n]))

This will send the first n words from the document.

0
votes

You can maybe use enumerate and break the loop like this:

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
         jeff_lines = jeff_file.readlines()
         for i, line in enumerate(jeff_lines):
                if i == 5:
                    break
                else:
                    await ctx.send(line)