From 9f59acd5e2225bf36f194f8e8b1f5439ebd9497f Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Oct 2018 08:40:42 +1300 Subject: [PATCH 1/6] Add listchannels and listguilds command listchannels - lists the channels in the guild, in displayed order listguilds - lists the guilds the bot is in, sorted by usercount --- tools/tools.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/tools/tools.py b/tools/tools.py index 1fc8d6c..9c1349f 100644 --- a/tools/tools.py +++ b/tools/tools.py @@ -11,9 +11,9 @@ from redbot.core.utils.menus import menu, DEFAULT_CONTROLS from tabulate import tabulate from contextlib import suppress as sps - BaseCog = getattr(commands, "Cog", object) + class Tools(BaseCog): """Mod and Admin tools.""" @@ -359,6 +359,52 @@ class Tools(BaseCog): ) await awaiter.edit(embed=embed) + @commands.command(name='listguilds', aliases=['listservers', 'guildlist', 'serverlist']) + async def listguilds(self, ctx): + """ + List the guilds|servers the bot is in + """ + asciidoc = lambda m: "```asciidoc\n{}\n```".format(m) + guilds = sorted(self.bot.guilds, key=lambda g: -g.member_count) + header = ("```\n" + "The bot is in the following {} server{}\n" + "```").format(len(guilds), 's' if len(guilds) > 1 else '') + + max_zpadding = max([len(str(g.member_count)) for g in guilds]) + form = "{gid} :: {mems:0{zpadding}} :: {name}" + all_forms = [form.format(gid=g.id, mems=g.member_count, name=g.name, zpadding=max_zpadding) for g in guilds] + final = '\n'.join(all_forms) + + await ctx.send(header) + print('FINAL', final) + for page in cf.pagify(final, delims=['\n'], shorten_by=16): + await ctx.send(asciidoc(page)) + + + @commands.command(name='listchannels', aliases=['channellist']) + async def listchannels(self, ctx): + """ + List the channels of the current server + """ + asciidoc = lambda m: "```asciidoc\n{}\n```".format(m) + channels = ctx.guild.channels + top_channels, category_channels = self.sort_channels(ctx.guild.channels) + + topChannels_formed = '\n'.join(self.channels_format(top_channels)) + categories_formed = '\n\n'.join([self.category_format(tup) for tup in category_channels]) + + ### + print(topChannels_formed) + print(categories_formed) + + await ctx.send(f"{ctx.guild.name} has {len(channels)} channel{'s' if len(channels) > 1 else ''}.") + + for page in cf.pagify(topChannels_formed, delims=['\n'], shorten_by=16): + await ctx.send(asciidoc(page)) + + for page in cf.pagify(categories_formed, delims=['\n\n'], shorten_by=16): + await ctx.send(asciidoc(page)) + @commands.guild_only() @commands.command() @checks.mod_or_permissions(manage_server=True) @@ -742,3 +788,51 @@ class Tools(BaseCog): roles = guild.roles role = discord.utils.find(lambda r: r.name.lower() == str(rolename).lower(), roles) return role + + + def sort_channels(self, channels): + temp = dict() + + channels = sorted(channels, key=lambda c: c.position) + + for c in channels[:]: + if isinstance(c, discord.CategoryChannel): + channels.pop(channels.index(c)) + temp[c] = list() + + for c in channels[:]: + if c.category: + channels.pop(channels.index(c)) + temp[c.category].append(c) + + category_channels = sorted([(cat, sorted(chans, key=lambda c: c.position)) for cat, chans in temp.items()], key=lambda t: t[0].position) + return channels, category_channels + + def channels_format(self, channels: list): + print('\nCHANNELS FORM :: ', channels) + + if channels == []: + return [] + + channel_form = "{name} :: {ctype} :: {cid}" + + def type_name(channel): + return channel.__class__.__name__[:-7] + + name_justify = max([len(c.name[:24]) for c in channels]) + type_justify = max([len(type_name(c)) for c in channels]) + + return [channel_form.format(name=c.name[:24].ljust(name_justify), ctype=type_name(c).ljust(type_justify), cid=c.id) for c in channels] + + + def category_format(self, cat_chan_tuple: tuple): + + cat = cat_chan_tuple[0] + chs = cat_chan_tuple[1] + + chfs = self.channels_format(chs) + if chfs != []: + ch_forms = ['\t' + f for f in chfs] + return '\n'.join([f'{cat.name} :: {cat.id}'] + ch_forms) + else: + return '\n'.join([f'{cat.name} :: {cat.id}'] + ['\tNo Channels']) From 77e143658d0217c5252e3b290c72c2e5e9fa2733 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Oct 2018 08:42:53 +1300 Subject: [PATCH 2/6] Add checks to new commands Mod or Manage Channels perm needed to use these commands --- tools/tools.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/tools.py b/tools/tools.py index 9c1349f..db97ebc 100644 --- a/tools/tools.py +++ b/tools/tools.py @@ -360,6 +360,7 @@ class Tools(BaseCog): await awaiter.edit(embed=embed) @commands.command(name='listguilds', aliases=['listservers', 'guildlist', 'serverlist']) + @checks.mod_or_permissions() async def listguilds(self, ctx): """ List the guilds|servers the bot is in @@ -380,7 +381,8 @@ class Tools(BaseCog): for page in cf.pagify(final, delims=['\n'], shorten_by=16): await ctx.send(asciidoc(page)) - + @commands.guild_only() + @checks.mod_or_permissions(manage_channels=True) @commands.command(name='listchannels', aliases=['channellist']) async def listchannels(self, ctx): """ From 5c7fd7f9c86505d90d7784d0e8f7dc1200acf136 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Oct 2018 11:17:55 +1300 Subject: [PATCH 3/6] Handle bot only content Send a message when the history only has content from bots --- chatchart/chatchart.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chatchart/chatchart.py b/chatchart/chatchart.py index d22a617..5a2ea1c 100644 --- a/chatchart/chatchart.py +++ b/chatchart/chatchart.py @@ -103,6 +103,7 @@ class Chatchart(BaseCog): return await ctx.send("No permissions to read that channel.") msg_data = {"total count": 0, "users": {}} + for msg in history: if len(msg.author.name) >= 20: short_name = "{}...".format(msg.author.name[:20]) @@ -119,6 +120,12 @@ class Chatchart(BaseCog): msg_data["users"][whole_name]["msgcount"] = 1 msg_data["total count"] += 1 + if msg_data['users'] == {}: + await em.delete() + return await ctx.message.channel.send(f'Only bots have sent messages in {channel.mention}') + + + for usr in msg_data["users"]: pd = float(msg_data["users"][usr]["msgcount"]) / float(msg_data["total count"]) msg_data["users"][usr]["percent"] = round(pd * 100, 1) From d90f3481ba95ed21e178703c53f96afb28ef83c0 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Oct 2018 11:24:02 +1300 Subject: [PATCH 4/6] Remove unnecessary spacing --- chatchart/chatchart.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/chatchart/chatchart.py b/chatchart/chatchart.py index 5a2ea1c..01f0392 100644 --- a/chatchart/chatchart.py +++ b/chatchart/chatchart.py @@ -124,8 +124,6 @@ class Chatchart(BaseCog): await em.delete() return await ctx.message.channel.send(f'Only bots have sent messages in {channel.mention}') - - for usr in msg_data["users"]: pd = float(msg_data["users"][usr]["msgcount"]) / float(msg_data["total count"]) msg_data["users"][usr]["percent"] = round(pd * 100, 1) From 8ef28a354fe1a5968a9556237d986912931479c6 Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Sat, 27 Oct 2018 12:30:56 -0700 Subject: [PATCH 5/6] Formatting and removing prints --- tools/tools.py | 59 ++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/tools/tools.py b/tools/tools.py index db97ebc..84f9425 100644 --- a/tools/tools.py +++ b/tools/tools.py @@ -359,7 +359,7 @@ class Tools(BaseCog): ) await awaiter.edit(embed=embed) - @commands.command(name='listguilds', aliases=['listservers', 'guildlist', 'serverlist']) + @commands.command(name="listguilds", aliases=["listservers", "guildlist", "serverlist"]) @checks.mod_or_permissions() async def listguilds(self, ctx): """ @@ -367,23 +367,25 @@ class Tools(BaseCog): """ asciidoc = lambda m: "```asciidoc\n{}\n```".format(m) guilds = sorted(self.bot.guilds, key=lambda g: -g.member_count) - header = ("```\n" - "The bot is in the following {} server{}\n" - "```").format(len(guilds), 's' if len(guilds) > 1 else '') + header = ("```\n" "The bot is in the following {} server{}:\n" "```").format( + len(guilds), "s" if len(guilds) > 1 else "" + ) max_zpadding = max([len(str(g.member_count)) for g in guilds]) form = "{gid} :: {mems:0{zpadding}} :: {name}" - all_forms = [form.format(gid=g.id, mems=g.member_count, name=g.name, zpadding=max_zpadding) for g in guilds] - final = '\n'.join(all_forms) + all_forms = [ + form.format(gid=g.id, mems=g.member_count, name=g.name, zpadding=max_zpadding) + for g in guilds + ] + final = "\n".join(all_forms) await ctx.send(header) - print('FINAL', final) - for page in cf.pagify(final, delims=['\n'], shorten_by=16): + for page in cf.pagify(final, delims=["\n"], shorten_by=16): await ctx.send(asciidoc(page)) @commands.guild_only() @checks.mod_or_permissions(manage_channels=True) - @commands.command(name='listchannels', aliases=['channellist']) + @commands.command(name="listchannels", aliases=["channellist"]) async def listchannels(self, ctx): """ List the channels of the current server @@ -392,19 +394,17 @@ class Tools(BaseCog): channels = ctx.guild.channels top_channels, category_channels = self.sort_channels(ctx.guild.channels) - topChannels_formed = '\n'.join(self.channels_format(top_channels)) - categories_formed = '\n\n'.join([self.category_format(tup) for tup in category_channels]) + topChannels_formed = "\n".join(self.channels_format(top_channels)) + categories_formed = "\n\n".join([self.category_format(tup) for tup in category_channels]) - ### - print(topChannels_formed) - print(categories_formed) + await ctx.send( + f"{ctx.guild.name} has {len(channels)} channel{'s' if len(channels) > 1 else ''}." + ) - await ctx.send(f"{ctx.guild.name} has {len(channels)} channel{'s' if len(channels) > 1 else ''}.") - - for page in cf.pagify(topChannels_formed, delims=['\n'], shorten_by=16): + for page in cf.pagify(topChannels_formed, delims=["\n"], shorten_by=16): await ctx.send(asciidoc(page)) - for page in cf.pagify(categories_formed, delims=['\n\n'], shorten_by=16): + for page in cf.pagify(categories_formed, delims=["\n\n"], shorten_by=16): await ctx.send(asciidoc(page)) @commands.guild_only() @@ -791,7 +791,6 @@ class Tools(BaseCog): role = discord.utils.find(lambda r: r.name.lower() == str(rolename).lower(), roles) return role - def sort_channels(self, channels): temp = dict() @@ -807,11 +806,13 @@ class Tools(BaseCog): channels.pop(channels.index(c)) temp[c.category].append(c) - category_channels = sorted([(cat, sorted(chans, key=lambda c: c.position)) for cat, chans in temp.items()], key=lambda t: t[0].position) + category_channels = sorted( + [(cat, sorted(chans, key=lambda c: c.position)) for cat, chans in temp.items()], + key=lambda t: t[0].position, + ) return channels, category_channels def channels_format(self, channels: list): - print('\nCHANNELS FORM :: ', channels) if channels == []: return [] @@ -824,8 +825,14 @@ class Tools(BaseCog): name_justify = max([len(c.name[:24]) for c in channels]) type_justify = max([len(type_name(c)) for c in channels]) - return [channel_form.format(name=c.name[:24].ljust(name_justify), ctype=type_name(c).ljust(type_justify), cid=c.id) for c in channels] - + return [ + channel_form.format( + name=c.name[:24].ljust(name_justify), + ctype=type_name(c).ljust(type_justify), + cid=c.id, + ) + for c in channels + ] def category_format(self, cat_chan_tuple: tuple): @@ -834,7 +841,7 @@ class Tools(BaseCog): chfs = self.channels_format(chs) if chfs != []: - ch_forms = ['\t' + f for f in chfs] - return '\n'.join([f'{cat.name} :: {cat.id}'] + ch_forms) + ch_forms = ["\t" + f for f in chfs] + return "\n".join([f"{cat.name} :: {cat.id}"] + ch_forms) else: - return '\n'.join([f'{cat.name} :: {cat.id}'] + ['\tNo Channels']) + return "\n".join([f"{cat.name} :: {cat.id}"] + ["\tNo Channels"]) From 6005bb3eee6d4bdd19d0e895b8284f4640510f66 Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Sat, 27 Oct 2018 12:33:10 -0700 Subject: [PATCH 6/6] Chatchart changes were handled in #27 --- chatchart/chatchart.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/chatchart/chatchart.py b/chatchart/chatchart.py index 01f0392..d22a617 100644 --- a/chatchart/chatchart.py +++ b/chatchart/chatchart.py @@ -103,7 +103,6 @@ class Chatchart(BaseCog): return await ctx.send("No permissions to read that channel.") msg_data = {"total count": 0, "users": {}} - for msg in history: if len(msg.author.name) >= 20: short_name = "{}...".format(msg.author.name[:20]) @@ -120,10 +119,6 @@ class Chatchart(BaseCog): msg_data["users"][whole_name]["msgcount"] = 1 msg_data["total count"] += 1 - if msg_data['users'] == {}: - await em.delete() - return await ctx.message.channel.send(f'Only bots have sent messages in {channel.mention}') - for usr in msg_data["users"]: pd = float(msg_data["users"][usr]["msgcount"]) / float(msg_data["total count"]) msg_data["users"][usr]["percent"] = round(pd * 100, 1)