Ejemplo n.º 1
0
    async def game_public_message(self):

        game_summary_embed = Embed()

        game_summary_embed.title = ("-" * 20) + f"Game {self.game.id} Summary" \
                                   + ("-" * 20)

        max_players = self.game.teams[0].size
        game_summary_embed.description = (
            "React to join!\n" + f"Created by: {self.user.mention}\n" +
            f"Mode: {self.mode_str} " +
            (f"({max_players})" if self.mode_str == "FFA" else "") + "\n" +
            f"Platform: {self.platform_choice}\n" +
            f"Platform: {self.platform_choice}\n" +
            f"Description: {self.game_description}\n")

        channel_prop = DatabaseFacade.get_property(JOIN_GAME_CHANNEL)
        channel_name = channel_prop.value

        channel: TextChannel = find(
            lambda channel: channel.name == channel_name,
            self.guild_reference.channels)

        game_message = await channel.send(embed=game_summary_embed)

        DatabaseFacade.update_game(self.game.id,
                                   message_did=str(game_message.id))
        await game_message.add_reaction(UNICODE_FORWARD_ARROW)
Ejemplo n.º 2
0
async def prop(context: commands.Context,
               prop_name: str,
               prop_value: str = None):
    if prop_value is None:
        prop = DatabaseFacade.get_property(prop_name)
        if prop is not None:
            await context.send(content=f"Value: {prop.value}")
        else:
            await context.send(f"Property undefined: {prop_name}")
    else:
        DatabaseFacade.set_property(prop_name, prop_value)
        await context.send(content=f"set property {prop_name} to {prop_value}")
Ejemplo n.º 3
0
    async def create_game_channel(self):
        game_category = DatabaseFacade.get_property(
            GAME_CATEGORY_PROPERTY_NAME)

        print(f"Searching for category: {game_category.value}")

        game_id: str = str(self.game.id)
        game_category = find(lambda cat: cat.name == game_category.value,
                             self.guild_reference.categories)

        print("Game categories: ")
        for category in self.guild_reference.categories:
            print(category.name)

        channel: TextChannel = await self.guild_reference.create_text_channel(
            name=f"Game-{game_id}", category=game_category)

        DatabaseFacade.update_game(self.game.id, channel_did=str(channel.id))

        await channel.set_permissions(self.user, read_messages=True)
        await self.game_channel_message(channel)
Ejemplo n.º 4
0
async def delete(context: commands.Context, *args):
    channel_did = str(context.channel.id)
    game: Game = DatabaseFacade.get_game_by_channel_did(channel_did)
    if game is not None:
        if len(args) > 0:
            await context.send("usage: !delete")
            return
        else:

            caller_did = str(context.author.id)
            caller = DatabaseFacade.get_player_by_did(caller_did)

            join_game_message_id = int(game.message_did)
            print(f"looking for join message with id {join_game_message_id}")

            join_game_channel_prop = DatabaseFacade.get_property(
                JOIN_GAME_CHANNEL)

            join_game_channel = discord.utils.find(
                lambda channel: channel.name == join_game_channel_prop.value,
                context.guild.text_channels)
            print(f"got join channel with name {join_game_channel.name}")

            join_game_message = await join_game_channel.fetch_message(
                join_game_message_id)

            if caller.id != game.creator_id:
                await context.send("Only the creator can delete a game")
                return

            DatabaseFacade.delete_game_by_id(game.id)

            print(f"deleting channel {context.channel.name}")
            await context.channel.delete()

            print(f"join_game_message has id")
            await join_game_message.delete()
            return
