예제 #1
0
    def on_message_create(self, event):
        if event.author.id == self.bot.client.state.me.id:
            return

        guild_state = self.bot.guild_state.get(event.guild.id)
        if not guild_state:
            return

        content = event.content

        # TODO: pull from config
        prefix = '!'
        if prefix and not content.startswith(prefix):
            return []
        else:
            content = content[len(prefix):]

        if not self.command_matches_re.match(content):
            return []

        options = []
        for command in self.commands:
            match = command.compiled_regex(self.group_abbrev).match(content)
            if match:
                options.append((command, match))
        options = sorted(options, key=lambda obj: obj[0].group is None)
        if not options:
            return

        user = None
        for command, match in options:
            # If this is an admin command, check if the user is an admin
            if command.level == -1:
                if not user:
                    user = User.with_id(event.author.id)

                if not user or not user.admin:
                    log.warning(
                        'User attempted to execute admin-only command %s (%s)',
                        command.name, event.author.id)
                    continue

            # TODO: permissions
            (command_event, kwargs) = command.execute(
                CommandEvent(command, event.message, match))
            gevent.spawn(command.func, guild_state, command_event, **kwargs)
예제 #2
0
def auth_discord_callback():
    if request.values.get('error'):
        return request.values['error']

    if 'state' not in session:
        raise APIError('No State', 400)

    discord = make_discord_session(state=session['state'])
    token = discord.fetch_token(
        current_app.config['DISCORD_TOKEN_URL'],
        client_secret=current_app.config['DISCORD_CLIENT_SECRET'],
        authorization_response=request.url)

    discord = make_discord_session(token=token)

    data = discord.get(current_app.config['DISCORD_API_BASE_URL'] +
                       '/users/@me').json()

    user = User.with_id(data['id'])

    if not user:
        User.create(
            user_id=data['id'],
            username=data['username'],
            discriminator=data['discriminator'],
            avatar=data['avatar'],
            bot=False,
        )
    else:
        User.update(
            username=data['username'],
            discriminator=data['discriminator'],
            avatar=data['avatar'],
        ).where(User.user_id == data['id']).execute()

    g.user = user
    return redirect('/')
예제 #3
0
def check_auth():
    g.user = None
    if 'user_id' in session:
        g.user = User.with_id(session['user_id'])