From c733d1f4f2840d0f0a65a3bbec5cef7193c10ec2 Mon Sep 17 00:00:00 2001 From: matnad Date: Tue, 19 Feb 2019 17:01:24 +0100 Subject: [PATCH] issue #6 fix DM char limit for displaying channels --- cogs/db_api.py | 2 +- cogs/poll_controls.py | 2 +- essentials/multi_server.py | 21 +++++++++++++++++---- pollmaster.py | 7 +++++++ utils/paginator.py | 8 +++++--- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/cogs/db_api.py b/cogs/db_api.py index 51fe1fc..cb9cb4b 100644 --- a/cogs/db_api.py +++ b/cogs/db_api.py @@ -20,7 +20,7 @@ class DiscordBotsOrgAPI: while True: logger.info('attempting to post server count') try: - await self.dblpy.post_server_count() + #await self.dblpy.post_server_count() logger.info('posted server count ({})'.format(len(self.bot.servers))) except Exception as e: logger.exception('Failed to post server count\n{}: {}'.format(type(e).__name__, e)) diff --git a/cogs/poll_controls.py b/cogs/poll_controls.py index f7b7a05..f61585b 100644 --- a/cogs/poll_controls.py +++ b/cogs/poll_controls.py @@ -222,7 +222,7 @@ class PollControls: else: return - def item_fct(item): + def item_fct(i,item): return f':black_small_square: **{item["short"]}**: {item["name"]}' title = f' Listing {short} polls' diff --git a/essentials/multi_server.py b/essentials/multi_server.py index c6cb007..8ff5a78 100644 --- a/essentials/multi_server.py +++ b/essentials/multi_server.py @@ -1,6 +1,10 @@ +import time + import discord from essentials.settings import SETTINGS +from utils.paginator import embed_list_paginated + async def get_pre(bot, message): '''Gets the prefix for a message.''' @@ -107,13 +111,22 @@ async def ask_for_channel(bot, server, message): return False # otherwise ask for a channel - text = 'Polls are bound to a specific channel on a server. Please select the channel for this poll by typing the corresponding number.\n' i = 1 + text = 'Polls are bound to a specific channel on a server. Please select the channel for this poll by typing the corresponding number.\n' for name in [c.name for c in channel_list]: - text += f'\n**{i}** - {name}' - i += 1 + to_add = f'\n**{i}** - {name}' + + # check if length doesn't exceed allowed maximum or split it into multiple messages + if text.__len__() + to_add.__len__() > 2048: + embed = discord.Embed(title="Select a channel", description=text, color=SETTINGS.color) + await bot.say(embed=embed) + text = 'Polls are bound to a specific channel on a server. Please select the channel for this poll by typing the corresponding number.\n' + else: + text += to_add + i += 1 + embed = discord.Embed(title="Select a channel", description=text, color=SETTINGS.color) - server_msg = await bot.say(embed=embed) + await bot.say(embed=embed) valid_reply = False nr = 1 diff --git a/pollmaster.py b/pollmaster.py index abc5613..9a445f3 100644 --- a/pollmaster.py +++ b/pollmaster.py @@ -55,6 +55,12 @@ async def on_ready(): print(bot.db) await bot.change_presence(game=discord.Game(name=f'V2 IS HERE >> pm!help')) + await bot.db.config.update_one( + {'_id': '261914618342014977'}, + {'$set': {'prefix': 'd!'}}, + upsert=True + ) + # check discord server configs try: db_server_ids = [entry['_id'] async for entry in bot.db.config.find({}, {})] @@ -97,6 +103,7 @@ async def on_command_error(e, ctx): # log error logger.error(f'{type(e).__name__}: {e}\n{"".join(traceback.format_tb(e.__traceback__))}') + raise e if SETTINGS.msg_errors: # send discord message for unexpected errors diff --git a/utils/paginator.py b/utils/paginator.py index c1d3385..771bc58 100644 --- a/utils/paginator.py +++ b/utils/paginator.py @@ -4,8 +4,9 @@ async def embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_pre # generate list embed.title = f'{items.__len__()} entries' text = '\n' - for item in items[start:start+per_page]: - text += item_fct(item) + '\n' + for i,item in enumerate(items[start:start+per_page]): + j = i+start + text += item_fct(j,item) + '\n' embed.description = text # footer text @@ -21,7 +22,8 @@ async def embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_pre # post / edit message if msg is not None: await bot.edit_message(msg, embed=embed) - await bot.clear_reactions(msg) + if str(msg.channel.type) != 'private': + await bot.clear_reactions(msg) else: msg = await bot.say(embed=embed)