Cannot get a specific letter in a string of a command

Viewed 45

I have been having trouble getting a specific part of the command. What I intend to do is that when L is in the x position I want it to send a message that is formatted one way and when L is in a different position I want it to send a different message.

The command I am working with here is: !en L BTC 19500 Nothing prints in the embed when I run the code

@bot.command() 
    async def en(ctx):
            if (ctx.author.id == 573284477158162433 or 244240895627624448):
                global channels, message
                x = ctx.message.content
                
                print(len(x))
                if(x[4:len(x)-11]=="L"):
                    message = "**Type:Long **"+"**Ticker: **"+x[4:len(x)-6] + "\n  **Entry Price:** " + x[7:]
                print(message)
        
             
                f=0
                for i in range(len(channels)):
                 f+=1
                 current_server = channels[i]
                 disc_channel = bot.get_channel(current_server)
               
                await ctx.send(content = "@everyone", allowed_mentions = allowed_mentions)
                embed = discord.Embed(title="Entry", description=message, color=0x70f508)
                embed.timestamp = datetime.datetime.utcnow()
                embed.set_footer(text="Viggy Analytics")
                await disc_channel.send(embed=embed)
1 Answers

Firstly, you have to check if the ctx.author.id is equal to both ids. The way you are doing it now only checks if it's equal to 573284477158162433. Second, you need to include the code that sends the embed inside the for-loop if you want it to send to each of the channels in your channels list. Lastly, your string slice should be x[4:len(x)-10], since x[4:len(x)-11] deletes the entire message content following the command. However, a much simpler way of doing this is simply getting x[4]. Here's a short tutorial on how to slice strings properly in Python.

@bot.command()
async def en(ctx):
    if (ctx.author.id == 573284477158162433 or ctx.author.id == 244240895627624448):
        global channels, message
        x = ctx.message.content

        if (x[4] == "L"):
            message = "**Type:Long **" + "**Ticker: **" + x[4:len(x) - 6] + "\n  **Entry Price:** " + x[7:]
        print(message)

        f = 0
        for i in range(len(channels)):
            f += 1
            current_server = channels[i]
            disc_channel = bot.get_channel(current_server)

        await ctx.send(content="@everyone", allowed_mentions=allowed_mentions)
        embed = discord.Embed(title="Entry", description=message, color=0x70f508)
        embed.timestamp = datetime.datetime.utcnow()
        embed.set_footer(text="Viggy Analytics")
        await disc_channel.send(embed=embed)
Related