Exemple #1
0
async def load_testing_config(necrobot):
    # 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))
    necrobot.register_bot_channel(main_discord_channel, MainBotChannel())

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

    # Ladder Channel
    ladder_admin_channel = server.find_channel(
        channel_name=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())
    necrobot.register_manager(MatchMgr())

    # Ratings
    ratingutil.init()
Exemple #2
0
 async def refresh(self):
     self._notifications_channel = server.find_channel(
         channel_name=Config.NOTIFICATIONS_CHANNEL_NAME)
     self._schedule_channel = server.find_channel(
         channel_name=Config.SCHEDULE_CHANNEL_NAME)
     self._client = server.client
     await self.update_schedule_channel()
Exemple #3
0
async def load_condorbot_config(necrobot):
    # PM Channel
    necrobot.register_pm_channel(CondorPMChannel())

    # Main Channel
    main_channel = server.find_channel(channel_id=Config.MAIN_CHANNEL_ID)
    if main_channel is None:
        console.warning(
            f'Could not find the main channel <#{Config.MAIN_CHANNEL_ID}>.')
    necrobot.register_bot_channel(main_channel, CondorMainChannel(ladder=True))

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

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

    # Ratings
    ratingutil.init()
Exemple #4
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()
Exemple #5
0
    async def initialize(self):
        self._main_channel = server.find_channel(channel_id=Config.MAIN_CHANNEL_ID)
        self._notifications_channel = server.find_channel(channel_name=Config.NOTIFICATIONS_CHANNEL_NAME)
        self._schedule_channel = server.find_channel(channel_name=Config.SCHEDULE_CHANNEL_NAME)
        self._client = server.client

        if Config.LEAGUE_NAME:
            try:
                await self.set_event(Config.LEAGUE_NAME)
                console.info('Event recovered: "{0}"'.format(self._event.schema_name))
            except necrobot.exception.SchemaDoesNotExist:
                console.warning('League "{0}" does not exist.'.format(Config.LEAGUE_NAME))
        else:
            console.warning('No league given in Config.')
Exemple #6
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
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:
            match_room = Necrobot().get_bot_channel(channel)
            completed = match_room is not None and match_room.played_all_races

            if completed_only and not completed:
                delete_this = False

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

        if delete_this:
            await matchdb.register_match_channel(match_id, None)
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 server.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
Exemple #9
0
    async def _do_execute(self, cmd):
        staff_role = server.find_role(Config.STAFF_ROLE)
        if staff_role is None:
            await cmd.channel.send(
                'Error: Couldn\'t find the staff role. Please contact incnone.'
            )

        ref_role = server.find_role(Config.REFEREE_ROLE)

        if ref_role is None:
            await cmd.channel.send(f'Alerting {staff_role.mention}.')
        else:
            await cmd.channel.send(f'Alerting {staff_role.mention}, {ref_role.mention}.')

        notifications_channel = server.find_channel(Config.NOTIFICATIONS_CHANNEL_NAME)
        if notifications_channel is None:
            await cmd.channel.send(
                'Error: Couldn\'t find the bot notifications channel.'
            )

        await notifications_channel.send(
            '{user} has called `{this}` in channel {channel}.'.format(
                user=cmd.author.display_name,
                this=self.mention,
                channel=cmd.channel.mention
            )
        )
Exemple #10
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 matchutil.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
Exemple #11
0
    async def _do_execute(self, cmd: Command):
        if not len(cmd.args) == 1:
            await cmd.channel.send(
                'Wrong number of args for `{0}`.'.format(self.mention)
            )
            return

        username = cmd.args[0]
        user = await userlib.get_user(any_name=username)
        if user is None:
            await cmd.channel.send(
                "Couldn't find a user with name `{0}`.".format(cmd.args[0])
            )
            return

        matches = await matchchannelutil.get_matches_with_channels(racer=user)
        deleted_any = False
        for the_match in matches:
            channel = server.find_channel(channel_id=the_match.channel_id)
            if channel is not None:
                await channel.delete()
                await matchutil.delete_match(match_id=the_match.match_id)
                deleted_any = True

        if deleted_any:
            await cmd.channel.send(
                "Dropped `{0}` from all their current matches.".format(user.display_name)
            )
        else:
            await cmd.channel.send(
                "Couldn't find any current matches for `{0}`.".format(user.display_name)
            )
Exemple #12
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:
                     await channel.set_permissions(target=ev.user.member,
                                                   read_messages=True)
