Exemplo n.º 1
0
    async def initialize(self):
        self._main_channel = server.main_channel
        self._notifications_channel = server.find_channel(
            channel_name=Config.NOTIFICATIONS_CHANNEL_NAME)
        self._schedule_channel = server.find_channel(channel_name='schedule')
        self._client = server.client

        await self.update_schedule_channel()
Exemplo n.º 2
0
async def get_matches_with_channels(racer: NecroUser = None) -> list:
    """
    Parameters
    ----------
    racer: NecroUser
        The racer to find channels for. If None, finds all channeled matches.
    
    Returns
    -------
    list[Match]
        A list of all Matches that have associated channels on the server featuring the specified racer.
    """
    matches = []
    if racer is not None:
        raw_data = await matchdb.get_channeled_matches_raw_data(
            must_be_scheduled=False, order_by_time=False, racer_id=racer.user_id
        )
    else:
        raw_data = await matchdb.get_channeled_matches_raw_data(must_be_scheduled=False, order_by_time=False)

    for row in raw_data:
        channel_id = int(row[13])
        channel = server.find_channel(channel_id=channel_id)
        if channel is not None:
            match = await make_match_from_raw_db_data(row=row)
            matches.append(match)
        else:
            console.warning('Found Match with channel {0}, but couldn\'t find this channel.'.format(channel_id))

    return matches
Exemplo n.º 3
0
async def delete_all_match_channels(log=False, completed_only=False) -> None:
    """Delete all match channels from the server.
    
    Parameters
    ----------
    log: bool
        If True, the channel text will be written to a log file before deletion.
    completed_only: bool
        If True, will only find completed matches.
    """
    for row in await matchdb.get_channeled_matches_raw_data():
        match_id = int(row[0])
        channel_id = int(row[13])
        channel = server.find_channel(channel_id=channel_id)
        delete_this = True
        if channel is not None:
            if completed_only:
                match_room = Necrobot().get_bot_channel(channel)
                if match_room is None or not match_room.played_all_races:
                    delete_this = False

            if delete_this:
                if log:
                    await writechannel.write_channel(
                        client=server.client,
                        channel=channel,
                        outfile_name='{0}-{1}'.format(match_id, channel.name)
                    )
                await server.client.delete_channel(channel)

        if delete_this:
            await matchdb.register_match_channel(match_id, None)
Exemplo n.º 4
0
async def make_private_room(race_private_info, discord_member):
    # Define permissions
    deny_read = discord.PermissionOverwrite(read_messages=False)
    permit_read = discord.PermissionOverwrite(read_messages=True)

    # Make a channel for the room
    # noinspection PyUnresolvedReferences
    race_channel = await server.client.create_channel(
        server.server,
        get_raceroom_name(race_private_info.race_info),
        discord.ChannelPermissions(target=server.server.default_role,
                                   overwrite=deny_read),
        discord.ChannelPermissions(target=server.server.me,
                                   overwrite=permit_read),
        discord.ChannelPermissions(target=discord_member,
                                   overwrite=permit_read),
        type=discord.ChannelType.text)

    if race_channel is None:
        return None

    # Put the race channel in the races category
    race_channel_category = server.find_channel(
        channel_name=Config.RACE_CHANNEL_CATEGORY_NAME)
    if race_channel_category is not None:
        await discordutil.set_channel_category(channel=race_channel,
                                               category=race_channel_category)

    new_room = PrivateRaceRoom(race_discord_channel=race_channel,
                               race_private_info=race_private_info,
                               admin_as_member=discord_member)
    await new_room.initialize()
    Necrobot().register_bot_channel(race_channel, new_room)

    return race_channel
Exemplo n.º 5
0
async def get_upcoming_and_current() -> list:
    """    
    Returns
    -------
    list[Match]
        A list of all upcoming and ongoing matches, in order. 
    """
    matches = []
    for row in await matchdb.get_channeled_matches_raw_data(must_be_scheduled=True, order_by_time=True):
        channel_id = int(row[13]) if row[13] is not None else None
        if channel_id is not None:
            channel = server.find_channel(channel_id=channel_id)
            if channel is not None:
                match = await make_match_from_raw_db_data(row=row)
                if match.suggested_time is None:
                    console.warning('Found match object {} has no suggested time.'.format(repr(match)))
                    continue
                if match.suggested_time > pytz.utc.localize(datetime.datetime.utcnow()):
                    matches.append(match)
                else:
                    match_room = Necrobot().get_bot_channel(channel)
                    if match_room is not None and await match_room.during_races():
                        matches.append(match)

    return matches
