From 8667412ba8aea7dbc2fcd60cb200a7da4087b3e6 Mon Sep 17 00:00:00 2001 From: bobloy Date: Mon, 15 Oct 2018 16:39:00 -0400 Subject: [PATCH] Infochannel initial commit and leaver readme update --- README.md | 3 +- infochannel/__init__.py | 5 ++ infochannel/info.json | 19 ++++++ infochannel/infochannel.py | 120 +++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 infochannel/__init__.py create mode 100644 infochannel/info.json create mode 100644 infochannel/infochannel.py diff --git a/README.md b/README.md index d5f16e0..5de2cdd 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ Cog Function | forcemention | **Alpha** |
Mentions unmentionable rolesVery simple cog, mention doesn't persist
| | hangman | **Alpha** |
Play a game of hangmanSome visual glitches and needs more customization
| | howdoi | **Incomplete** |
Ask coding questions and get results from StackExchangeNot yet functional
| -| leaver | **Alpha** |
Send a message in a channel when a user leaves the serverJust released, please report bugs
| +| leaver | **Beta** |
Send a message in a channel when a user leaves the serverSeems to be functional, please report any bugs or suggestions
| +| infochannel | **Alpha** |
Create a channel to display server infoJust released, please report bugs
| | lovecalculator | **Alpha** |
Calculate the love between two users[Snap-Ons] Just updated to V3
| | lseen | **Alpha** |
Track when a member was last onlineAlpha release, please report bugs
| | nudity | **Incomplete** |
Checks for NSFW images posted in non-NSFW channelsLibrary this is based on has a bug, waiting for author to merge my PR
| diff --git a/infochannel/__init__.py b/infochannel/__init__.py new file mode 100644 index 0000000..514cd5f --- /dev/null +++ b/infochannel/__init__.py @@ -0,0 +1,5 @@ +from .infochannel import InfoChannel + + +def setup(bot): + bot.add_cog(InfoChannel(bot)) diff --git a/infochannel/info.json b/infochannel/info.json new file mode 100644 index 0000000..c68d4d5 --- /dev/null +++ b/infochannel/info.json @@ -0,0 +1,19 @@ +{ + "author": [ + "Bobloy" + ], + "bot_version": [ + 3, + 0, + 0 + ], + "description": "Create a channel with updating server info", + "hidden": false, + "install_msg": "Thank you for installing InfoChannel. Get started with `[p]load infochannel`, then `[p]help InfoChannel`", + "requirements": [], + "short": "Updating server info channel", + "tags": [ + "bobloy", + "utils" + ] +} \ No newline at end of file diff --git a/infochannel/infochannel.py b/infochannel/infochannel.py new file mode 100644 index 0000000..f6b3ff0 --- /dev/null +++ b/infochannel/infochannel.py @@ -0,0 +1,120 @@ +from typing import Any + +import discord +from redbot.core import Config, commands +from redbot.core.bot import Red + +Cog: Any = getattr(commands, "Cog", object) + + +class InfoChannel(Cog): + """ + Create a channel with updating server info + + Less important information about the cog + """ + + def __init__(self, bot: Red): + self.bot = bot + self.config = Config.get_conf( + self, identifier=731101021116710497110110101108, force_registration=True + ) + + default_guild = { + "channel_id": None, + "category_id": None, + "member_count": True, + "channel_count": False, + } + + self.config.register_guild(**default_guild) + + @commands.command() + async def infochannel(self, ctx: commands.Context): + """ + Toggle info channel for this server + """ + + def check(m): + return ( + m.content.upper() in ["Y", "YES", "N", "NO"] + and m.channel == ctx.channel + and m.author == ctx.author + ) + + guild: discord.Guild = ctx.guild + channel_id = await self.config.guild(guild).channel_id() + if channel_id is not None: + channel: discord.VoiceChannel = guild.get_channel(channel_id) + else: + channel: discord.VoiceChannel = None + + if channel_id is not None and channel is None: + await ctx.send("Info channel has been deleted, recreate it?") + elif channel_id is None: + await ctx.send("Enable info channel on this server?") + else: + await ctx.send("Info channel is {}. Delete it?".format(channel.mention)) + + msg = await self.bot.wait_for("message", check=check) + + if msg.content.upper() in ["N", "NO"]: + await ctx.send("Cancelled") + return + + if channel is None: + await self.make_infochannel(guild) + else: + await self.delete_infochannel(guild, channel) + + if not await ctx.tick(): + await ctx.send("Done!") + + async def make_infochannel(self, guild: discord.Guild): + category: discord.CategoryChannel = await guild.create_category("────Server Stats────") + + overwrites = {guild.default_role: discord.PermissionOverwrite(connect=False)} + + channel = await guild.create_voice_channel( + "Placeholder", category=category, reason="InfoChannel make", overwrites=overwrites + ) + + await self.config.guild(guild).channel_id.set(channel.id) + await self.config.guild(guild).category_id.set(category.id) + + await self.update_infochannel(guild) + + async def delete_infochannel(self, guild: discord.Guild, channel: discord.VoiceChannel): + await channel.category.delete(reason="InfoChannel delete") + await channel.delete(reason="InfoChannel delete") + await self.config.guild(guild).clear() + + async def update_infochannel(self, guild: discord.Guild): + guild_data = await self.config.guild(guild).all() + + channel_id = guild_data["channel_id"] + if channel_id is None: + return + + channel: discord.VoiceChannel = guild.get_channel(channel_id) + + if channel is None: + return + + name = "" + if guild_data["member_count"]: + name += "Members: {} ".format(guild.member_count) + + if guild_data["channel_count"]: + name += "─ Channels: {}".format(len(guild.channels)) + + if name == "": + name = "Stats not enabled" + + await channel.edit(reason="InfoChannel update", name=name) + + async def on_member_join(self, member: discord.Member): + await self.update_infochannel(member.guild) + + async def on_member_remove(self, member: discord.Member): + await self.update_infochannel(member.guild)