<commit_before>
import discord
from discord.ext.commands.formatter import HelpFormatter, Paginator
from discord.ext.commands import Command

import inspect
import itertools
from modules import utilities

import clients

class CustomHelpFormatter(HelpFormatter):
	
	'''Custom Help Formatter'''
	
	def __init__(self, embed_color):
		self.embed_color = embed_color
		self.embed_total_limit = 6000
		self.embed_field_limit = 1024
		self.embed_fields_limit = 25
	
	async def format(self):
		'''Format'''
		description_paginator = Paginator(max_size = 2048)
		max_width = self.max_name_size
		if not isinstance(self.command, Command) or self.has_subcommands():
			filtered_command_list = await self.filter_command_list()
		if self.is_bot():
			def category(tup):
				cog = tup[1].cog_name
				# we insert the zero width space there to give it approximate last place sorting position
				return cog if cog is not None else "\u200bNo Category"
			data = sorted(filtered_command_list, key = lambda c: category(c).lower())
			embeds = [discord.Embed(title = "My Commands", color = self.embed_color)]
			for category, commands in itertools.groupby(data, key = category):
				commands = sorted(commands, key = lambda c: c[0])
				if len(commands) > 0:
					field_paginator = Paginator(max_size = self.embed_field_limit)
					self._add_subcommands_to_page(max_width, commands, field_paginator)
					# Embed Limit
					total_paginator_characters = len(field_paginator.pages) * len(category + " (coninued)") 
					for page in field_paginator.pages:
						total_paginator_characters += len(page)
					if utilities.embed_total_characters(embeds[-1]) + total_paginator_characters > self.embed_total_limit:
						embeds.append(discord.Embed(color = self.embed_color))
					# 
					if len(embeds[-1].fields) <= self.embed_fields_limit - len(field_paginator.pages):
						embeds[-1].add_field(name = category, value = field_paginator.pages[0], inline = False)
					else:
						embeds.append(discord.Embed(color = self.embed_color).add_field(name = category, value = field_paginator.pages[0], inline = False))
					for page in field_paginator.pages[1:]:
						embeds[-1].add_field(name = "{} (continued)".format(category), value = page, inline = False)
			return embeds
		elif isinstance(self.command, Command):
			# <signature portion>
			title = self.get_command_signature()
			# <long doc> section
			if self.command.help:
				description_paginator.add_line(self.command.help, empty = True)
			# end it here if it's just a regular command
			if not self.has_subcommands() or not list(filtered_command_list):
				description_paginator.close_page()
				if not self.command.help:
					return [discord.Embed(title = title, description = self.command.description, color = self.embed_color)]
				elif len(self.command.help) <= 2048:
					description = clients.code_block.format(self.command.help) if "  " in self.command.help else self.command.help
					description += "\n" + self.command.description
					return [discord.Embed(title = title, description = description, color = self.embed_color)]
				return self.embeds(title, description_paginator)
			subcommands = sorted(filtered_command_list, key = lambda c: c[0])
			subcommands_lines = self._subcommands_lines(max_width, subcommands)
			if (not self.command.help or len(self.command.help) <= 2048) and len('\n'.join(subcommands_lines)) <= self.embed_field_limit - 8:
			# 8: len("```\n") * 2
				embed = discord.Embed(color = self.embed_color)
				value = "{}\n".format(description_paginator.pages[0]) if description_paginator.pages else ""
				value += self.command.description
				if not value:
					embed.title = title
				else:
					embed.add_field(name = title, value = value, inline = False)
				embed.add_field(name = "Subcommands for {}".format(self.command), value = clients.code_block.format('\n'.join(subcommands_lines)), inline = False)
				return [embed]
			description_paginator.add_line("Subcommands for {}:".format(self.command))
			self._add_subcommands_to_page(max_width, subcommands, description_paginator)
		else: # cog
			description = inspect.getdoc(self.command)
			if description:
				# <description> portion
				description_paginator.add_line(description, empty = True)
			title = "{} Commands".format(type(self.command).__name__)
			subcommands = sorted(filtered_command_list, key = lambda c: c[0])
			self._add_subcommands_to_page(max_width, subcommands, description_paginator)
		return self.embeds(title, description_paginator)
	
	def _add_subcommands_to_page(self, max_width, commands, paginator):
		for line in self._subcommands_lines(max_width, commands):
			paginator.add_line(line)
	
	@staticmethod
	def _subcommands_lines(max_width, commands):
	# def _subcommands_lines(max_width, commands, indent = True):
		lines = []
		for name, command in commands:
			if name in command.aliases: # skip aliases
				continue
			line = '{0:<{width}}  {1}'.format(name, command.short_doc, width = max_width)
			# line = '{indent}{0:<{width}}  {1}'.format(name, command.short_doc, width = max_width, indent = "  " if indent else "")
			if len(line) <= 55:
				lines.append(line)
			else:
				cutoff = line[:55].rfind(' ')
				lines.append(line[:cutoff])
				while cutoff + 55 < len(line):
					new_cutoff = line[:cutoff + 55].rfind(' ')
					lines.append(' ' * (max_width + 2) + line[cutoff + 1:new_cutoff])<commit_msg>[Discord] Improve method and variable names in help formatter<commit_after>
import discord
from discord.ext.commands.formatter import HelpFormatter, Paginator
from discord.ext.commands import Command