Exemplo n.º 6
0
async def make_match_room(match: Match, register=False) -> MatchRoom or None:
    """Create a discord.Channel and a corresponding MatchRoom for the given Match. 
    
    Parameters
    ----------
    match: Match
        The Match to create a room for.
    register: bool
        If True, will register the Match in the database.

    Returns
    -------
    Optional[MatchRoom]
        The created room object.
    """
    # Check to see the match is registered
    if not match.is_registered:
        if register:
            await match.commit()
        else:
            console.warning('Tried to make a MatchRoom for an unregistered Match ({0}).'.format(match.matchroom_name))
            return None

    # Check to see if we already have the match channel
    channel_id = match.channel_id
    match_channel = server.find_channel(channel_id=channel_id) if channel_id is not None else None

    # If we couldn't find the channel or it didn't exist, make a new one
    if match_channel is None:
        # Create permissions
        deny_read = discord.PermissionOverwrite(read_messages=False)
        permit_read = discord.PermissionOverwrite(read_messages=True)
        racer_permissions = []
        for racer in match.racers:
            if racer.member is not None:
                racer_permissions.append(discord.ChannelPermissions(target=racer.member, overwrite=permit_read))

        # Make a channel for the room
        # noinspection PyUnresolvedReferences
        match_channel = await server.client.create_channel(
            server.server,
            get_matchroom_name(match),
            discord.ChannelPermissions(target=server.server.default_role, overwrite=deny_read),
            discord.ChannelPermissions(target=server.server.me, overwrite=permit_read),
            *racer_permissions,
            type=discord.ChannelType.text)

        if match_channel is None:
            console.warning('Failed to make a match channel.')
            return None

    # Make the actual RaceRoom and initialize it
    match.set_channel_id(int(match_channel.id))
    new_room = MatchRoom(match_discord_channel=match_channel, match=match)
    Necrobot().register_bot_channel(match_channel, new_room)
    await new_room.initialize()

    return new_room
Exemplo n.º 7
0
    async def refresh(self) -> None:
        """Called when post_login_init() is run a second+ time"""
        channel_pairs = {}
        for channel, bot_channel in self._bot_channels.items():
            new_channel = server.find_channel(channel_id=channel.id)
            if new_channel is not None:
                channel_pairs[new_channel] = bot_channel
            bot_channel.refresh(new_channel)
        self._bot_channels = channel_pairs

        for manager in self._managers:
            await manager.refresh()
Exemplo n.º 8
0
 async def ne_process(self, ev: NecroEvent):
     if ev.event_type == 'rtmp_name_change':
         for row in await matchdb.get_channeled_matches_raw_data():
             if int(row[2]) == ev.user.user_id or int(row[3]) == ev.user.user_id:
                 channel_id = int(row[13])
                 channel = server.find_channel(channel_id=channel_id)
                 if channel is not None:
                     read_perms = discord.PermissionOverwrite(read_messages=True)
                     await server.client.edit_channel_permissions(
                         channel=channel,
                         target=ev.user.member,
                         overwrite=read_perms
                     )
Exemplo n.º 9
0
 async def _recover_stored_match_rooms() -> None:
     """Recover MatchRoom objects on bot init
     
     Creates MatchRoom objects for `Match`es in the database which are registered (via their `channel_id`) to
     some discord.Channel on the server.
     """
     console.info('Recovering stored match rooms------------')
     for row in await matchdb.get_channeled_matches_raw_data():
         channel_id = int(row[13])
         channel = server.find_channel(channel_id=channel_id)
         if channel is not None:
             match = await matchutil.make_match_from_raw_db_data(row=row)
             new_room = MatchRoom(match_discord_channel=channel, match=match)
             Necrobot().register_bot_channel(channel, new_room)
             await new_room.initialize()
             console.info('  Channel ID: {0}  Match: {1}'.format(channel_id, match))
         else:
             console.info('  Couldn\'t find channel with ID {0}.'.format(channel_id))
     console.info('-----------------------------------------')
