CodeIsLaw
CodeIsLaw

Reputation: 345

Layout error when sending a multiline string as message with discord bot

I got a discord bot up and running. My goal is to send a message to the user after a certain command. This works, but somehow my layout is wrong. The first line of my string is correct and after that the rest is send with some leading spaces.

client = commands.Bot(command_prefix = '/')

@client.command()
async def start(ctx):
    welcome_message = f"""
    Welcome to my bot
    here is a new line which has leading spaces
    this line here has some aswell
    """
    await ctx.send(welcome_message)

So basically my message looks like this:

Welcome to my bot
    here is a new line which has leading spaces
    this line here has some aswell

Even with welcome_message = welcome_message.lstrip() before sending doesn't fix it.

Upvotes: 1

Views: 631

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

The indentation in this case is part of the string (since it's a multiline string), you could simply remove it:

@client.command()
async def start(ctx):
    welcome_message = welcome_message = f"""
Welcome to my bot
here is a new line which has leading spaces
this line here has some aswell
    """
    await ctx.send(welcome_message)

If you want to keep the indentation use the textwrap.dedent method:

import textwrap

@client.command()
async def start(ctx):
    welcome_message = welcome_message = f"""
    Welcome to my bot
    here is a new line which has leading spaces
    this line here has some aswell
    """
    await ctx.send(textwrap.dedent(welcome_message))

Upvotes: 5

Related Questions