From c51fe4dcb5e3c74c9ca8c0929d1967ffbc552c9f Mon Sep 17 00:00:00 2001 From: Peter Gilbert Date: Thu, 18 Aug 2022 08:20:58 -0700 Subject: [PATCH] fix info command being over char limit --- users.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/users.py b/users.py index c75555b..3d05182 100644 --- a/users.py +++ b/users.py @@ -121,7 +121,27 @@ def handleLog(self, timestamp: datetime, message: str): # Ignore but mirror log if it's new if timestamp > self.lastUpdateTimestamp: self.bot.log.debug(f"Ignored: {message}") - + + async def check_char_limit(self, tosend, ctx, headers=()): + """ + Check if the message is under the 2000 character discord limit, if not send in multiple messages + """ + # For each message -> make sure they are under the char limit + # If not make a new table and then send a new message + # Definitely could be more efficient but it works lol + messages = [tosend] + x = 0 + for message in messages: + while len(f'```\n{tabulate(messages[x], headers=headers, tablefmt="fancy_grid")}\n```') > DISCORD_MAX_CHAR: + if x == len(messages) - 1: + messages.append([]) + messages[x+1].append(messages[x][-1]) + messages[x] = messages[x][0:-1] + await ctx.send( + f'```\n{tabulate(messages[x], headers=headers, tablefmt="fancy_grid")}\n```' + ) + x += 1 + @commands.command() async def users(self, ctx, arg: str = None): """ @@ -144,21 +164,8 @@ async def users(self, ctx, arg: str = None): user.hoursAlive, ] ) - # For each message -> make sure they are under the char limit - # If not make a new table and then send a new message - # Definitely could be more efficient but it works lol - messages = [table] - x = 0 - for message in messages: - while len(f'```\n{tabulate(messages[x], headers=headers, tablefmt="fancy_grid")}\n```') > DISCORD_MAX_CHAR: - if x == len(messages) - 1: - messages.append([]) - messages[x+1].append(messages[x][-1]) - messages[x] = messages[x][0:-1] - await ctx.send( - f'```\n{tabulate(messages[x], headers=headers, tablefmt="fancy_grid")}\n```' - ) - x += 1 + await self.check_char_limit(table, ctx, headers=headers) + @commands.command() async def info(self, ctx, name=None): @@ -184,4 +191,4 @@ async def info(self, ctx, name=None): for perk in user.perks: if int(user.perks[perk]) != 0: table.append([perk, user.perks[perk]]) - await ctx.send(f'```\n{tabulate(table, tablefmt="fancy_grid")}\n```') + await self.check_char_limit(table, ctx)