Example #1
0
    async def on_message(self, message):
        if not message.content.startswith(
                self.bot.command_prefix) and not message.author.bot:
            guild = message.guild
            author = message.author
            channel = message.channel
            if guild is not None:
                with self.cursor_context() as cursor:
                    db_util.select("statistics_global").items(
                        "id",
                        "msg_count").where(id_server=guild.id,
                                           id_user=author.id,
                                           id_channel=channel.id).run(cursor)
                    row = cursor.fetchone()

                with self.cursor_context(commit=True) as cursor:
                    if not row:
                        db_util.insert("statistics_global").items(
                            id_server=guild.id,
                            id_user=author.id,
                            id_channel=channel.id,
                            msg_count=1).run(cursor)
                    else:
                        row_id = row[0]
                        db_util.update("statistics_global").items(
                            msg_count=DBFunction("msg_count+1")).where(
                                id=row_id).run(cursor)
Example #2
0
    async def set(self, ctx, tz_name: str):
        """Set your current timezone"""
        try:
            local_tz = pytz.timezone(tz_name)
        except pytz.exceptions.UnknownTimeZoneError:
            await ctx.reply(f'The timezone "**{tz_name}**" does not exist')
            return

        with self.cursor_context(commit=True) as cursor:
            cursor.execute(
                *db_util.select("profiles").items("id").limit(1).where(
                    id_user=ctx.author.id).build)
            row = cursor.fetchone()

        with self.cursor_context(commit=True) as cursor:
            if not row:
                cursor.execute(*db_util.insert("profiles").items(
                    id_user=ctx.author.id, timezone=tz_name).build)
            else:
                cursor.execute(*db_util.update("profiles").items(
                    timezone=tz_name).where(id=row[0]).build)

        utc = pytz.utc
        time_now = datetime.datetime.utcnow()
        time_now_utc = utc.localize(time_now)
        time_now_localized = time_now_utc.astimezone(local_tz)
        time_now_formatted = time_now_localized.strftime('%Y-%m-%d %H:%M:%S')
        await ctx.reply(
            f'Your timezone has been set to "**{tz_name}**", your local time is **{time_now_formatted}**'
        )