def db_up(self, event): """Upload a JSON file to the DB""" games_json = {} found_game = False for attachment in event.msg.attachments.values(): if '.json' in attachment.filename.lower(): found_game = True response = requests.get(attachment.url) games_json = response.json() for game in games_json: if not Game.get_game_by_name(game): game_name = game game = games_json[game] Game.create( name=game_name, alias=game['alias'], desc=game['desc'], join_role=game['join_role'], time_message=game['time_msg'], a_channel=game['a_channel'], a_message=game['a_message'], interval=game['interval'], attendees=game['attendees'], join_src=game['join_src'], play_src=game['play_src'], last_announcement=datetime.utcnow() ) event.msg.reply('Created game {}'.format(game_name)) else: event.msg.reply('Game {} already existed.'.format(game)) event.msg.reply('Done') if not found_game: event.msg.reply('You need to upload a JSON file!')
def clear_game_command(self, event, game): """Clears attendees for game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) # type: Game if not game: event.msg.reply( 'Game not found, check your spelling and try again') return query = Game.update(attendees=[]) query.where(Game.name == game.name).execute() event.msg.reply('Okay! Cleared attendee list for {}'.format(game.name))
def add_game(self, event, name, desc, create_channels=False): """ Sets up a new game """ if create_channels: channel_name = name.replace(' ', '-') guild = event.msg.guild category = guild.create_category(channel_name) a_channel = category.create_text_channel('announcements') a_channel.topic = desc Game.new(name=name, desc=desc, ac=a_channel.id if create_channels else None)
def start_command(self, event, game): """Executes starting code for a game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) if not game: return event.msg.reply( 'Game not found, check your spelling and try again') return game.execute(event, exec_type='start')
def join_command(self, event, game): """Adds a user to a game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) if not game: return event.msg.reply( 'Game not found, check your spelling and try again') return game.execute(event)
def queue_announcements(self): try: next_trigger = Game.select().order_by( (Game.next_announcement).asc()).where( Game.next_announcement != None).limit(1).get() except Game.DoesNotExist: return self.announce_task.set_next_schedule(next_trigger.next_announcement)
def set_command(self, event, game, key, value): """Sets a value on a game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) if not game: return event.msg.reply( 'Game not found, check your spelling and try again') if key == 'a_m' or key == 'a_message': query = Game.update(a_message=value) elif key == 'a_c' or key == 'a_channel': query = Game.update(a_channel=value) elif key == 'alias': if ' ' in value: return event.msg.reply('Alias can\'t contain spaces!') query = Game.update(alias=value) elif key == 'role': query = Game.update(join_role=value) elif key == 'time_msg': query = Game.update(time_message=value) else: return event.msg.reply( 'Invalid key, check your spelling and try again') query.where(Game.name == game.name).execute() return event.msg.reply('Okay! Set value {} for key {} on {}'.format( value, key, game.name))
def code_get_command(self, event, src_type, game): """Gets join or start code for a game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) # type: Game if not game: event.msg.reply( 'Game not found, check your spelling and try again') return if src_type == 'join': event.msg.reply('```{}```'.format(game.join_src)) elif src_type == 'start': event.msg.reply('```{}```'.format(game.play_src)) else: event.msg.reply('Either `!code set join` or `!code set start`')
def trigger_announcement(self): games = Game.select().where( Game.next_announcement < (datetime.utcnow() + timedelta(seconds=1))) for game in games: # type: Game message = game.a_message channel = self.state.channels.get(game.a_channel) if not channel: self.log.warning( 'Not triggering announcement, channel %s was not found!', game.a_channel) channel.send_message(message) game.set_interval(game.interval) next_announcement = Eventual(game.announce_next(self)) next_announcement.set_next_schedule(datetime.utcnow() + timedelta(days=2)) self.queue_announcements()
def on_reaction_add(self, event): if event.user_id == self.bot.client.state.me.id: return if 'team' in event.emoji.name.lower(): team = event.emoji.name.lower().replace('mmteam', '') reply = self.join_team(team, event.guild.members[event.user_id].user) else: game_name = EMOJIS[event.emoji.id].replace('MM', '') game = Game.get_game_by_name(game_name) # type: Game if not game: reply = 'Game {} not found, check your spelling and try again'.format( game_name) else: reply = game.execute(event, mode=ExecMode.reaction) event.guild.members[event.user_id].user.open_dm().send_message(reply) event.delete()
def db_down(self, event): """Download all games in the DB as JSON file""" games_json = {} games = Game.select() for game in games: # type: Game game_json = { 'alias': game.alias, 'desc': game.desc, 'join_role': game.join_role, 'time_msg': game.time_message, 'a_channel': game.a_channel, 'a_message': game.a_message, 'interval': game.interval, 'attendees': game.attendees, 'join_src': game.join_src, 'play_src': game.play_src } games_json[game.name] = game_json event.msg.reply('Here is your file!', attachments=[('database.json', json.dumps(games_json))])
def code_set_command(self, event, src_type, game): """Sets join or start source for a game""" game = game.replace('_', ' ') game = Game.get_game_by_name(game) # type: Game if not game: event.msg.reply( 'Game not found, check your spelling and try again') return src = event.codeblock if src_type == 'join': game.set_exec_code(src) event.msg.reply('Okay! Set ```{}``` as join code for {}'.format( src, game.name)) elif src_type == 'start': game.set_exec_code(src, exec_type=ExecType.start) event.msg.reply('Okay! Set ```{}``` as start code for {}'.format( src, game.name)) else: event.msg.reply('Either `!code set join` or `!code set start`')
def schedule_command(self, event, game, interval): """Sets schedule for a game""" game = Game.get_game_by_name(game) if not game: event.msg.reply( 'Game not found, check your spelling and try again') return if interval == 'clear': game.clear_interval() event.msg.reply('Cleared interval for {}'.format(game.name)) self.queue_announcements() return if not game.a_channel or not game.a_message: event.msg.reply( 'Please be sure to set an announcement message and channel first!' ) return game.set_interval(interval) event.msg.reply('Okay! Set interval {} for {}'.format( interval, game.name)) self.queue_announcements()