Esempio n. 1
0
import webbrowser
from typing import Callable, FrozenSet, List, Iterator, Type

from elasticsearch import Elasticsearch
from elasticsearch_dsl.analysis import Analyzer
from elasticsearch_dsl.connections import get_connection
from tldextract import extract

from sahyun_bot.elastic import CustomDLC, ManualUserRank
from sahyun_bot.elastic_settings import BaseDoc, TEST_ONLY_VALUES
from sahyun_bot.the_danger_zone import nuke_from_orbit
from sahyun_bot.utils import debug_ex
from sahyun_bot.utils_logging import get_logger

LOG = get_logger(__name__)

DOCUMENTS = frozenset([
    CustomDLC,
    ManualUserRank,
])


class ElasticAware:
    def __init__(self, use_elastic: bool = False):
        self.use_elastic = use_elastic

    def set_use_elastic(self, use: bool):
        LOG.warning('Elastic is %sabled for %s.', 'en' if use else 'dis',
                    type(self).__name__)
        self.use_elastic = use
Esempio n. 2
0
"""
Main module of the application. Launches the bot, begins listening to commands and executing them.
"""
from sahyun_bot.modules import *
from sahyun_bot.utils_bot import setup_console
from sahyun_bot.utils_elastic import setup_elastic
from sahyun_bot.utils_logging import get_logger

LOG = get_logger('bot')  # __name__ becomes main


def run_main():
    LOG.warning('Launching bot...')
    setup_elastic(us, tl)
    bot.launch_in_own_thread()
    setup_console(tc)
    print_error_warning()


def print_error_warning():
    LOG.warning('---------------------------------------------------------')
    LOG.warning('Event loop error may follow, but you can ignore it.')
    LOG.warning('WindowsSelectorEventLoopPolicy is missing sometimes.')
    LOG.warning('No, it is not the Python version, it was added with 3.7.')
    LOG.warning('Apparently, it would fix the problem.')
    LOG.warning('At least that is what somebody on the internet said.')
    LOG.warning('Well, they also said to ignore all errors on close.')
    LOG.warning('I have not found a way to do that.')
    LOG.warning('Must have been a lie.')
    LOG.warning('Well, know that it was worse. Like, way worse.')
    LOG.warning('We are talking multiple pages of errors worse.')
from itertools import chain

from sahyun_bot.utils_logging import get_logger

LOG = get_logger('thedangerzone')  # __name__ becomes main


def nuke_from_orbit(reason: str):
    """
    Forcibly and immediately shuts down the application. To be used when data integrity is at risk.
    """
    LOG.critical(f'Forcing shutdown of the application. Reason: {reason}')
    import os
    os._exit(1)


if __name__ == '__main__':
    """
    Main function for an interactive shell. Loads a bunch of utils into locals() so they can be used immediately.
    Use bot.py for a sane main function.
    """
    # noinspection PyUnresolvedReferences
    from collections import Counter
    from sahyun_bot.modules import *
    from sahyun_bot.elastic import *
    from sahyun_bot.utils_elastic import setup_elastic_usage

    setup_elastic_usage(us, tl, use_elastic=True)

    local_utils = [
        m[:-3] for m in os.listdir(os.path.dirname(__file__))