async def get(self, ctx, eventID): logCommand(ctx) regex = re.compile('#\d{4}$') if not regex.match(eventID): await ctx.send( f"Events must be specified with an eventID; e.g '#1234'.") return regex = re.compile(f'.*{eventID}\.json$') eventFile = None for root, dirs, files in os.walk(EVENT_DIR): for file in files: if regex.match(file): eventFile = file if eventFile: with open(f"{EVENT_DIR}{eventFile}") as file: data = json.load(file) await ctx.send(embed=self.buildEventEmbed(data)) else: logger.info(f"Did not find any event called: {eventID}.") await ctx.send(f"No event found called: {eventID}.")
async def destroy(self, ctx, event): logCommand(ctx) if self.getEventByID(event): with open(f"{EVENT_DIR}{self.getEventByID(event)}") as file: data = json.load(file) if data['organiser'] == ctx.author.name: os.remove(f"{EVENT_DIR}{self.getEventByID(event)}") logger.info(f"{event} removed by {ctx.author.name}") await ctx.send(f"Event {data['id']} has been removed.") channel = self.bot.get_guild(EVENT_GUILD).get_channel( EVENT_CHANNEL) await channel.send( f"{ctx.author} has removed event: {data['id']}.") return else: logger.info( f"{ctx.author.name} is not authorized to remove event: {event}." ) await ctx.send( f"Only the organiser of the event({data['organiser']}) may remove it." ) return else: await ctx.send(f"Found no event called: {event}") return
async def quote40k(self, ctx): logger.info(f"{ctx.author} requested a quote.") quotes = open(WARHAMMER_QUOTES,"r") response = random.choice(quotes.readlines()).rstrip('\n') quotes.close() await ctx.send(response)
async def join(self, ctx, eventID, *, args): logCommand(ctx) # Attempt to load the event by eventId. eventFile = self.getEventByID(eventID) # If no event was loaded, assume there is not event by that eventId. # Give feedback to caller and stop. if not eventFile: await ctx.send(f"No event found matching: {eventID}") return # If an event was loaded, we assume it is well formatted and proceed to # add the user to the participant list. else: # Load the event json so that we may operate on it. with open(f"{EVENT_DIR}{eventFile}") as file: data = json.load(file) # If the caller is already in the participant list, then there is nothing to be done. # Give feeback to the caller and stop. if ctx.author.id in data['participants']: await ctx.send( f"{ctx.author.name} is already participating in event: {eventID}" ) return # The caller is not already in the participant list. # We now add them to the list. else: # Append the callers discordID to the participants array. data['participants'].append(ctx.author.id) # Override the previous event data with our updated json. with open(f"{EVENT_DIR}{eventFile}", "w") as file: json.dump(data, file) # Log the update, and give feedback to the caller. logger.info(f"Added {ctx.author.name} to event: {eventID}.") await ctx.send( f"Added {ctx.author.name} to event: {data['name']} - {eventID}." ) # Unless the command was passed with a "-silent" argument, post the update to the EVENT_CHANNEL. if not "-silent" in shlex.split(args): channel = self.bot.get_guild(EVENT_GUILD).get_channel( EVENT_CHANNEL) await channel.send( f"{ctx.author.name} has joined event: {data['name']} - {eventID}" )
async def leave(self, ctx, eventID, *, args): logCommand(ctx) # Attempt to load event. eventFile = self.getEventByID(eventID) # If no event found, assume the eventID was wrong. # Give feedback to caller and stop. if not eventFile: await ctx.send(f"No event found matching: {eventID}") return # Assume that we got a well formatted event and proceed. else: # Load event json so that we may operate on it. with open(f"{EVENT_DIR}{eventFile}") as file: data = json.load(file) # Check if the author is in the participant list. # If not, give feedback to the caller and stop. if not ctx.author.id in data['participants']: await ctx.send( f"{ctx.author.name} is not participating in event: {eventID}." ) return # Since the caller is in the participant list, proceed to remove them. else: # Remove the caller's discordID from the participants array. data['participants'].remove(ctx.author.id) # Override the event json with our updated data. with open(f"{EVENT_DIR}{eventFile}", "w") as file: json.dump(data, file) # Log the removal, and give feedback to the caller. logger.info( f"Removed [ctx.author.name] from event: {data['name']} - {eventID}." ) await ctx.send( f"{ctx.author.name} is no longer participating in event: {eventID}." ) # Unless the command was invoked with the "-silent" argument, post the event to the EVENT_CHANNEL. if not "-silent" in shlex.split(args): channel = self.bot.get_guild(EVENT_GUILD).get_channel( EVENT_CHANNEL) await channel.send( f"{ctx.author.name} has joined event: {data['name']} - {eventID}" )
async def skills(self, ctx, *, query=None): logCommand(ctx) with open(NC_SKILLS) as file: skillList = json.load(file) uniqueSkillGroup = sorted( set([skillList[skill]['skill_set'] for skill in skillList])) # Case 1: Invoked with no command, or the 'list' argument # Show the invoker a list of all available skills if query == None or query == "list": listEmbed = discord.Embed( title=f"Necromunda Skill List", color=discord.Color.blue(), description=f"The following Skillset and skills are loaded") for skillgroup in uniqueSkillGroup: formattedskills = '\n'.join([ skillList[skill]['name'] for skill in skillList if skillList[skill]['skill_set'] == skillgroup ]) listEmbed.add_field(name=f'{skillgroup}', inline=True, value=f"{formattedskills}") await ctx.send(embed=listEmbed) # Case 2: Invoked with a skill set. # NOTE: Because Skill Sets are values, we shift the query into Title-case to match our values. elif query.title() in uniqueSkillGroup: output = "" for entry in [[ skillList[skill]['skill_number'], skillList[skill]['name'] ] for skill in skillList if skillList[skill]['skill_set'] == query.title()]: output += f"{entry[0]} - {entry[1]}\n" listEmbed = discord.Embed( title=f'Necromunda Skill Set: {query}', color=discord.Color.blue(), description= f'The {query} skill set contains the following skills:\n\n' + output) await ctx.send(embed=listEmbed) # Case 3: Invoked with a specific skill elif query in skillList: listEmbed = discord.Embed( title=f"Necromunda skill: {skillList[query]['name']}", color=discord.Color.blue(), description=f"{skillList[query]['definition']}") listEmbed.add_field(name='Skill Set', inline=True, value=f"{skillList[query]['skill_set']}") listEmbed.add_field(name='Skill Number', inline=True, value=f"{skillList[query]['skill_number']}") listEmbed.set_footer(text=f"Source: {skillList[query]['source']}") await ctx.send(embed=listEmbed) # Case 4: No hit in either the skill sets or skill list; lets try a regex match or bail with an apology else: logger.info( f"No hit: Did not find term: {query} in Necromunda_Skills.json" ) termlist = [element for element in skillList] regex = re.compile(f".*{query}.*") resultlist = list(filter(regex.match, termlist)) if resultlist: response = "```" for term in resultlist: response += f"- {skillList[term]['name'].ljust(22)}{skillList[term]['skill_set']}\n" response += "```" embedResult = discord.Embed( title=f"No hits for {query} in Necromunda Skills", color=discord.Color.red(), description= f"No exact match found for {query}, but there were some partial hits:" ) embedResult.add_field(name="Partial hits", inline=False, value=response) await ctx.send(embed=embedResult) else: embedResult = discord.Embed( title=f"No hits at all for {query} in Necromunda Skills", color=discord.Color.red(), description= f"No hits at all for {query}; Perhaps it's called something else?\n\nTry '!nc skills list' for a list of all loaded skills." ) await ctx.send(embed=embedResult)