def create_chain(self): """Creates Markov chain from messages stored in the sqlite db and generates sentence. Returns: markov_sentence (str): Markov chain generated sentence. """ start_words = [] word_dict = {} flag = 1 count = 0 db = Database() messages = db.get_items() for item in messages: # reformat messages and split the words into lists temp_list = item.split() # add the first word of each message to a list if ( len(temp_list) > 0 and temp_list[0].lower() != self.bot.user.name and not temp_list[0].isdigit() ): start_words.append(temp_list[0]) # create a dictionary of words that will be used to form the sentence for index, item in enumerate(temp_list): # add new word to dictionary if temp_list[index] not in word_dict: word_dict[temp_list[index]] = [] # add next word to dictionary if ( index < len(temp_list) - 1 and temp_list[index + 1].lower() != self.bot.user.name and not temp_list[index + 1].isdigit() ): word_dict[temp_list[index]].append(temp_list[index + 1]) # choose a random word to start the sentence curr_word = random.choice(start_words) sentence = '' # loop through the chain while flag == 1 and count < 100: # add word to sentence count += 1 sentence += curr_word + ' ' # choose a random word if len(word_dict[curr_word]) != 0: curr_word = random.choice(word_dict[curr_word]) # nothing can follow the current word, end the chain elif len(word_dict[curr_word]) == 0: flag = 0 # format final sentence markov_sentence = self.format_sentence(sentence) return markov_sentence
class Account(commands.Cog): def __init__(self, bot): self.bot = bot self.db = Database() self.db.setup() @commands.command() @commands.cooldown(1, 10, commands.BucketType.user) async def setaccount(self, ctx, steam_id): """!setaccount <steam_id> - Register Steam ID to use PB commands.""" if not steamid.is_valid_steamid(steam_id): embed = discord.Embed( colour=discord.Colour.darker_grey(), description='Invalid *<steam_id>* for **!setaccount**') return await ctx.send(embed=embed) embed = discord.Embed(colour=discord.Colour.blue(), title='Account Registration') embed.set_thumbnail(url=ctx.author.avatar_url) embed.add_field(name='Player', value=ctx.author) embed.add_field(name='Steam ID', value=steam_id) discord_id = str(ctx.author) if self.db.get_account(discord_id): self.db.update_item(discord_id, steam_id) else: self.db.add_item(discord_id, steam_id) return await ctx.send(embed=embed)
class Pb(commands.Cog): def __init__(self, bot): self.bot = bot self.db = Database() @commands.command() @commands.cooldown(1, 10, commands.BucketType.user) async def pb(self, ctx, mapname, mode='kzt', runtype='pro'): """!pb <map> <mode> <runtype> - Get personal best time for map.""" embed = discord.Embed(colour=discord.Colour.darker_grey()) discord_id = str(ctx.author) account = self.db.get_account(discord_id) if not account: embed.description = ( f'{ctx.author.name},\n' 'register your Steam ID with **!setaccount** *<steam_id>*\n' 'before using personal best commands.') return await ctx.send(embed=embed) steam_id = account[1] mapname = mapname.lower() mode = mode.lower() runtype = runtype.lower() if not kzapi.valid_search_records(mapname, mode, runtype): embed.description = 'Invalid search parameters for **!pb**' return await ctx.send(embed=embed) data = kzapi.get_pb(steam_id, mapname, mode, runtype) if not data: embed.description = f'Search for **!pb** *{mapname} {mode} {runtype}* failed.' return await ctx.send(embed=embed) player = data[0]['player_name'] mode = kzapi.MODES_ALT[data[0]['mode']] time = kzapi.convert_time(data[0]['time']) teleports = data[0]['teleports'] server = data[0]['server_name'] date = data[0]['created_on'][:10] info = (f'Map: {mapname}\n' f'Difficulty: {kzapi.MAPS[mapname]}\n' f"Mode: {mode.upper()}\n" f"Runtype: {runtype.upper()}") embed = discord.Embed(colour=discord.Colour.blue(), title='Personal Best', description=info) embed.set_thumbnail(url=f'{kzapi.MAP_IMG_URL}{mapname}.jpg') embed.add_field(name='Player', value=player) embed.add_field(name='Time', value=time) embed.add_field(name='Teleports', value=teleports) embed.add_field(name='Server', value=server) embed.add_field(name='Date', value=date) await ctx.send(embed=embed)
class ChatLog(commands.Cog): def __init__(self, bot): self.bot = bot self.db = Database() self.db.setup() @commands.Cog.listener() async def on_message(self, message): ctx = await self.bot.get_context(message) if ctx.valid or message.author.bot: return else: record = self.clean_message(message.clean_content) if record: self.db.add_item(record) def clean_message(self, message): """Cleans the message of unnecessary information. Args: message (str): Message from the chat to modify. Returns: text (str): Text that has URLs, mentions, and punctuation removed. """ text = message.lower() text = text.replace('codex', '') text = re.sub(r'''(\s)\#\w+''', '', text) text = re.sub( r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)''' r'''(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+''' r'''(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|''' r'''[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', '', text) depunctuate = str.maketrans('', '', string.punctuation) text = text.translate(depunctuate) text = ' '.join(text.split()) return text
class Rank(commands.Cog): def __init__(self, bot): self.bot = bot self.db = Database() @commands.command() @commands.cooldown(1, 10, commands.BucketType.user) async def rank(self, ctx, mode='kzt', runtype='pro'): """!rank <mode> <runtype> - Get personal rank on points leaderboard.""" embed = discord.Embed(colour=discord.Colour.darker_grey()) discord_id = str(ctx.author) account = self.db.get_account(discord_id) if not account: embed.description = ( f'{ctx.author.name},\n' 'register your Steam ID with **!setaccount** *<steam_id>*\n' 'before using personal best commands.') return await ctx.send(embed=embed) mode = mode.lower() if not kzapi.valid_search_leaderboard(mode, runtype): embed.description = 'Invalid search parameters for **!rank**' return await ctx.send(embed=embed) steam_id = account[1] steam64 = steamid.steamid_to_steam64(steam_id) data = kzapi.get_rank(steam64, mode, runtype) if not data: embed.description = f'Search for **!rank** *{mode} {runtype}* failed.' return await ctx.send(embed=embed) player = data[0]['player_name'] points = data[0]['points'] average = data[0]['average'] finishes = data[0]['finishes'] info = (f'Mode: {mode.upper()}\n' f'Runtype: {runtype.upper()}') embed = discord.Embed(colour=discord.Colour.blue(), title='Rank', description=info) embed.set_thumbnail(url=ctx.author.avatar_url) embed.add_field(name='Player', value=player, inline=False) embed.add_field(name='Points', value=points) embed.add_field(name='Average', value=average) embed.add_field(name='Map Completions', value=finishes) await ctx.send(embed=embed)
class JumpPb(commands.Cog): def __init__(self, bot): self.bot = bot self.db = Database() @commands.command() @commands.cooldown(1, 10, commands.BucketType.user) async def jumppb(self, ctx, bindtype='nobind'): """!jumppb <bind/nobind> - Get personal best jumpstats.""" embed = discord.Embed(colour=discord.Colour.darker_grey()) discord_id = str(ctx.author) account = self.db.get_account(discord_id) if not account: embed.description = ( f'{ctx.author.name},\n' 'register your Steam ID with **!setaccount** *<steam_id>*\n' 'before using personal best commands.') return await ctx.send(embed=embed) bindtype = bindtype.lower() if not kzapi.valid_search_jumppb(bindtype): embed.description = 'Invalid search parameters for **!jumppb**' return await ctx.send(embed=embed) steam_id = account[1] jumptypes = '' distances = '' strafes = '' for jumptype in kzapi.JUMPTYPES_ALT: data = kzapi.get_jumppb(steam_id, jumptype, bindtype) jumptypes += f'{jumptype}\n' distances += f"{data[0]['distance']}\n" if data else 'N/A\n' strafes += f"{data[0]['strafe_count']}\n" if data else 'N/A\n' embed = discord.Embed( colour=discord.Colour.blue(), title=f'Personal Best Jumpstats', description= f"Type: {'No Bind' if bindtype == 'nobind' else 'Binded'}") embed.set_thumbnail(url=ctx.author.avatar_url) embed.add_field(name='Player', value=ctx.author.name, inline=False) embed.add_field(name='Jumptype', value=jumptypes) embed.add_field(name='Distance', value=distances) embed.add_field(name='Strafes', value=strafes) await ctx.send(embed=embed)
def __init__(self, bot): self.bot = bot self.db = Database()