Example #1
0
def create_todo_reminder():
    if (guild_id := request.json.get('guild_id')) is not None:

        member = User.query.get(get_internal_id())

        if guild_id in [x.id for x in member.permitted_guilds()]:

            if (time_diff := request.json.get('time')) is not None and \
                    (todo_id := request.json.get('todo_id')) is not None:

                if (todo := Todo.query.get(todo_id)) is not None and \
                        todo.guild_id == guild_id:

                    reminder = Reminder(message=Message(embed=Embed(
                        title='Todo Reminder!',
                        description=
                        'You have stuff to do. More specifically, **{}**'.
                        format(todo.value),
                        color=0xFFFFFF)),
                                        time=unix_time() + time_diff,
                                        channel_id=todo.channel_id,
                                        username='******',
                                        method='todo',
                                        set_by=get_internal_id(),
                                        name='Todo Reminder')

                    db.session.add(reminder)
                    db.session.commit()

                else:
                    abort(400)
Example #2
0
def todo_dashboard():
    if (guild_id := request.args.get('id')) is not None:
        member = User.query.get(get_internal_id())
        guild = Guild.query.filter(Guild.guild == guild_id).first_or_404()

        if member is None:
            return redirect(url_for('cache'))

        elif guild not in member.permitted_guilds():
            return abort(403)

        else:
            todos = {}
            global_todos = []

            for todo in guild.todo_list:
                if todo.channel_id is None:
                    global_todos.append(todo)

                else:
                    if todos.get(todo.channel_id) is None:
                        todos[todo.channel] = [todo]

                    else:
                        todos[todo.channel].append(todo)

            return render_template('todo_dashboard/todo_dashboard.html',
                                   guilds=member.permitted_guilds(),
                                   guild=guild,
                                   member=member,
                                   todos=todos,
                                   global_todos=global_todos)
Example #3
0
def change_restrictions():
    if (guild_id := request.json.get('guild_id')) and \
            (command := request.json.get('command')) and \
            (roles := request.json.get('roles')) is not None:

        member = User.query.get(get_internal_id())

        if guild_id in [x.id for x in member.permitted_guilds()]:
            guild = Guild.query.get(guild_id)

            role_ids = [r.id for r in guild.roles]

            CommandRestriction \
                .query \
                .filter(CommandRestriction.role_id.in_(role_ids)) \
                .filter(CommandRestriction.command == command) \
                .delete(synchronize_session='fetch')

            for role in filter(lambda r: int(r) in role_ids, roles):
                c = CommandRestriction(role_id=role, command=command)
                db.session.add(c)

            db.session.commit()

            return '', 201

        else:
            abort(403)
Example #4
0
def alter_todo():
    if (guild_id := request.json.get('guild_id')) is not None:

        member = User.query.get(get_internal_id())

        if guild_id in [x.id for x in member.permitted_guilds()]:

            if request.method == 'POST':
                if (channel_id := request.json.get('channel_id')) is not None and \
                        (value := request.json.get('value')) is not None and \
                        0 < len(value) <= 2000:

                    if channel_id == -1:
                        todo = Todo(guild_id=guild_id,
                                    channel_id=None,
                                    user_id=member.id,
                                    value=value)

                        db.session.add(todo)
                        db.session.commit()

                        return jsonify({'id': todo.id})

                    else:
                        guild = Guild.query.get(guild_id)

                        if guild.channels.filter(
                                Channel.id == channel_id).first() is not None:
                            todo = Todo(guild_id=guild_id,
                                        channel_id=channel_id,
                                        user_id=member.id,
                                        value=value)

                            db.session.add(todo)
                            db.session.commit()

                            return jsonify({'id': todo.id})

                        else:
                            abort(404)

                else:
                    abort(400)

            elif request.method == 'DELETE':
                if (channel_id := request.json.get('channel_id')) is not None and \
                        (todo_id := request.json.get('todo_id')) is not None:

                    if channel_id == -1:
                        channel_id = None

                    Todo.query \
                        .filter(Todo.id == todo_id) \
                        .filter(Todo.channel_id == channel_id) \
                        .filter(Todo.guild_id == guild_id) \
                        .delete(synchronize_session='fetch')
