Ejemplo n.º 1
0
    async def removegroup(self, ctx: Context, eventMessage: EventMessage, *,
                          groupName: str):
        """
        Remove a role group from the event.

        Example: removegroup 1 Bravo
        """
        event = EventDatabase.getEventByMessage(eventMessage.id)
        groupName = groupName.strip('"')

        if not event.hasRoleGroup(groupName):
            await ctx.send("No role group found with name {}".format(groupName)
                           )
            return

        # Remove reactions, remove role, update event, add reactions, export
        for reaction in event.getReactionsOfGroup(groupName):
            await eventMessage.remove_reaction(reaction, self.bot.user)
        event.removeRoleGroup(groupName)
        await msgFnc.updateMessageEmbed(eventMessage, event)
        EventDatabase.toJson()  # Update JSON file
        await ctx.send("Group {} removed from {}".format(groupName, event))
Ejemplo n.º 2
0
    async def _update_event(self,
                            event: Event,
                            import_db=False,
                            reorder=True,
                            export=True):
        # TODO: Move to a more appropriate location
        if import_db:
            await self.bot.import_database()
            # Event instance might have changed because of DB import, get again
            event = EventDatabase.getEventByMessage(event.messageID)

        try:
            message = await msgFnc.getEventMessage(event, self.bot)
        except MessageNotFound:
            message = await msgFnc.createEventMessage(event,
                                                      self.bot.eventchannel)

        await msgFnc.updateMessageEmbed(eventMessage=message,
                                        updatedEvent=event)
        await msgFnc.updateReactions(event=event,
                                     message=message,
                                     reorder=reorder)
        if export:
            EventDatabase.toJson()
Ejemplo n.º 3
0
    async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
        if payload.member == self.bot.user or \
                payload.channel_id != self.bot.eventchannel.id:
            # Bot's own reaction, or reaction outside of the event channel
            return

        if payload.emoji.name in cfg.EXTRA_EMOJIS:
            return

        # Remove the reaction
        message = await self.bot.eventchannel.fetch_message(payload.message_id)
        user = payload.member
        await message.remove_reaction(payload.emoji, user)

        # Get event from database with message ID
        try:
            event: Event = EventDatabase.getEventByMessage(message.id)
        except EventNotFound as e:
            print(e)
            await self.bot.logchannel.send(
                "NOTE: reaction to a non-existent event. "
                "msg: {} role: {} user: {} ({}#{})"
                .format(message.id, payload.emoji,
                        user.display_name,
                        user.name, user.discriminator))
            return

        # Get emoji string
        if payload.emoji.is_custom_emoji():
            emoji = payload.emoji
        else:
            emoji = payload.emoji.name

        # Find signup of user
        signup: Optional[Role] = event.findSignupRole(user.id)

        # Get role with the emoji
        try:
            role = event.findRoleWithEmoji(emoji)
        except RoleNotFound as e:
            raise RoleNotFound("{} in event {} by user {}#{}"
                               .format(str(e), event, user.name,
                                       user.discriminator))
        if role.name == "ZEUS":
            # somebody with Nitro added the ZEUS reaction by hand
            return

        late_signoff = False

        """
        if user is not signed up and the role is     free, sign up
        if user is not signed up and the role is not free, do nothing
        if user is     signed up and they select    the same role, sign off
        if user is     signed up and they select a different role, do nothing
        """
        if signup is None:

            # Sign up if role is free
            if role.userID is None:
                # signup
                event.signup(role, user)

                # Update event
                await msgFnc.updateMessageEmbed(message, event)
                EventDatabase.toJson()
            else:
                # Role is already taken, ignoring sign up attempt
                return
            message_action = "Signup"

        elif signup.emoji == emoji:
            # undo signup
            event.undoSignup(user)

            # Update event
            await msgFnc.updateMessageEmbed(message, event)
            EventDatabase.toJson()

            message_action = "Signoff"

            print("Signed off role name:", role.name)
            if role.name in cfg.SIGNOFF_NOTIFY_ROLES[event.platoon_size]:
                print("Signoff in to be notified")
                date = event.date
                print("Event date:", date)
                time_delta = date - datetime.today()
                if time_delta > timedelta(days=0):
                    days_str = ""
                    hours_str = ""
                    minutes_str = ""
                    days = time_delta.days
                    hours = time_delta.seconds // (60 * 60)
                    minutes = (time_delta.seconds - hours * 60 * 60) // 60
                    if days > 0:
                        days_str = "{} days ".format(days)
                    if hours > 0:
                        hours_str = "{} hours ".format(hours)
                    if minutes > 0:
                        minutes_str = "{} minutes".format(minutes)

                    timestring = "{}{}{}".format(days_str, hours_str,
                                                 minutes_str)

                    if time_delta < cfg.SIGNOFF_NOTIFY_TIME and \
                            self.bot.signoff_notify_user != user:
                        print("Delta:", time_delta)
                        print("Date delta smaller than notify period")
                        late_signoff = True
        else:
            # user reacted to another role while signed up
            return

        if late_signoff and not event.sideop:
            message = "{}: User {} ({}#{}) signed off from {} role {} " \
                      "{} before the operation." \
                      .format(self.bot.signoff_notify_user.mention,
                              user.display_name,
                              user.name,
                              user.discriminator,
                              event,
                              role.display_name,
                              timestring)
        else:
            message = "{}: event: {} role: {} user: {} ({}#{})" \
                      .format(message_action, event, role.display_name,
                              user.display_name,
                              user.name,
                              user.discriminator)

        await self.bot.logchannel.send(message)
