From 95b285bcd07b8e4130ac348cf421a9326f6968cd Mon Sep 17 00:00:00 2001 From: Fadi Atamny Date: Sun, 3 May 2020 12:13:58 +0300 Subject: [PATCH] added few commands and restrictions for them moved the functions to be static methods and sorted some other things out. --- welcomeCog/__init__.py | 2 +- welcomeCog/welcome.py | 197 ++++++++++++++++++++++++++++++++--------- 2 files changed, 155 insertions(+), 44 deletions(-) diff --git a/welcomeCog/__init__.py b/welcomeCog/__init__.py index f24b4db..318f347 100644 --- a/welcomeCog/__init__.py +++ b/welcomeCog/__init__.py @@ -2,4 +2,4 @@ from .welcome import WelcomeCog from redbot.core.bot import Red def setup(bot: Red): - bot.add_cog(WelcomeCog()) \ No newline at end of file + bot.add_cog(WelcomeCog(bot)) \ No newline at end of file diff --git a/welcomeCog/welcome.py b/welcomeCog/welcome.py index 1d1bcec..12b1b7f 100644 --- a/welcomeCog/welcome.py +++ b/welcomeCog/welcome.py @@ -9,68 +9,179 @@ from redbot.core.utils.chat_formatting import box, humanize_list, pagify url = 'https://raw.githubusercontent.com/Kanium/KaniumCogs/master/welcomeCog/data/embedded_message.json' allowed_guilds = {274657393936302080, 693796372092289024, 508781789737648138} - - -async def fetchMessage(): - async def fetch(): - async with aiohttp.ClientSession() as session: - async with session.get(url) as response: - html = await response.text() - x = json.loads(str(html)) - return x - return await fetch() - - -def formatMessage(jsonFormat): - try: - message = discord.Embed(title=str(jsonFormat['title']), description=''.join( - map(str, jsonFormat['description'])), color=int(jsonFormat['color'], 16)) - message.set_thumbnail(url=jsonFormat['thumbnail']) - for field in jsonFormat['fields']: - if(field['id'] != 'links'): - message.add_field( - name=field['name'], value=field['value'], inline=field['inline']) - else: - message.add_field(name=field['name'], value=''.join( - map(str, field['value'])), inline=field['inline']) - - message.set_footer( - text=jsonFormat['footer']['text'], icon_url=jsonFormat['footer']['icon_url']) - return message - - except: - message = discord.Embed(title="Kanium", description='', color=0x3399ff) - message.add_field( - name="Welcome", value='Welcome To Kanium !', inline=True) - return message +admin_roles = {'Developer', 'admin', 'Council'} class WelcomeCog(commands.Cog): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.message = '' + + def __init__(self, bot): + self.bot = bot + self.message: str = '' + self.channel: discord.TextChannel = None + self.dailyJoinedCount: int = 0 + self.totalJoinedCount: int = 0 + self.dailyLeftCount: int = 0 + self.totalLeftCount: int = 0 + self.totalLogs: int = 0 + self.toggleLogs: bool = True + self.bot.loop.create_task(WelcomeCog.countReset(self)) + + @staticmethod + async def countReset(obj): + while True: + obj.dailyJoinedCount = 0 + obj.dailyLeftCount = 0 + await asyncio.sleep(86400) + + @staticmethod + async def fetchMessage(): + async def fetch(): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + html = await response.text() + x = json.loads(str(html)) + return x + return await fetch() + + @staticmethod + def formatMessage(jsonFormat: str): + try: + message = discord.Embed(title=str(jsonFormat['title']), description=''.join( + map(str, jsonFormat['description'])), color=int(jsonFormat['color'], 16)) + message.set_thumbnail(url=jsonFormat['thumbnail']) + for field in jsonFormat['fields']: + if(field['id'] != 'links'): + message.add_field( + name=field['name'], value=field['value'], inline=field['inline']) + else: + message.add_field(name=field['name'], value=''.join( + map(str, field['value'])), inline=field['inline']) + + message.set_footer( + text=jsonFormat['footer']['text'], icon_url=jsonFormat['footer']['icon_url']) + return message + + except: + message = discord.Embed( + title='Kanium', description='', color=0x3399ff) + message.add_field( + name="Welcome", value='Welcome To Kanium !', inline=True) + return message + + @commands.command(name='pullmessage', description='pulls the message from github again') + @commands.has_any_role(*admin_roles) + async def pullMessage(self, ctx: commands.Context) -> None: + try: + await ctx.trigger_typing() + self.message = await WelcomeCog.fetchMessage() + await ctx.send('Welcome message updated') + except: + print('error occured fetching message') @commands.command(name='welcomepreview', case_insensitive=True, description='Shows a preview of the welcome message') - async def previewMessage(self, ctx): + @commands.has_any_role(*admin_roles) + async def previewMessage(self, ctx: commands.Context) -> None: try: + await ctx.trigger_typing() if ctx.guild.id not in allowed_guilds: return if self.message == '': - self.message = await fetchMessage() - message = formatMessage(self.message) + self.message = await WelcomeCog.fetchMessage() + message = WelcomeCog.formatMessage(self.message) await ctx.send(content=None, embed=message) except(): print(f'Error Occured!') + @commands.command(name='channel', description='Sets the channel to sends log to') + @commands.has_any_role(*admin_roles) + async def logChannel(self, ctx: commands.Context, channel: discord.TextChannel) -> None: + await ctx.trigger_typing() + + if not channel in ctx.guild.channels: + await ctx.send('Channel doesnt exist in guild') + return + + if not channel.permissions_for(ctx.guild.me).send_messages: + await ctx.send('No permissions to talk in that channel.') + return + + self.channel = channel + + await ctx.send(f'I will now send event notices to {channel.mention}.') + + @commands.command(name='stats', description='Shows current statistics') + @commands.has_any_role(*admin_roles) + async def statistics(self, ctx: commands.Context) -> None: + await ctx.trigger_typing() + + message = '```py\nDaily Joined = {0}\tDaily Left = {1}\nTotal Joined = {2}\tTotal Left = {3}\n------------------------\nTotal Logs = {4}```'.format( + self.dailyJoinedCount, self.dailyLeftCount, self.totalJoinedCount, self.totalLeftCount, self.totalLogs) + + await ctx.send(message) + + @commands.command(name='resetstats', description='Resets statistics') + @commands.has_any_role(*admin_roles) + async def resetStatistics(self, ctx: commands.Context) -> None: + await ctx.trigger_typing() + + self.dailyJoinedCount = 0 + self.dailyLeftCount = 0 + self.totalJoinedCount = 0 + self.totalLeftCount = 0 + self.totalLogs = 0 + + await ctx.send('Successfully reset the statistics') + @commands.Cog.listener() - async def on_member_join(self, member: discord.Member): + async def on_member_join(self, member: discord.Member) -> None: try: if member.guild.id not in allowed_guilds: return if self.message == '': - self.message = await fetchMessage() - message = formatMessage(self.message) + self.message = await WelcomeCog.fetchMessage() + message = WelcomeCog.formatMessage(self.message) await member.send(content=None, embed=message) + if self.channel in member.guild.channels and self.toggleLogs: + await self.channel.send('{0} - has joined the server'.format(member)) + self.totalJoinedCount += 1 + self.dailyJoinedCount += 1 + self.totalLogs += 1 except (discord.NotFound, discord.Forbidden): print( f'Error Occured! sending a dm to {member.display_name} didnt work !') + + @commands.Cog.listener() + async def on_member_remove(self, member: discord.Member) -> None: + try: + if self.channel in member.guild.channels and self.toggleLogs: + await self.channel.send('{0} - has left the server'.format(member)) + self.totalLeftCount += 1 + self.dailyLeftCount += 1 + self.totalLogs += 1 + except (discord.NotFound, discord.Forbidden): + print( + f'Error Occured!') + + @commands.Cog.listener() + async def on_member_ban(self, guild: discord.Guild, member: discord.Member) -> None: + try: + if not self.channel in member.guild.channels: + print('{0} - has been banned from the server'.format(member)) + return + await self.channel.send('{0} - has been banned from the server'.format(member)) + self.totalLogs += 1 + except (discord.NotFound, discord.Forbidden): + print( + f'Error Occured!') + + @commands.Cog.listener() + async def on_member_ban(self, guild: discord.Guild, member: discord.Member) -> None: + try: + if not self.channel in member.guild.channels: + print('{0} - has been unbanned from the server'.format(member)) + return + await self.channel.send('{0} - has been unbanned from the server'.format(member)) + self.totalLogs += 1 + except (discord.NotFound, discord.Forbidden): + print( + f'Error Occured!')