예제 #1
0
    def increment(
        cls,
        channel_id: int,
        user_id: int,
        guild_id: int,
        last_msg_at: datetime,
        count: int,
        is_webhook: bool,
    ):
        """Increment user_channel count, if it doesn't exist, create it"""
        user_channel = (
            session.query(UserChannel)
            .filter_by(channel_id=channel_id, user_id=user_id, guild_id=guild_id)
            .one_or_none()
        )
        if not user_channel:
            session.add(
                UserChannel(
                    guild_id=guild_id,
                    channel_id=channel_id,
                    user_id=user_id,
                    is_webhook=is_webhook,
                    count=count,
                    last_msg_at=last_msg_at,
                )
            )

        else:
            user_channel.count = user_channel.count + count
            if user_channel.last_msg_at < last_msg_at:
                user_channel.last_msg_at = last_msg_at

        session.commit()
예제 #2
0
    def add(
        self,
        guild_id: int,
        user_id: int,
        start_time: datetime,
        end_time: datetime,
        roles_to_return: list,
        channels_to_return: list,
        channels_to_remove: list,
        reason: str,
        typ: str,
    ):
        """Adds unverify to the database"""
        unverify = (
            session.query(Unverify).filter_by(user_id=user_id).filter_by(status="waiting").one_or_none()
        )

        if not unverify:
            added = Unverify(
                guild_id=guild_id,
                user_id=user_id,
                start_time=start_time,
                end_time=end_time,
                roles_to_return=roles_to_return,
                channels_to_return=channels_to_return,
                channels_to_remove=channels_to_remove,
                reason=reason,
                typ=typ,
            )
            session.add(added)
            session.commit()
        else:
            added = None
        return added
예제 #3
0
 def edit_rule(self, guild_id: int, command: str, allow: bool) -> ACL_rule:
     rule = self.get_rule(guild_id, command)
     if rule is None:
         raise NotFound(guild_id=guild_id, command=command)
     rule.default = allow
     session.commit()
     return rule
예제 #4
0
 def add(self, user_id: int, message_id: int, channel_id: int, text: str):
     session.add(
         Seeking(user_id=user_id,
                 message_id=message_id,
                 channel_id=channel_id,
                 text=text))
     session.commit()
예제 #5
0
 def create_term_message(message_id: int,
                         channel_id: int) -> ExamsTermsMessage:
     item = ExamsTermsMessage(message_id=str(message_id),
                              channel_id=str(channel_id))
     session.add(item)
     session.commit()
     return item
예제 #6
0
 def save_sent_code(self, login: str, code: str):
     """Updates a specified login with a new verification code"""
     person = session.query(Valid_person).\
         filter(Valid_person.login == login).one_or_none()
     person.code = code
     person.status = 2
     session.commit()
예제 #7
0
    async def update_db(self, ctx):
        with open("merlin-latest", "r") as f:
            data = f.readlines()

        new_people = []
        new_logins = []

        for line in data:
            line = line.split(":")
            login = line[0]
            name = line[4].split(",", 1)[0]
            try:
                year = line[4].split(",")[1]
            except IndexError:
                continue
            new_people.append(Valid_person(login=login, year=year, name=name))
            new_logins.append(login)

        for person in new_people:
            session.merge(person)

        for person in session.query(Valid_person):
            if person.login not in new_logins:
                try:
                    # check for muni
                    int(person.login)
                    person.year = "MUNI"
                except ValueError:
                    person.year = "dropout"

        session.commit()

        await ctx.send("Update databaze probehl uspesne")
예제 #8
0
    def create_audit_log(audit_type: AuditLogItemType,
                         guild: Optional[discord.Guild] = None,
                         user: Optional[Union[discord.Member,
                                              discord.User]] = None,
                         channel_id: Optional[int] = None,
                         data: Optional[dict] = None) -> AuditLogItem:
        if guild is not None and user is not None:
            if not UserRepo.guild_user_exist(user.id, guild.id):
                UserRepo.create_guild_user(user)
        elif guild is None and user is not None:
            if not UserRepo.user_exist(user.id):
                UserRepo.create_user(user)
        elif guild is not None and user is None:
            if not GuildRepo.guild_exist(guild.id):
                GuildRepo.create_guild(guild)

        item = AuditLogItem(
            user_id=str(user.id) if user is not None else None,
            guild_id=str(guild.id) if guild is not None else None,
            type=audit_type,
            data=data,
            channel_id=str(channel_id) if channel_id is not None else None)
        session.add(item)
        session.commit()
        return item
