async def delete(self, ctx, name): repo = TagRepository() if not await repo.check_tag_exists(name, ctx.guild.id): embed = discord.Embed(title=f'Error: Tag {name} does not exist', color=Colors.Error) await ctx.send(embed=embed) return tag = await repo.get_tag(name, ctx.guild.id) if ctx.author.guild_permissions.administrator: await self._delete_tag(name, ctx) return if tag['fk_UserId'] != ctx.author.id: embed = discord.Embed( title= f'Error: Tag {name} is not owned by {self.get_full_name(ctx.author)}', color=Colors.Error) await ctx.send(embed=embed) return await self._delete_tag(name, ctx)
async def about(self, ctx, name): name = name.lower() repo = TagRepository() if not await repo.check_tag_exists(name, ctx.guild.id): embed = discord.Embed(title=f'Error: Tag {name} does not exist', color=Colors.Error) await ctx.send(embed=embed) return tag = await repo.get_tag(name, ctx.guild.id) author = self.bot.get_user(tag['fk_UserId']) embed = discord.Embed(title='Tag Information:', color=Colors.ClemsonOrange) embed.add_field(name='Name ', value=tag['name']) embed.add_field(name='Content ', value=tag['content']) fullNameGet = self.get_full_name(author) embed.set_footer(text=fullNameGet, icon_url=author.avatar_url) embed.add_field(name='Creation Date: ', value=tag['CreationDate'], inline=False) await ctx.send(embed=embed)
async def delete(self, ctx: commands.Context, name): tag_repo = TagRepository() claims_repo = ClaimsRepository() if not await tag_repo.check_tag_exists(name, ctx.guild.id): embed = discord.Embed(title=f'Error: tag {name} does not exist', color=Colors.Error) await ctx.send(embed=embed) return tag = await tag_repo.get_tag(name, ctx.guild.id) if tag['fk_UserId'] == ctx.author.id: await self._delete_tag(name, ctx) return claims = await claims_repo.fetch_all_claims_user(ctx.author) if ctx.command.claims_check(claims): await self._delete_tag(name, ctx) return error_str = f'Error: You do not have the tag_delete claim or you do not own this tag' embed = discord.Embed(title=error_str, color=Colors.Error) await ctx.send(embed=embed)
async def add(self, ctx, name: str, *, content: str): name = name.lower() is_admin = ctx.author.guild_permissions.administrator if len(content.split('\n')) > MAX_NON_ADMIN_LINE_LENGTH and not is_admin: embed = discord.Embed(title= f'Error: Tag line number exceeds {MAX_NON_ADMIN_LINE_LENGTH} lines', color=Colors.Error) await ctx.send(embed=embed) return if len(content) > MAX_TAG_CONTENT_SIZE: embed = discord.Embed(title= f'Error: Tag content exceeds {MAX_TAG_CONTENT_SIZE} characters', color=Colors.Error) await ctx.send(embed=embed) return if len(name) > MAX_TAG_NAME_SIZE: embed = discord.Embed(title= f'Error: Tag name exceeds {MAX_TAG_NAME_SIZE} characters', color=Colors.Error) await ctx.send(embed=embed) return content = discord.utils.escape_mentions(content) repo = TagRepository() if await repo.check_tag_exists(name, ctx.guild.id): embed = discord.Embed(title= f'Error: Tag "{name}" already exists in this server', color=Colors.Error) await ctx.send(embed=embed) return tag = Tag(name, content, datetime.utcnow(), ctx.guild.id, ctx.author.id) await TagRepository().insert_tag(tag) embed=discord.Embed(title=":white_check_mark: Tag successfully added", color=Colors.ClemsonOrange) embed.add_field(name="Name", value=name, inline=True) embed.add_field(name="Content", value=content, inline=True) await ctx.send(embed=embed)
async def tag(self, ctx, tag=None): repo = TagRepository() if tag: if not await repo.check_tag_exists(tag, ctx.guild.id): embed = discord.Embed( title=f'Error: Tag "{tag}" does not exist in this server', color=Colors.Error) return await ctx.send(embed=embed) return await ctx.send(await repo.get_tag_content(tag, ctx.guild.id)) tags = await repo.get_all_server_tags(ctx.guild.id) pages = [] #check for if no tags exist in this server if not tags: embed = discord.Embed(title=f'Available Tags', color=Colors.ClemsonOrange) embed.add_field(name='Available:', value='No currently available tags') msg = await ctx.send(embed=embed) await self.bot.messenger.publish(Events.on_set_deletable, msg=msg, author=ctx.author) return #begin generating paginated columns #chunk the list of tags into groups of TAG_CHUNK_SIZE for each page for chunk in self.chunk_list([role['name'] for role in tags], TAG_CHUNK_SIZE): #we need to create the columns on the page so chunk the list again content = '' for col in self.chunk_list(chunk, 3): #the columns wont have the perfect number of elements every time, we need to append spaces if #the list entries is less then the number of columns while len(col) < 3: col.append(' ') #Cocatenate the formatted column string to the page content string content += "{: <20} {: <20} {: <20}\n".format(*col) #Apped the content string to the list of pages to send to the paginator #Marked as a code block to ensure a monospaced font and even columns pages.append(f'```{content}```') #send the pages to the paginator service await self.bot.messenger.publish(Events.on_set_pageable_text, embed_name='Available Tags', field_title='Available:', pages=pages, author=ctx.author, channel=ctx.channel)
async def _delete_tag(self, name, ctx): repo = TagRepository() content = await repo.get_tag_content(name, ctx.guild.id) await repo.delete_tag(name, ctx.guild.id) embed = discord.Embed( title=':white_check_mark: Tag successfully deleted', color=Colors.ClemsonOrange) embed.add_field(name='Name', value=name, inline=True) embed.add_field(name='Content', value=content, inline=True) await ctx.send(embed=embed)
async def on_guild_message_received(self, message: discord.Message) -> None: repo = TagRepository() tagsContent = [] tagFound = False pattern = re.compile(f'(^|\\s)[{TAG_PREFIX}](\\w+)') for match in set(i[1] for i in pattern.findall(message.content)): if not await repo.check_tag_exists(match, message.guild.id): continue tagFound = True tagsContent.append(await repo.get_tag_content(match, message.guild.id)) await repo.increment_tag_use_counter(match, message.guild.id) log.info( f'Tag "{match}" invoked in guild: {message.guild.id} by: {message.author.id}' ) if not tagFound: return tag_str = '\n-------\n'.join(tagsContent) pages = [] # If length of all tags is greater than the threshold, sends it to the paginate service, otherwise sends as a normal message if len(tagsContent) > 1: for chunk in self.chunk_iterable(tag_str, TAG_PAGINATE_THRESHOLD): pages.append(chunk) if len(pages) > 1: await self.bot.messenger.publish(Events.on_set_pageable_text, embed_name='Tags Contents', field_title='Contents', pages=pages, author=message.author, channel=message.channel) else: msg = await message.channel.send(tag_str) await self.bot.messenger.publish(Events.on_set_deletable, msg=msg, author=message.author, timeout=60)
async def on_message_recieved(self, message: discord.Message) -> None: repo = TagRepository() pattern = f'(^| )[{TAG_PREFIX}](?P<name>\\w+)($| )' found_name = re.search(pattern, message.content) if not found_name: return name = found_name.groupdict()['name'].lower() if not await repo.check_tag_exists(name, message.guild.id): return content = await repo.get_tag_content(name, message.guild.id) log.info( f'Tag "{found_name}" invoked in guild: {message.guild.id} by: {message.author.id}' ) msg = await message.channel.send(content) await self.bot.messenger.publish(Events.on_set_deletable, msg=msg, author=message.author, timeout=60)