def update_slack_users(self): slack_users = {u.id: u for u in slackapi.get_user_list()} for p in Player.objects.all(): u = slack_users.get(p.slack_user_id) if u != None and u.tz_offset != (p.timezone_offset and p.timezone_offset.total_seconds()): p.timezone_offset = None if u.tz_offset is None else timedelta(seconds=u.tz_offset) p.save()
def update_slack_users(self): slack_users = slackapi.get_user_list() name_set = {u.name.lower() for u in slack_users} for p in Player.objects.all(): in_slack_group = p.lichess_username.lower() in name_set if in_slack_group != p.in_slack_group: p.in_slack_group = in_slack_group p.save()
def update_slack_users(self): slack_users = {u.name.lower(): u for u in slackapi.get_user_list()} for p in Player.objects.all(): u = slack_users.get(p.lichess_username.lower()) in_slack_group = u != None if in_slack_group != p.in_slack_group: with reversion.create_revision(): reversion.set_comment('Joined slack.') p.in_slack_group = in_slack_group p.save() if u != None and u.tz_offset != (p.timezone_offset and p.timezone_offset.total_seconds()): p.timezone_offset = None if u.tz_offset is None else timedelta(seconds=u.tz_offset) p.save()
def create_team_channel(self, team_ids): username_to_id = {u.name: u.id for u in slackapi.get_user_list()} intro_message = 'Welcome! This is your private team channel. Feel free to chat, study, discuss strategy, or whatever you like!\n' \ + 'You need to pick a team captain and a team name by {season_start}.\n' \ + 'Once you\'ve chosen (or if you need help with anything), contact one of the moderators:\n' \ + '{mods}' for team in Team.objects.filter(id__in=team_ids).select_related('season__league').nocache(): pairings_url = abs_url(reverse('by_league:by_season:pairings_by_team', args=[team.season.league.tag, team.season.tag, team.number])) mods = team.season.league.leaguemoderator_set.filter(is_active=True) mods_str = ' '.join(('<@%s>' % lm.player.lichess_username.lower() for lm in mods)) season_start = '?' if team.season.start_date is None else team.season.start_date.strftime('%b %-d') intro_message_formatted = intro_message.format(mods=mods_str, season_start=season_start) team_members = team.teammember_set.select_related('player').nocache() chesster_id = username_to_id['chesster'] user_ids = [username_to_id.get(tm.player.lichess_username.lower()) for tm in team_members] channel_name = 'team-%d-s%s' % (team.number, team.season.tag) try: group = slackapi.create_group(channel_name) time.sleep(1) except slackapi.NameTaken: logger.error('Could not create slack team, name taken: %s' % channel_name) continue channel_ref = '#%s' % group.name for user_id in user_ids: if user_id: try: slackapi.invite_to_group(group.id, user_id) except slackapi.SlackError: logger.exception('Could not invite %s to slack' % user_id) time.sleep(1) slackapi.invite_to_group(group.id, chesster_id) time.sleep(1) with reversion.create_revision(): reversion.set_comment('Creating slack channel') team.slack_channel = group.id team.save() slackapi.set_group_topic(group.id, pairings_url) time.sleep(1) slackapi.leave_group(group.id) time.sleep(1) slackapi.send_message(channel_ref, intro_message_formatted) time.sleep(1)