예제 #9
0
    def add_to_emoji_stat_usage_count(user: discord.Member,
                                      emoji: Union[discord.Emoji,
                                                   discord.PartialEmoji, str],
                                      channel_id: int,
                                      write: bool = True):
        member_stats = StatsRepo.get_member_emoji_stats_for_emoji(user, emoji)
        guild_stats = StatsRepo.get_guild_emoji_stats_for_emoji(
            user.guild, emoji
        )  # Here is already user created so we dont need to check if user exist again
        current_time = datetime.utcnow()

        if guild_stats.first_used is None:
            guild_stats.first_used = current_time
            guild_stats.first_used_at_channel_id = str(channel_id)
            guild_stats.first_used_by_id = member_stats.user_id

        if member_stats.first_used is None:
            member_stats.first_used = current_time
            member_stats.first_used_at_channel_id = str(channel_id)

        member_stats.last_used = current_time
        member_stats.last_used_at_channel_id = str(channel_id)
        member_stats.use_count += 1

        guild_stats.last_used = current_time
        guild_stats.last_used_at_channel_id = str(channel_id)
        guild_stats.last_used_by_id = member_stats.user_id
        guild_stats.use_count += 1
        if write: session.commit()
예제 #10
0
    def add(
        self,
        guild_id: int,
        action: str,
        giver: int,
        receiver: int,
    ) -> Interaction:
        result = (session.query(Interaction).filter(
            Interaction.guild_id == guild_id,
            Interaction.name == action,
            Interaction.giver == giver,
            Interaction.receiver == receiver,
        ).one_or_none())

        if result is not None:
            result.count += 1
            session.commit()
            return result

        result = Interaction(
            guild_id=guild_id,
            name=action,
            giver=giver,
            receiver=receiver,
            count=1,
        )
        session.add(result)
        session.commit()
        return result
예제 #11
0
    def increment(
        self,
        channel_id: int,
        user_id: int,
        guild_id: int,
        last_message_id: int,
        last_message_at: datetime,
    ):
        """Increment user_channel count, 
        if it doesn't exist, create it"""
        user_channel = (session.query(UserChannel).filter_by(
            channel_id=channel_id, user_id=user_id,
            guild_id=guild_id).one_or_none())
        if not user_channel:
            session.add(
                UserChannel(
                    channel_id=channel_id,
                    user_id=user_id,
                    last_message_at=last_message_at,
                    last_message_id=last_message_id,
                    guild_id=guild_id,
                ))

        else:
            user_channel.count = user_channel.count + 1
            if user_channel.last_message_at < last_message_at:
                user_channel.last_message_at = last_message_at
                user_channel.last_message_id = last_message_id

        session.commit()
예제 #12
0
    def decrement(
        self,
        channel_id: int,
        user_id: int,
        guild_id: int,
        last_message_id: int,
        last_message_at: datetime,
    ):
        """Decrement user_channel count."""
        user_channel = (session.query(UserChannel).filter_by(
            channel_id=channel_id, user_id=user_id,
            guild_id=guild_id).one_or_none())
        if not user_channel:
            session.add(
                UserChannel(
                    channel_id=channel_id,
                    user_id=user_id,
                    count=0,
                    last_message_at=last_message_at,
                    last_message_id=last_message_id,
                    guild_id=guild_id,
                ))

        else:
            user_channel.count = user_channel.count - 1
            if user_channel.last_message_at < last_message_at:
                user_channel.last_message_at = last_message_at
                user_channel.last_message_id = last_message_id

        session.commit()
예제 #13
0
    def edit_group(
        self,
        guild_id: int,
        name: str,
        *,
        new_name: str = None,
        parent: str = None,
        role_id: int = None,
    ) -> ACL_group:
        group = self.get_group(guild_id, name)
        if group is None:
            raise NotFound(guild_id=guild_id, name=name)

        if new_name is not None:
            name = new_name
            group.name = name
        if parent is not None:
            if self.get_group(guild_id, parent) is None:
                raise NotFound(guild_id=guild_id, name=parent)
            group.parent = parent
        if role_id is not None:
            group.role_id = role_id

        session.commit()
        return group
 def update_welcome_message_enable(guild: discord.Guild,
                                   val: bool,
                                   write: bool = True):
     guild_config = WelcomeMessageRepo.find(guild)
     if guild_config.welcome_message_enable != val:
         guild_config.welcome_message_enable = val
         if write: session.commit()
예제 #15
0
 def add_user(self, acl_group_id, user_id, perms):
     # TODO check group id validity
     rule = Acl_user_binding(acl_group_id=acl_group_id,
                             user_id=user_id,
                             perms=perms)
     session.add(rule)
     session.commit()
예제 #16
0
    async def update_db(self, ctx, convert_0xit: bool = False):
        with open("merlin-latest", "r") as f:
            data = f.readlines()

        found_people = []
        found_logins = []
        new_logins = []
        login_results = session.query(Valid_person.login).all()
        # Use unpacking on results
        old_logins = [value for value, in login_results]

        for line in data:
            line = line.split(":")
            login = line[0]
            name = line[4].split(",", 1)[0]
            try:
                year = line[4].split(",")[1]
            except IndexError:
                continue

            if convert_0xit and year.endswith("1r"):
                person = session.query(Valid_person).\
                    filter(Valid_person.login == login).\
                    one_or_none()
                if person is not None and person.year.endswith("0r"):
                    year = year.replace("1r", "0r")

            found_people.append(Valid_person(login=login, year=year,
                                             name=name))
            found_logins.append(login)

        for login in found_logins:
            if login not in old_logins:
                new_logins.append(login)

        await ctx.send(f"Našel jsem {len(new_logins)} nových loginů.")

        for person in found_people:
            session.merge(person)

        cnt_new = 0
        for person in session.query(Valid_person):
            if person.login not in found_logins:
                try:
                    # check for muni
                    int(person.login)
                    person.year = "MUNI"
                except ValueError:
                    person.year = "dropout"
            elif convert_0xit and person.login in new_logins:
                if person.year.endswith("1r"):
                    person.year = person.year.replace("1r", "0r")
                    cnt_new += 1

        session.commit()

        await ctx.send("Aktualizace databáze proběhla úspěšně.")
        if convert_0xit:
            await ctx.send(f"Debug: Našel jsem {cnt_new} nových prvaků.")
