How can I format a python pretty table for a discord message?

277 Views Asked by At

I'm trying to create a discord bot and part of it reads a JSON file and creates a table from it. The table looks great in visual studio when I test it (The bottom half of the screenshot) But when I send it as a message in discord it shifts everything and looks all messed up. Any ideas how I can get the discord message to match what I see in visual studio? enter image description here

This is the code I have currently

def get_list(dct):
   table = PrettyTable()
   for column in dct[0].keys():
       table.add_column(column, [])
   for item in dct:
       row_list = []
       for i in item.values():
           row_list.append(i)
       table.add_row(row_list)
   return table


async def on_message(message):
   if message.content.startswith('$LIST ToPlay'):
       with open('GameListData.json', 'r+') as file:
           data = json.load(file)
           msg = get_list(data['ToPlay']).ToString()
           await message.channel.send(msg)
1

There are 1 best solutions below

0
Lapis Pheonix On

Discord doesnt keep the same formatting, you need to use a code block for the output.

def get_list(dct):
   table = PrettyTable()
   for column in dct[0].keys():
       table.add_column(column, [])
   for item in dct:
       row_list = []
       for i in item.values():
           row_list.append(i)
       table.add_row(row_list)
   return table


async def on_message(message):
   if message.content.startswith('$LIST ToPlay'):
       with open('GameListData.json', 'r+') as file:
           data = json.load(file)
           msg = get_list(data['ToPlay']).ToString()
           await message.channel.send(f"```{msg}```")