Ejemplo n.º 5
0
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
    user = bot.get_user(payload.user_id)

    # DEBUG output, enable if needed.
    # print("RAW reaction received")
    # print("From user:"******"(" + str(payload.user_id) + ")")
    # print("reaction:", payload.emoji)
    # print("emoji name:", payload.emoji.name)
    # print("emoji id:", payload.emoji.id)
    # print(payload.emoji.name, "?=", UNICODE_1, ":", payload.emoji.name == UNICODE_1)
    # print("payload channel: {}".format(payload.channel_id))

    guild: Guild = bot.get_guild(payload.guild_id)

    # Looks like all possible send targets inherit from Messageable, and EITHER
    # GuildChannel or PrivateChannel (all 3 of which are in discord.abc)
    channel: discord.TextChannel = bot.get_channel(payload.channel_id)
    print(f"channel: {channel}")
    message: Message = await channel.fetch_message(payload.message_id)

    if not user.bot:

        create_game_prop = DatabaseFacade.get_property(
            property_name=CREATE_GAME_CHANNEL)

        # if the above evaluated true, then channel should have a name
        # attribute at this point.
        create_game_channel_name = create_game_prop.value

        join_game_prop = DatabaseFacade.get_property(
            property_name=JOIN_GAME_CHANNEL)

        join_game_channel_name = join_game_prop.value

        if type(channel) == DMChannel:
            print("checked as DMChannel")
            message_sequence: MessageSequenceTest \
                = message_states.get_user_sequence(user)

            # if the user has a sequence with the bot already...
            if message_sequence is not None:
                # pass the message along to the sequence's handler
                await message_sequence.run_next_handler(message)
                return
            # otherwise... just ignore them I think?
            else:
                return
        # this is the message the bot put in the create a game channel
        elif channel.name == create_game_channel_name:
            print("checked as a create game message")
            reactions = message.reactions
            for reaction in reactions:
                try:
                    await message.remove_reaction(reaction.emoji, user)
                except Exception as e:
                    print(f"ERROR: While removing reaction, got exception of "
                          f"type {type(e)}")

            new_game_sequence = NewGameSequence(user, guild)
            await message_states.add_user_sequence(user, new_game_sequence)
            await new_game_sequence.start_sequence()
        elif channel.name == join_game_channel_name:
            print("checked as a join game message")
            # reaction was added on a message in the games channel
            game: Game = DatabaseFacade.get_game_by_message_did(str(
                message.id))

            user_as_player = DatabaseFacade.get_player_by_did(str(user.id))
            if game is not None:
                DatabaseFacade.add_player_to_game(game.id, user_as_player)
                game_channel: TextChannel = bot.get_channel(
                    int(game.channel_id))
                await game_channel.set_permissions(user, read_messages=True)

                if game.is_full():
                    print("game is full, clearing all messages")
                    await message.clear_reactions()
            else:
                print(f"Game not found with message_did: {message.id}")

        # this should become a check against all of the game's private channel
        # messages, which I'll probably build team swapping into
        elif channel.category.name == DatabaseFacade.get_property(
                GAME_CATEGORY_PROPERTY_NAME).value:
            print("CHECKED as a channel in the game category")
            if message.author.bot:
                game: Game = DatabaseFacade.get_game_by_game_message_did(
                    str(message.id))

                user_as_player = DatabaseFacade.get_player_by_did(str(user.id))

                emoji_name = payload.emoji.name
                print("")
                if emoji_name == UNICODE_0:
                    team_number = 0
                elif emoji_name == UNICODE_1:
                    team_number = 1
                elif emoji_name == UNICODE_2:
                    team_number = 2
                elif emoji_name == UNICODE_3:
                    team_number = 3
                elif emoji_name == UNICODE_4:
                    team_number = 4
                elif emoji_name == UNICODE_5:
                    team_number = 5
                elif emoji_name == UNICODE_6:
                    team_number = 6
                else:
                    print(f"React with bad emoji: {emoji_name}")
                    try:
                        await message.remove_reaction(payload.emoji, user)
                    except Exception as e:
                        print(
                            f"ERROR: While removing reaction, got exception of "
                            f"type {type(e)}")

                    return

                if DatabaseFacade.add_player_to_team(game.id, team_number,
                                                     user_as_player):
                    await channel.send(
                        content=f"{user.display_name} joined team "
                        f"{team_number}")
                else:
                    await channel.send(
                        content=f"Could not add {user.display_name} to team "
                        f"{team_number}: team is full or does not exist.")

                try:
                    await message.remove_reaction(payload.emoji, user)
                except Exception as e:
                    print(f"ERROR: While removing reaction, got exception of "
                          f"type {type(e)}")

        else:
            print("CHECKED as other message")