Ejemplo n.º 4
0
    async def on_raw_reaction_add(self, payload: RawReactionActionEvent):

        if payload.member == self.bot.user or \
                payload.channel_id != self.bot.eventchannel.id:
            # Bot's own reaction, or reaction outside of the event channel
            return

        if payload.emoji.name in cfg.EXTRA_EMOJIS:
            return

        message: Message = await self.bot.eventchannel.fetch_message(
            payload.message_id)
        if message.author != self.bot.user:
            # We don't care about reactions to other messages than our own.
            # Makes it easier to test multiple bot instances on the same
            # channel
            return

        # Remove the reaction
        user: User = payload.member
        await message.remove_reaction(payload.emoji, user)

        # Get event from database with message ID
        try:
            event: Event = EventDatabase.getEventByMessage(message.id)
        except EventNotFound as e:
            print(e)
            await self.bot.logchannel.send(
                "NOTE: reaction to a non-existent event. "
                "msg: {} role: {} user: {} ({}#{})".format(
                    message.id, payload.emoji, user.display_name, user.name,
                    user.discriminator))
            return

        # Get emoji string
        if payload.emoji.is_custom_emoji():
            emoji = payload.emoji
        else:
            emoji = payload.emoji.name

        # Find signup of user
        old_signup: Optional[Role] = event.findSignupRole(user.id)

        # If a user is already signed up as Zeus they can't sign off or change
        # roles without the Event Moderator
        if old_signup and old_signup.name == cfg.EMOJI_ZEUS:
            return

        # Get role with the emoji
        # TODO: remove when converter exists
        try:
            role = event.findRoleWithEmoji(emoji)
        except RoleNotFound as e:
            raise RoleNotFound("{} in event {} by user {}#{}".format(
                str(e), event, user.name, user.discriminator))

        if role.name == cfg.EMOJI_ZEUS:
            # Somebody with Nitro added the ZEUS reaction by hand, ignoring
            return

        late_signoff_delta = None
        old_role = ""

        """
        if user is not signed up and the role is free, sign up
        if user is not signed up and the role is not free, do nothing
        if user is signed up and they select the same role, sign off
        if user is signed up and they select a different role, change to that role
        """  # NOQA
        if old_signup and emoji == old_signup.emoji:
            # User clicked a reaction of the current signed up role
            removed_role = event.undoSignup(user)
            message_action = "SIGNOFF"
        else:
            try:
                removed_role, _ = event.signup(role, user)
            except RoleTaken:
                # Users can't take priority with a reaction
                return
            if removed_role is None:
                # User wasn't signed up to any roles previously
                message_action = "SIGNUP"
            else:
                # User switched from a different role
                message_action = "CHANGE"
                old_role = "{} -> ".format(removed_role.display_name)

        # Update discord embed
        await msgFnc.updateMessageEmbed(message, event)
        EventDatabase.toJson()

        delta_message = ""
        if removed_role and not event.sideop:
            # User signed off or changed role, checking if there's a need to
            # ping
            late_signoff_delta = self._calculate_signoff_delta(
                event, removed_role, user)
            if late_signoff_delta is not None and not event.sideop:
                delta_message = "{}: {} before the operation:\n" \
                                .format(self.bot.signoff_notify_user.mention,
                                        late_signoff_delta)

        message = f"{delta_message}{message_action}: {event}, role: " \
                  f"{old_role}{role.display_name}, " \
                  f"user: {user.display_name} " \
                  f"({user.name}#{user.discriminator})"

        await self.bot.logchannel.send(message)