Esempio n. 1
0
async def leave(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: !leave")
            return
        player_did = str(context.author.id)
        leaver = DatabaseFacade.get_player_by_did(player_did=player_did)

        if leaver.id == game.creator_id:
            await context.send("The creator cannot leave the game; use !delete"
                               " to remove the game instead.")
            return

        DatabaseFacade.remove_player_from_game(game.id, leaver)

        game_channel_id: int = int(game.channel_id)

        game_channel: TextChannel = bot.get_channel(game_channel_id)

        await game_channel.set_permissions(context.author, read_messages=False)

        print("leave functioned properly so far....")

        await context.send(
            content=f"User {context.author.name} has left the game")

        print("sent message on channel", context.channel.id)

        return
Esempio n. 2
0
async def kick(context: commands.Context, mentioned: User, *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: !kick <@user>\nCan only be used by the"
                               " creator of the game")
            return
        else:

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

            if caller.id != game.creator_id:
                await context.send("!kick can only be used by the creator.")
                return

            mentioned_did = str(mentioned.id)
            kicked = DatabaseFacade.get_player_by_did(mentioned_did)

            if kicked.id == game.creator.id:
                await context.send("!kick cannot be used to remove the creator"
                                   )
                return

            success = DatabaseFacade.remove_player_from_game(game.id, kicked)

            if not success:
                await context.send(
                    f"Cannot kick user {mentioned.name}; user is not in game")
                return

            game_channel: TextChannel = bot.get_channel(context.channel.id)

            await game_channel.set_permissions(mentioned, read_messages=False)

            await context.send(
                content=f"User {mentioned.name} has been kicked.")
Esempio n. 3
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
Esempio n. 4
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")