예제 #17
0
 def remove_user_give_karma(user:Union[discord.Member, discord.User], value: int, positive: bool, write: bool = True):
   if value != 0:
     item = KarmaRepo.get_user(user)
     if positive:
       item.given_karma_positive = item.given_karma_positive - value
     else:
       item.given_karma_negative = item.given_karma_negative - value
     if write: session.commit()
예제 #18
0
 def update_guild(cls, guild_id=None, guild_name=None):
     if guild_id is None or guild_name is None:
         return None
     guilds = session.query(UserChannel).filter_by(guild_id=guild_id).all()
     for guild in guilds:
         if guild.guild_name != guild_name:
             guild.guild_name = guild_name
     session.commit()
예제 #19
0
 def create_warden_flag(flagged_message_id: int,
                        warden_message: discord.Message):
     item = WardenFlag(flagged_message_id=str(flagged_message_id),
                       warden_flag_message_id=str(warden_message.id),
                       channel_id=str(warden_message.channel.id),
                       guild_id=str(warden_message.guild.id))
     session.add(item)
     session.commit()
예제 #20
0
    def save_verified(self, login: str, discord_id: str):
        """"Inserts login with discord name into database"""
        session.add(Permit(login=login, discord_ID=discord_id))

        person = session.query(Valid_person).filter(Valid_person.login == login).one_or_none()
        person.status = 0

        session.commit()
예제 #21
0
 def set_programme(self, shortcut, name, link):
     programme = Programme(
         shortcut=shortcut,
         name=name,
         link=link
     )
     session.merge(programme)
     session.commit()
예제 #22
0
 def update_review(self, id, tier, anonym: bool, text):
     review = Review(id=id,
                     tier=tier,
                     anonym=anonym,
                     text_review=text,
                     date=datetime.date.today())
     session.merge(review)
     session.commit()
예제 #23
0
 def remove_from_inventory(player_id: int, item_id: int):
     slot = session.query(InventorySlot).filter(
         InventorySlot.owner_id == str(player_id),
         InventorySlot.item_id == item_id).first()
     if slot is not None:
         session.query(InventorySlot).filter(
             InventorySlot.id == slot.id).delete()
         session.commit()
예제 #24
0
 def update_user(cls, user_id=None, user_name=None):
     if user_id is None or user_name is None:
         return None
     users = session.query(UserChannel).filter_by(user_id=user_id).all()
     for user in users:
         if user.user_name != user_name:
             user.user_name = user_name
     session.commit()
예제 #25
0
 def update_channel(cls, channel_id=None, channel_name=None):
     if channel_id is None or channel_name is None:
         return None
     channels = session.query(UserChannel).filter_by(channel_id=channel_id).all()
     for channel in channels:
         if channel.channel_name != channel_name:
             channel.channel_name = channel_name
     session.commit()
예제 #26
0
    def add_rule(self, guild_id: int, command: str, allow: bool = False) -> ACL_rule:
        if self.get_rule(guild_id, command) is not None:
            raise Duplicate(guild_id=guild_id, command=command)

        rule = ACL_rule(guild_id=guild_id, command=command, default=allow)
        session.add(rule)
        session.commit()
        return rule
예제 #27
0
    def __create_player(user: Union[discord.Member, discord.User]) -> Player:
        if not UserRepo.user_exist(user.id):
            UserRepo.create_user(user)

        item = Player(user_id=str(user.id))
        session.add(item)
        session.commit()
        return item
예제 #28
0
 def add_vote(self, review_id, vote: bool, author):
     relevance = ReviewRelevance(
         member_ID=author,
         vote=vote,
         review=review_id
     )
     session.merge(relevance)
     session.commit()
예제 #29
0
    def create_effect(item_id: int, effect_id: Union[EffectEnum, int],
                      ammount: int) -> Effect:
        if isinstance(effect_id, EffectEnum): effect_id = effect_id.value
        item = Effect(item_id=item_id, effect_id=effect_id, ammount=ammount)

        session.add(item)
        session.commit()

        return item
예제 #30
0
    def update(cls, emote: discord.Emoji):
        """Add new emote"""
        db_emote = session.query(Emote).filter(
            Emote.emote_id == emote.id).one_or_none()

        if db_emote is not None:
            if db_emote.name != emote.name:
                db_emote.name = emote.name
            session.commit()