Exemple #13
0
async def load_lunkybot_config(necrobot):

    necrobot.register_pm_channel(PMBotChannel())

    server.main_channel = server.find_channel(
        channel_name=Config.MAIN_CHANNEL_NAME)
    if server.main_channel is None:
        console.warning(
            f'Could not find the "{Config.MAIN_CHANNEL_NAME}" channel.')
    necrobot.register_bot_channel(server.main_channel, MainBotChannel())
Exemple #14
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()
Exemple #15
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))
    necrobot.register_bot_channel(main_discord_channel, MainBotChannel())
Exemple #16
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_id=Config.MAIN_CHANNEL_ID)
    if main_discord_channel is None:
        console.warning(
            f'Could not find the main channel <#{Config.MAIN_CHANNEL_ID}>.')
    necrobot.register_bot_channel(main_discord_channel, MainBotChannel())
Exemple #17
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
                     )
Exemple #18
0
async def load_condorbot_config(necrobot):
    # PM Channel
    necrobot.register_pm_channel(CondorPMChannel())

    # Main Channel
    main_channel = server.find_channel(Config.MAIN_CHANNEL_NAME)
    necrobot.register_bot_channel(main_channel, CondorMainChannel(ladder=True))

    # 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 important!)
    necrobot.register_manager(LeagueMgr())
    necrobot.register_manager(MatchMgr())
    necrobot.register_manager(CondorMgr())

    # Ratings
    ratingutil.init()
Exemple #19
0
async def load_condorbot_config(necrobot):
    # PM Channel
    necrobot.register_pm_channel(CondorPMChannel())

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

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

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

    # Ratings
    ratingutil.init()
Exemple #20
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('-----------------------------------------')
Exemple #21
0
async def get_match_room(match: Match) -> Optional[MatchRoom]:
    """Get the MatchRoom corresponding to the given match, if any.

    Parameters
    ----------
    match: Match
        The Match to get the MatchRoom for.
    """
    if not match.is_registered:
        console.warning('Trying to get a MatchRoom for an unregistered Match.')
        return None

    channel_id = match.channel_id
    if channel_id is None:
        console.warning('Called get_match_room on an unchanneled match.')
        return None

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

    try:
        matchroom = Necrobot().get_bot_channel(discord_channel=channel)
    except KeyError:
        console.warning(
            'Couldn\'t find MatchRoom for match with ID (match_id={1}), due to KeyError.'
            .format(channel_id, match.match_id))
        return None

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

    return matchroom
Exemple #22
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 channel.delete()
    match.set_channel_id(None)
Exemple #23
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)
Exemple #24
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 = {server.guild.default_role: deny_read}
        for racer in match.racers:
            if racer.member is not None:
                racer_permissions[racer.member] = permit_read

        # Find the matches category channel
        channel_categories = MatchGlobals(
        ).channel_categories  # type: List[discord.CategoryChannel]
        if channel_categories is None:
            category_name = Config.MATCH_CHANNEL_CATEGORY_NAME
            if len(category_name) > 0:
                channel_category = await server.create_channel_category(
                    category_name)
                MatchGlobals().set_channel_categories([channel_category])

        # Attempt to create the channel in each of the categories in reverse order
        if channel_categories is not None:
            success = False
            for channel_category in reversed(channel_categories):
                try:
                    match_channel = await server.guild.create_text_channel(
                        name=get_matchroom_name(match),
                        overwrites=racer_permissions,
                        category=channel_category)
                    success = True
                    break
                except discord.HTTPException:
                    pass

            # If we still haven't made the channel, we're out of space, so register a new matches category
            if not success:
                new_channel_category = await server.create_channel_category(
                    name=Config.MATCH_CHANNEL_CATEGORY_NAME)
                MatchGlobals().add_channel_category(
                    channel=new_channel_category)
                match_channel = await server.guild.create_text_channel(
                    name=get_matchroom_name(match),
                    overwrites=racer_permissions,
                    category=new_channel_category)

        # If we don't have or want a category channel, just make the match without a category
        else:
            match_channel = await server.guild.create_text_channel(
                name=get_matchroom_name(match), overwrites=racer_permissions)

        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
Exemple #25
0
 async def initialize(self):
     await self._recover_stored_match_rooms()
     category_channel = server.find_channel(channel_name=Config.MATCH_CHANNEL_CATEGORY_NAME)
     MatchGlobals().set_channel_category(category_channel)
Exemple #26
0
 def results_channel(self):
     return server.find_channel(
         channel_name=Config.RACE_RESULTS_CHANNEL_NAME)