Example #5
0
def audit_log():
    class PseudoEvent:
        def __init__(self, reminder):
            self.event_name = 'create'
            self.bulk_count = None
            self.user = User.query.get(reminder.set_by)
            self.time = reminder.set_at
            self.reminder = reminder

    def combine(a, b):
        out = []

        while len(a) * len(b) > 0:
            if a[0].time > b[0].time:
                out.append(a.pop(0))
            else:
                out.append(b.pop(0))

        if len(a) > 0:
            out.extend(a)

        else:
            out.extend(b)

        return out

    if (guild_id := request.args.get('id')) is not None:
        member = User.query.get(get_internal_id())
        guild = Guild.query.filter(Guild.guild == guild_id).first_or_404()

        if member is None:
            return redirect(url_for('cache'))

        elif guild not in member.permitted_guilds():
            return abort(403)

        else:
            events = Event.query.filter(Event.guild_id == guild.id).order_by(
                Event.time.desc())
            pseudo_events = [
                PseudoEvent(r) for r in itertools.chain(
                    *[c.reminders for c in guild.channels])
            ]

            all_events = combine(events.all(), pseudo_events)

            return render_template('audit_log/audit_log.html',
                                   guilds=member.permitted_guilds(),
                                   guild=guild,
                                   member=member,
                                   events=all_events)
Example #6
0
def change_aliases():
    if (guild_id := request.json.get('guild_id')) is not None:

        member = User.query.get(get_internal_id())

        if guild_id in [x.id for x in member.permitted_guilds()]:

            if request.method == 'POST':
                if (command := request.json.get('command')) is not None and \
                        (name := request.json.get('name')) is not None:

                    if 1 < len(command) < 2048 and len(name) < 12:
                        if (alias_id := request.json.get('id')) is not None and \
                                (alias := CommandAlias.query.filter_by(guild_id=guild_id,
                                                                       id=alias_id).first()) is not None:

                            if name != alias.name and CommandAlias.query.filter_by(
                                    guild_id=guild_id,
                                    name=name).first() is not None:
                                return 'Name must be unique', 400

                            else:
                                alias.name = name
                                alias.command = command

                        else:
                            if CommandAlias.query.filter_by(
                                    guild_id=guild_id,
                                    name=name).first() is not None:
                                return 'Name must be unique', 400

                            else:
                                alias = CommandAlias(name=name,
                                                     command=command,
                                                     guild_id=guild_id)

                                db.session.add(alias)
                                db.session.commit()

                                return jsonify({
                                    'id': alias.id,
                                    'name': alias.name,
                                    'command': alias.command
                                })

                    else:
                        return 'Invalid input lengths', 400

                else:
                    abort(400)
Example #7
0
def settings_dashboard():
    if (guild_id := request.args.get('id')) is not None:
        member = User.query.get(get_internal_id())
        guild = Guild.query.filter(Guild.guild == guild_id).first_or_404()

        if member is None:
            return redirect(url_for('cache'))

        elif guild not in member.permitted_guilds():
            return abort(403)

        else:
            return render_template(
                'settings_dashboard/settings_dashboard.html',
                guilds=member.permitted_guilds(),
                guild=guild,
                member=member,
                command_restrictions=guild.command_restrictions)
Example #8
0
def change_blacklist():
    if (guild_id := request.json.get('guild_id')) is not None and \
            (channel_id := request.json.get('channel_id')) is not None:

        member = User.query.get(get_internal_id())
        guild = Guild.query.get(guild_id)
        channel = Channel.query.get(channel_id)

        if guild in member.permitted_guilds() and channel in guild.channels:

            if request.method == 'POST':
                channel.blacklisted = True

            else:  # method is delete
                channel.blacklisted = False

            db.session.commit()

            return '', 201

        else:
            abort(400)
def advanced_message_editor(guild_id: int, reminder_uid: str):
    member = User.query.get(get_internal_id())
    guild = Guild.query.filter(Guild.guild == guild_id).first_or_404()

    if member is None:
        return redirect(url_for('cache'))

    elif guild not in member.permitted_guilds():
        return abort(403)

    else:
        reminder = Reminder.query.filter(Reminder.uid == reminder_uid).first()

        if reminder is None:
            return abort(404)

        else:
            return render_template(
                'reminder_dashboard/advanced_message_editor/advanced_message_editor.html',
                guilds=member.permitted_guilds(),
                guild=guild,
                member=member,
                message=reminder,
                reminder_uid=reminder_uid)
                'Please upload a maximum of 8MB, with up to 260 character filename'
            )
            return redirect(
                url_for('advanced_message_editor',
                        guild_id=guild_id,
                        reminder_uid=reminder_uid))

    else:
        reminder.attachment = None
        reminder.attachment_name = None

    reminder.tts = field('tts') is not None

    reminder.content = field('message_content')

    Event.new_edit_event(reminder, get_internal_id())

    db.session.commit()

    return redirect(
        url_for('advanced_message_editor',
                guild_id=guild_id,
                reminder_uid=reminder_uid))


@app.route('/download_attachment/<reminder_uid>')
def download_attachment(reminder_uid):
    reminder = Reminder.query.filter(
        Reminder.uid == reminder_uid).first_or_404()

    if reminder is None or reminder.attachment is None: