Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

read from a file and send as an embed how many times a word occurred discord.py

is possible to read from a file how many times a word is repeated and send itas an embed in discord.py?

i tried this but it dosent add a field

    @client.command(brief='[&help nslaves] if it dosent work',
                    description='do [&slaveslottery] before you run this command')
    async def ntimes(ctx):
    
        counts = {}  # create empty dictionary
        embed = discord.Embed(title=f"the lines are :", description="",
                              color=discord.Color.blue())
        def lolen():
            with open(f"./{ctx.author}/{ctx.aut

hor}lol.txt") as f:
            for line in f:
                stripline = line.strip()  # strip whitespace
                myhash = hash(stripline)
                if myhash:
                    if myhash in counts:  # duplicate line, inc count
                        counts[myhash] = counts[myhash] + 1
                    else:
                        counts[myhash] = 1  # new entry
        f.close()
        # re-read file, and print out duplicate lines
        with open(f"./{ctx.author}/{ctx.author}lol.txt") as f:
            for line in f:
                stripline = line.strip()
                myhash = hash(stripline)
                if myhash:
                    if counts[myhash] > 1:
                        # print duplicate line and count
                        embed.add_field(name=f"stripline, occurred, {counts[myhash]}", value="hei", inline=True)
                        # after printing dup, clear ctr so prints once
                        counts[myhash] = 0
    lolen()
question from:https://stackoverflow.com/questions/65906151/read-from-a-file-and-send-as-an-embed-how-many-times-a-word-occurred-discord-py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...