コード例 #1
0
async def random_quote(bot):
    """
    Retorna um quote aleatório.
    """
    server = make_hash('id', bot.guild.id)
    payload = Query.get_quotes(server.decode('utf-8'))
    client = get_gql_client(BACKEND_URL)

    try:
        response = client.execute(payload)
    except Exception as err:
        print(f'Erro: {str(err)}\n\n')
        return await bot.send('Buguei')

    quotes = response.get('quotes')
    if not quotes:
        return await bot.send('Ainda não aprendi quotes neste servidor')

    # sorteia um quote vindo da memória de longo rpazo
    chosen_quote = choice([quote['quote'] for quote in quotes])
    # recupera os últimos quotes ditos nesse server da memória de curto prazo
    server_memory = get_short_memory_value(server)

    # se o quote sorteado não for um quote repetido
    if chosen_quote not in server_memory.get('last_quotes', []):
        # atualiza memória de curto prazo e retorna o quote sorteado
        server_memory['last_quotes'].append(chosen_quote)
        if len(server_memory['last_quotes']) > 10:
            server_memory['last_quotes'].pop(0)
        set_short_memory_value(server, server_memory)
        return await bot.send(chosen_quote)

    # se ela souber menos que 10 quotes nesse server pode retornar o quote repetido mesmo
    if len(quotes) < 10:
        return await bot.send(chosen_quote)

    # Se não tem que ir sorteando quotes até não ser repetido
    while chosen_quote in server_memory['last_quotes']:
        chosen_quote = choice([quote['quote'] for quote in quotes])

    # Atualiza a memória de curto rpazo ao selecionar o quote
    server_memory['last_quotes'].append(chosen_quote)
    if len(server_memory['last_quotes']) > 10:
        server_memory['last_quotes'].pop(0)
    set_short_memory_value(server, server_memory)

    return await bot.send(chosen_quote)
コード例 #2
0
ファイル: commands.py プロジェクト: brunolcarli/Luci
async def random_quote(bot):
    """
    Retorna um quote aleatório.
    """
    server = make_hash('id', bot.guild.id)
    payload = Query.get_quotes(server.decode('utf-8'))
    client = get_gql_client(BACKEND_URL)

    try:
        response = client.execute(payload)
    except Exception as err:
        print(f'Erro: {str(err)}\n\n')
        return await bot.send('Buguei')

    quotes = response.get('quotes')
    if not quotes:
        return await bot.send('Ainda não aprendi quotes neste servidor')

    chosen_quote = choice([quote['quote'] for quote in quotes])

    return await bot.send(chosen_quote)