import inspect
import itertools
from modules import utilities

import clients

class CustomHelpFormatter(HelpFormatter):
	
	'''Custom Help Formatter'''
	
	def __init__(self, embed_color):
		self.embed_color = embed_color
		self.embed_total_limit = 6000
		self.embed_field_limit = 1024
		self.embed_fields_limit = 25
	
	async def format(self):
		'''Format'''
		description_paginator = Paginator(max_size = 2048)
		max_width = self.max_name_size
		if not isinstance(self.command, Command) or self.has_subcommands():
			filtered_command_list = await self.filter_command_list()
		if self.is_bot():
			def category(tup):
				cog = tup[1].cog_name
				# we insert the zero width space there to give it approximate last place sorting position
				return cog if cog is not None else "\u200bNo Category"
			data = sorted(filtered_command_list, key = lambda c: category(c).lower())
			embeds = [discord.Embed(title = "My Commands", color = self.embed_color)]
			for category, commands in itertools.groupby(data, key = category):
				commands = sorted(commands, key = lambda c: c[0])
				if len(commands) > 0:
					field_paginator = Paginator(max_size = self.embed_field_limit)
					self._add_subcommands_to_page(max_width, commands, field_paginator)
					# Embed Limit
					total_paginator_characters = len(field_paginator.pages) * len(category + " (coninued)") 
					for page in field_paginator.pages:
						total_paginator_characters += len(page)
					if utilities.embed_total_characters(embeds[-1]) + total_paginator_characters > self.embed_total_limit:
						embeds.append(discord.Embed(color = self.embed_color))
					# 
					if len(embeds[-1].fields) <= self.embed_fields_limit - len(field_paginator.pages):
						embeds[-1].add_field(name = category, value = field_paginator.pages[0], inline = False)
					else:
						embeds.append(discord.Embed(color = self.embed_color).add_field(name = category, value = field_paginator.pages[0], inline = False))
					for page in field_paginator.pages[1:]:
						embeds[-1].add_field(name = "{} (continued)".format(category), value = page, inline = False)
			return embeds
		elif isinstance(self.command, Command):
			# <signature portion>
			title = self.get_command_signature()
			# <long doc> section
			if self.command.help:
				description_paginator.add_line(self.command.help, empty = True)
			# end it here if it's just a regular command
			if not self.has_subcommands() or not list(filtered_command_list):
				description_paginator.close_page()
				if not self.command.help:
					return [discord.Embed(title = title, description = self.command.description, color = self.embed_color)]
				elif len(self.command.help) <= 2048:
					description = clients.code_block.format(self.command.help) if "  " in self.command.help else self.command.help
					description += "\n" + self.command.description
					return [discord.Embed(title = title, description = description, color = self.embed_color)]
				return self.embeds(title, description_paginator)
			subcommands = sorted(filtered_command_list, key = lambda c: c[0])
			subcommand_lines = self.generate_subcommand_lines(max_width, subcommands)
			if (not self.command.help or len(self.command.help) <= 2048) and len('\n'.join(subcommand_lines)) <= self.embed_field_limit - 8:
			# 8: len("```\n") * 2
				embed = discord.Embed(color = self.embed_color)
				value = "{}\n".format(description_paginator.pages[0]) if description_paginator.pages else ""
				value += self.command.description
				if not value:
					embed.title = title
				else:
					embed.add_field(name = title, value = value, inline = False)
				embed.add_field(name = "Subcommands for {}".format(self.command), value = clients.code_block.format('\n'.join(subcommand_lines)), inline = False)
				return [embed]
			description_paginator.add_line("Subcommands for {}:".format(self.command))
			self._add_subcommands_to_page(max_width, subcommands, description_paginator)
		else: # cog
			description = inspect.getdoc(self.command)
			if description:
				# <description> portion
				description_paginator.add_line(description, empty = True)
			title = "{} Commands".format(type(self.command).__name__)
			subcommands = sorted(filtered_command_list, key = lambda c: c[0])
			self._add_subcommands_to_page(max_width, subcommands, description_paginator)
		return self.embeds(title, description_paginator)
	
	def _add_subcommands_to_page(self, max_width, commands, paginator):
		for line in self.generate_subcommand_lines(max_width, commands):
			paginator.add_line(line)
	
	@staticmethod
	def generate_subcommand_lines(max_width, commands):
	# def _subcommands_lines(max_width, commands, indent = True):
		lines = []
		for name, command in commands:
			if name in command.aliases: # skip aliases
				continue
			line = '{0:<{width}}  {1}'.format(name, command.short_doc, width = max_width)
			# line = '{indent}{0:<{width}}  {1}'.format(name, command.short_doc, width = max_width, indent = "  " if indent else "")
			if len(line) <= 55:
				lines.append(line)
			else:
				cutoff = line[:55].rfind(' ')
				lines.append(line[:cutoff])
				while cutoff + 55 < len(line):
					new_cutoff = line[:cutoff + 55].rfind(' ')
					lines.append(' ' * (max_width + 2) + line[cutoff + 1:new_cutoff])
{'fraction_non_alphanumeric': 0.11794112201444178, 'fraction_numerical': 0.010183299389002037, 'mean_word_length': 4.431372549019608, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022190', 'n_tokens_mistral': 3993, 'n_tokens_neox': 3374, 'n_words': 781}