Exemplo n.º 10
0
async def load_condorbot_config(necrobot):
    # PM Channel
    necrobot.register_pm_channel(CondorPMChannel())

    # Main Channel
    necrobot.register_bot_channel(server.main_channel, CondorMainChannel())

    # Admin channel
    condor_admin_channel = server.find_channel(channel_name='adminchat')
    if condor_admin_channel is None:
        console.warning('Could not find the "{0}" channel.'.format('adminchat'))
    necrobot.register_bot_channel(condor_admin_channel, CondorAdminChannel())

    # Managers (Order is very important!)
    necrobot.register_manager(LeagueMgr())
    necrobot.register_manager(MatchMgr())
    necrobot.register_manager(CondorMgr())

    # Ratings
    ratingutil.init()
Exemplo n.º 11
0
async def close_match_room(match: Match) -> None:
    """Close the discord.Channel corresponding to the Match, if any.
    
    Parameters
    ----------
    match: Match
        The Match to close the channel for.
    """
    if not match.is_registered:
        console.warning('Trying to close the room for an unregistered match.')
        return

    channel_id = match.channel_id
    channel = server.find_channel(channel_id=channel_id)
    if channel is None:
        console.warning('Coudn\'t find channel with id {0} in close_match_room '
                        '(match_id={1}).'.format(channel_id, match.match_id))
        return

    await Necrobot().unregister_bot_channel(channel)
    await server.client.delete_channel(channel)
    match.set_channel_id(None)
Exemplo n.º 12
0
async def make_room(race_info):
    # Make a channel for the room
    race_channel = await server.client.create_channel(
        server.server,
        get_raceroom_name(race_info),
        type=discord.ChannelType.text)

    if race_channel is None:
        console.warning('Failed to make a race channel.')
        return None

    # Put the race channel in the races category
    race_channel_category = server.find_channel(
        channel_name=Config.RACE_CHANNEL_CATEGORY_NAME)
    if race_channel_category is not None:
        await discordutil.set_channel_category(channel=race_channel,
                                               category=race_channel_category)

    # Make the actual RaceRoom and initialize it
    new_room = RaceRoom(race_discord_channel=race_channel, race_info=race_info)
    await new_room.initialize()

    Necrobot().register_bot_channel(race_channel, new_room)

    # Send PM alerts
    alert_pref = UserPrefs(daily_alert=None, race_alert=True)

    alert_string = 'A new race has been started:\nFormat: {1}\nChannel: {0}'.format(
        race_channel.mention, race_info.format_str)
    for member_id in await userdb.get_all_discord_ids_matching_prefs(alert_pref
                                                                     ):
        member = server.find_member(discord_id=member_id)
        if member is not None:
            try:
                await server.client.send_message(member, alert_string)
            except discord.errors.Forbidden:
                continue

    return race_channel
Exemplo n.º 13
0
async def load_necrobot_config(necrobot):
    Config.RECORDING_ACTIVATED = False

    # PM Channel
    necrobot.register_pm_channel(PMBotChannel())

    # Main Channel
    main_discord_channel = server.find_channel(
        channel_name=Config.MAIN_CHANNEL_NAME)
    if main_discord_channel is None:
        console.warning('Could not find the "{0}" channel.'.format(
            Config.MAIN_CHANNEL_NAME))
    server.main_channel = main_discord_channel
    necrobot.register_bot_channel(server.main_channel, MainBotChannel())

    # # Ladder Channel
    # ladder_admin_channel = necrobot.find_channel(Config.LADDER_ADMIN_CHANNEL_NAME)
    # if ladder_admin_channel is None:
    #     console.warning('Could not find the "{0}" channel.'.format(Config.LADDER_ADMIN_CHANNEL_NAME))
    # necrobot.register_bot_channel(ladder_admin_channel, LadderAdminChannel())

    # Managers
    necrobot.register_manager(DailyMgr())
Exemplo n.º 14
0
 def results_channel(self):
     return server.find_channel(
         channel_name=Config.RACE_RESULTS_CHANNEL_NAME)
Exemplo n.º 15
0
 def __init__(self, daily_type: DailyType):
     self._daily_type = daily_type
     self._daily_update_future = asyncio.ensure_future(self._daily_update())
     self._leaderboard_channel = server.find_channel(
         channel_name=Config.DAILY_LEADERBOARDS_CHANNEL_NAME)