async def start_challonge(command, msg, channel, guild): # Base url to access Challonge's API try: tour_url = get_chal_tour_id(read_db('channel', 'bracket', channel.id)) # Grab tour_url from bracket command except: raise Exception(bold("Challonge") + ": Bracket link is missing. Try setting the bracket command") subdomain = read_db('guild', 'challonge', guild) # Server's subdomain with Challonge # Properly add the subdomain to the bracket url if subdomain: tour_url = subdomain + '-' + tour_url # Get the participants for the tournament parts_get = requests_get(base_url + tour_url + "/participants.json", headers={"User-Agent":"Lizard-BOT"}, auth=(chal_user, api_key)) # Check the status codes # Anything but a 200 is bad if '200' in str(parts_get.status_code): return parts_get.json(), tour_url # 404 usually means a bad bracket link elif '404' in str(parts_get.status_code): raise Exception(bold("Challonge") + ": Lizard-BOT can not find tournament: " + tour_url) # Challonge error # Usually a 5xx error else: print(parts_get.text) # Print raw text to console raise Exception(bold("Challonge") + ": Unknown Challonge error for <" + tour_url + "> initializing connection to Challonge")
async def status(command, msg, user, channel, *args, **kwargs): currentRound = read_db('channel', 'round', channel.id) if currentRound: # Read the status message for a channel and make it bold # Currently the message must have {0} so it can fill in the current round try: message = read_db('channel', 'status', channel.id) return bold(message.format(currentRound)) except: raise Exception( bold("Status") + ": Round message includes invalid {}. Please correct the status message to include only {0}" ) return bold( "Tournament has not begun. Please wait for the TOs to start Round 1!")
async def user_has_permission(self, user, command, id): ''' Performs various checks on the user and the command to determine if they're allowed to use it. ''' # Check if the user is an admin and # if the command is an admin command. if command in self.admin_commands: botrole = read_db('guild', 'botrole', id) # If botrole is not set, allow the command if not botrole: return True # Find if the user has botrole for role in user.roles: if role.id == botrole: return True return False # Check if the command is a dev command elif command in self.dev_commands: # If the user is a dev and that the command was sent from the proper server, give access to the dev commands if user.id in dev_ids and id in dev_guild: return True return False # User passed all the tests so they're allowed to # call the function. return True
async def challonge_seeding(command, msg, user, channel, *args, **kwargs): if len(msg.split(' ')) > 1: raise Exception(bold("Challonge_Seeding") + ": Too many arguments. " + await help_lizard('','','','')) async with channel.typing(): parts, tour_url = await start_challonge(command, msg, channel, kwargs['guild']) # If there is no message, seed whole bracket msg # Else, grab the first param try: if not msg: seed_num = 0 # Error on negative number elif int(msg.split(' ')[-1]) <= 0: raise else: seed_num = int(msg.split(' ')[-1]) except: raise Exception(bold("Challonge_Seeding") + ": Seeding number must be a positive integer or 0 for everybody") # Get Google Sheets ID sheet_id = read_db('channel', 'seeding', channel.id) # If seeding hasn't been set, inform user if not sheet_id: raise Exception(bold("Challonge_Seeding") + ": There is no seeding sheet for this channel. Please view <https://github.com/lizardman301/Lizard-bot-rsf/blob/master/doc/seeding_with_sheets.md> for a walkthrough") # Grab what the seeds were seeds = seeding(sheet_id, parts, base_url + tour_url, seed_num) # Seeding takes place in different method await channel.send("**SEEDING:**\n {0}".format(',\n'.join(escape_markdown(pformat(seeds))[1:-1].split(', ')))) # Final message that seeding is complete return bold("SEEDING IS NOW COMPLETE!\nPLEASE REFRESH YOUR BRACKETS\nWAIT FOR THE ROUND 1 ANNOUNCEMENT TO START PLAYING")
async def botrole(command, msg, user, channel, *args, **kwargs): botrole = read_db('guild', 'botrole', kwargs['guild']) try: botrole_name = channel.guild.get_role(botrole).name except: botrole_name = 'everyone' # Pull the role name from the guild's roles return "The bot role is {0}".format(bold(botrole_name))
async def on_message(message): command = 'bracket' # Pre-setting the variable for use in the try/except block below (L78) # If the bot is the user, do not respond if message.author == client.user: return # If the bot is mentioned in a message, respond with a message informing it of being a bot if client.user.mentioned_in(message): # If @everyone or @here is used, ignore if "@everyone" in message.content or "@here" in message.content: return # Choose from a random response, then follow with a Bot message responses = ["Ok", "Thanks", "Sounds good to me", "Buff Rashid", "Buff Rashid", "Beep Boop", "Yes", "No", "Good to know", "Glad to hear it", "I'll keep that in mind", "The answer lies in the heart of battle", "Go home and be a family man"] await message.channel.send("{0} \n**I am a Bot that plays Rashid. Mentions cause my little Rashid brain to short circuit. Did you have ~~an eagle spi~~ a command?**".format(random_choice(responses))) return try: # Check if the channel is in the DB # Add it if it isn't if not settings_exist(message.guild.id, message.channel.id): raise Exception("Lizard-BOT failed to create DB entry for: " + message.guild.name + ". Guild ID: " + message.guild.id) # Get prefix for the guild prefix = read_db('guild', 'prefix-lizard', message.guild.id) # Check if the attempted_cmd is !prefix-lizard and has too many args if (message.content.split(' ')[0] == "!prefix-lizard" or message.content.split(' ')[0] == "!prefliz") and len(message.content.split()) > 1: await message.channel.send("Too many arguments. Check help-lizard for more info") return # Hardcode prefix command to be accessible via ! elif message.content.split(' ')[0] == "!prefix-lizard" or message.content.split(' ')[0] == "!prefliz": response = await client.interface.call_command('prefix-lizard', 0, 0, 0, guild=message.guild.id) if response: await message.channel.send(response) return # If other commands don't start with the correct prefix, do nothing elif not message.content.startswith(prefix): return # Check if the attempted_cmd takes arguments elif message.content.split(' ')[0][1:].lower() in client.no_arg_cmds and len(message.content.split()) > 1: await message.channel.send("Too many arguments. Check help-lizard for more info") return # Rotate through commands to see if the message matches for command in client.commands: command = command.lower() # Lower the command for easier matching msg = message.content # The message attempted_cmd = msg.split(' ')[0][1:].lower() # Get the attempted command from the beginning of the string if attempted_cmd in ['challonge', 'chal', 'edit'] and len(msg.split(' ')) > 1: attempted_cmd += ' ' + msg.split(' ')[1].lower() # Check if the message begins with a command if attempted_cmd and attempted_cmd == command: user = message.author # The author kwargs = {'guild':message.guild.id} # Remove the command from the start msg = msg[len(command)+1:].strip() # Check command to see if we need keyword args if command in ['challonge checkin', 'chal checkin']: kwargs['guild_members'] = message.guild.members elif command in ['edit botrole', 'edit role']: kwargs['guild_default_role'] = message.guild.default_role kwargs['role_mentions'] = message.role_mentions elif command in ['edit bracket', 'edit pingtest', 'edit status', 'edit seeding', 'edit stream', 'edit tos']: kwargs['channel_mentions'] = message.channel_mentions if command in ['edit tos']: kwargs['mentions'] = message.mentions elif command in ['draw']: kwargs['full_msg'] = message kwargs['client'] = client # Await the interface calling the command response = await client.interface.call_command(command, msg, user, message.channel, **kwargs) # If there is a response, send it if response: await message.channel.send(response) break except Exception: string_info = str(sys_exc_info()[1]) # Error message function_name = string_info.split(':')[0] # The command the error message came from # Expected error # Return friendly user message # Additional checks needed for challonge and edit commands that have multiple subcommands if client.interface._func_mapping[command].__name__ in function_name.strip("*").lower() or ('challonge' in client.interface._func_mapping[command].__name__ and 'challonge' in function_name.strip("*").lower()) or ('edit' in client.interface._func_mapping[command].__name__ and 'edit' in function_name.strip("*").lower()): await message.channel.send(function_name.replace('_', '-') + ': ' + ':'.join(string_info.split(':')[1:])) elif command == 'dev': # Return user message await message.channel.send(escape_markdown(traceback_format_exc())) # Print error to console traceback_print_exc() else: # Print error to console traceback_print_exc() # If we get this far and something breaks # Something is very wrong. Send user generic error message await message.channel.send("I is broken.\nBuff Rashid and submit an issue via <https://github.com/lizardman301/Lizard-bot-rsf/issues>\nOr just tell Lizardman301. That's what I do.")
async def TOs(command, msg, user, channel, *args, **kwargs): tos = read_db('channel', 'tos', channel.id) # If we get a value back, return TOs if tos: return tos return "There are no TOs associated with this channel."
async def stream(command, msg, user, channel, *args, **kwargs): # Custom message for a stream return read_db('channel', 'stream', channel.id)
async def prefix(command, msg, user, channel, *args, **kwargs): # Gets the prefix set for a guild return "The prefix is: {0}".format( read_db('guild', 'prefix-lizard', kwargs['guild']))
async def pingtest(command, msg, user, channel, *args, **kwargs): # Custom message used to teach users how to do a ping test return read_db('channel', 'pingtest', channel.id)
async def bracket(command, msg, user, channel, *args, **kwargs): # Custom message for setting a bracket return read_db('channel', 'bracket', channel.id)