Ejemplo n.º 1
0
async def add_mapping(mapping: BotInstanceMapping, guildId: str):
    if mapping.bot_instance is not None:
        data.add_bot_instance(guildId, mapping.bot_instance)

        task = {
            'kwargs': {
                'bot_token': mapping.bot_instance
            },
            'function': 'start_new_bot_instance'
        }
        data.add_task(task)

    bot_instance = data.get_bot_instance(guildId)
    if not bot_instance:
        raise HTTPException(status_code=404, detail="Bot config not found")

    return {
        "bot_instance": bot_instance[BOT_TOKEN_KEY],
        "bot_avatar": bot_instance[BOT_AVATAR_KEY],
        "bot_name": bot_instance[BOT_NAME_KEY],
        'avatar_timout': bot_instance[AVATAR_TIMEOUT_KEY],
        'name_timeout': bot_instance[NAME_TIMEOUT_KEY],
        'activity_type': bot_instance[BOT_ACTIVITY_TYPE_KEY],
        'activity_text': bot_instance[BOT_ACTIVITY_TEXT_KEY],
        'bot_id': bot_instance[BOT_ID_KEY],
        "guildId": guildId
    }
Ejemplo n.º 2
0
async def delete_mapping(guildId: str):
    bot_instance = data.get_bot_instance(guildId)

    if not bot_instance:
        raise HTTPException(status_code=404, detail="Bot config not found")

    if guildId is not None:
        task = {
            'kwargs': {
                'guild_id': int(guildId)
            },
            'function': 'delete_bot_instance'
        }
        data.add_task(task)
Ejemplo n.º 3
0
def tasks():
    if request.method == 'POST':
        important = 0
        if 'important' in request.form:
            important = 1
        category = None
        if request.form['category'] != -1:
            category = request.form['category']

        date = datetime.datetime.strptime(request.form['due_date'], "%Y-%m-%d")
        data.add_task(request.form['description'], date.strftime("%d.%m.%Y"), important, category)

    t = data.get_tasks()
    c = data.get_category()
    return render_template('tasks.html', tasks=t, categories=c)
Ejemplo n.º 4
0
async def add_mapping(mapping: BotActivityMapping, guildId: str):
    bot_instance = data.get_bot_instance(guildId)

    if not bot_instance:
        raise HTTPException(status_code=404, detail="Bot config not found")

    task = {
        'kwargs': {
            'guild_id': int(guildId),
            'bot_id': int(bot_instance[BOT_ID_KEY]),
            'activity_type_str': mapping.activity_type,
            'activity_text': mapping.activity_text
        },
        'function': 'update_activity'
    }
    data.add_task(task)

    return {"success": 1}
Ejemplo n.º 5
0
async def add_mapping(mapping: BotAvatarMapping, guildId: str):
    bot_instance = data.get_bot_instance(guildId)
    if not bot_instance:
        raise HTTPException(status_code=404, detail="Bot config not found")

    # avatar timout
    if bot_instance[AVATAR_TIMEOUT_KEY] and int(
            bot_instance[AVATAR_TIMEOUT_KEY]) <= time.time():
        data.set_avatar_timout(bot_instance[GUILD_ID_KEY], 0)
        bot_instance[AVATAR_TIMEOUT_KEY] = 0

    if not bot_instance[AVATAR_TIMEOUT_KEY]:
        # read new avatar data and decode the base64 form
        ext, image_data = re.findall('/(.*);base64,(.*)',
                                     mapping.bot_avatar)[0]
        new_avatar = base64.decodebytes(image_data.encode('utf-8'))

        # get path to tmp folder where avatars will be stored temporarily
        tmp_folder = os.path.abspath('tmp/')
        if not os.path.exists(tmp_folder):
            os.mkdir(tmp_folder)

        # get path to new file and write bytes to it
        file_path = os.path.join(os.path.abspath('tmp/'),
                                 f'{guildId}_tmp_bot_avatar.{ext}')
        with open(file_path, 'wb+') as file:
            file.write(new_avatar)

        task = {
            'kwargs': {
                'guild_id': int(guildId),
                'bot_id': int(bot_instance[BOT_ID_KEY]),
                'new_avatar_path': file_path,
            },
            'function': 'update_avatar'
        }
        data.add_task(task)

    return {
        "bot_avatar": mapping.bot_avatar,
        'guildId': guildId,
        'avatar_timeout': int(bool(bot_instance[AVATAR_TIMEOUT_KEY]))
    }
Ejemplo n.º 6
0
async def add_mapping(mapping: BotNameMapping, guildId: str):
    bot_instance = data.get_bot_instance(guildId)
    if not bot_instance:
        raise HTTPException(status_code=404, detail="Bot config not found")

    # name timout
    if bot_instance[NAME_TIMEOUT_KEY] and int(bot_instance[NAME_TIMEOUT_KEY]) <= time.time():
        data.set_name_timeout(bot_instance[GUILD_ID_KEY], 0)
        bot_instance[NAME_TIMEOUT_KEY] = 0

    if mapping.bot_name and not bot_instance[NAME_TIMEOUT_KEY]:
        task = {
            'kwargs': {
                'guild_id': int(guildId),
                'bot_id': int(bot_instance[BOT_ID_KEY]),
                'new_name': mapping.bot_name,
            },
            'function': 'update_name'
        }
        data.add_task(task)

    return {"guildId": guildId, "bot_name": mapping.bot_name, 'name_timeout': int(bool(bot_instance[NAME_TIMEOUT_KEY]))}
Ejemplo n.º 7
0
async def add_mappings(mapping: AlertsSettings, guildId: str):
    if mapping.settings:
        data.set_alerts_settings(guildId, json.dumps(mapping.settings))

    output_data = data.get_alerts_settings(guildId)
    if not output_data:
        raise HTTPException(status_code=500,
                            detail="Failed to set alerts settings")

    # add timer if daily stats is enabled
    if output_data[ALERTS_SETTINGS_KEY]['daily_stats'][
            'enabled'] and output_data[ALERTS_SETTINGS_KEY]['daily_stats'][
                'instances']:
        # delete old timers
        data.delete_timers(str(guildId))

        # loop through each instance of the daily_stats
        for i, instance in enumerate(
                output_data[ALERTS_SETTINGS_KEY]['daily_stats']['instances']):
            task = {
                'kwargs': {
                    'guild_id':
                    int(guildId),
                    'timezone':
                    instance['settings']['timezone']
                    if 'timezone' in instance['settings'] else '0',
                    'channel':
                    instance['channel'],
                },
                'function': 'start_daily_stats_timers'
            }
            data.add_task(task)

    return {
        GUILD_ID_KEY: guildId,
        ALERTS_SETTINGS_KEY: output_data[ALERTS_SETTINGS_KEY]
    }