Example #1
0
 async def on_message(self, message):
     member = self.bot.guild.get_member(message.author.id)
     if isinstance(message.author, discord.Member) and (
         message.guild != self.bot.guild
     ):
         return
     with DBManager.create_session_scope() as db_session:
         user = User._create_or_get_by_discord_id(
             db_session,
             message.author.id,
             user_name=str(member) if member else str(message.author),
         )
         Message._create(
             db_session,
             message.id,
             message.author.id,
             message.channel.id
             if isinstance(message.author, discord.Member)
             else None,
             message.content,
         )
         db_session.commit()
         HandlerManager.trigger(
             "discord_message",
             message_raw=message,
             message=message.content,
             author=message.author,
             user_level=user.level if user else 50,
             channel=message.channel
             if isinstance(message.author, discord.Member)
             else None,
             whisper=not isinstance(message.author, discord.Member),
         )
Example #2
0
    def __init__(self, bot):
        self.bot = bot

        self.twitter_client = None
        self.listener = None

        if self.bot:
            HandlerManager.add_handler("twitter_follows",
                                       self.on_twitter_follows)

        if "twitter" not in bot.config:
            return

        twitter_config = bot.config["twitter"]

        if not (twitter_config.get("consumer_key", False)
                and twitter_config.get("consumer_secret", False)
                and twitter_config.get("access_token", False)
                and twitter_config.get("access_token_secret", False)):
            return

        try:
            self.twitter_auth = tweepy.OAuthHandler(
                twitter_config["consumer_key"],
                twitter_config["consumer_secret"])
            self.twitter_auth.set_access_token(
                twitter_config["access_token"],
                twitter_config["access_token_secret"])

            self.twitter_client = tweepy.API(self.twitter_auth)
        except:
            log.exception("Twitter authentication failed.")
            self.twitter_client = None
Example #3
0
 async def on_ready(self):
     self.bot.guild = self.get_guild(int(self.bot.settings["discord_guild_id"]))
     if not self.bot.guild:
         log.error("Discord Guild not found!")
         return
     log.info(f"Discord Bot has started with id {self.user.id}")
     HandlerManager.trigger("discord_ready")
Example #4
0
    def wait_discord_load(self):
        self.socket_manager = SocketManager(self.bot_name, self.execute_now)
        self.module_manager = ModuleManager(self.socket_manager,
                                            bot=self).load()

        self.commands = CommandManager(
            socket_manager=self.socket_manager,
            module_manager=self.module_manager,
            bot=self,
        ).load()
        HandlerManager.trigger("manager_loaded")

        # promote the admin to level 2000
        owner = self.config["main"].get("owner_id", None)
        if owner is None:
            log.warning(
                "No admin user specified. See the [main] section in the example config for its usage."
            )
        else:
            with DBManager.create_session_scope() as db_session:
                owner = User._create_or_get_by_discord_id(
                    db_session, str(owner))
                if owner is None:
                    log.warning(
                        "The login name you entered for the admin user does not exist on twitch. "
                        "No admin user has been created.")
                else:
                    owner.level = 2000
Example #5
0
 def enable(self, bot):
     if not bot:
         return
     if not self.bot.movienight_api.active:
         log.error("API is not running!")
         return
     
     HandlerManager.add_handler("movie_night_started", self.movie_night_started_event)
Example #6
0
 def quit_bot(self):
     HandlerManager.trigger("on_quit")
     try:
         ScheduleManager.base_scheduler.print_jobs()
         ScheduleManager.base_scheduler.shutdown(wait=False)
     except:
         log.exception("Error while shutting down the apscheduler")
     self.private_loop.call_soon_threadsafe(self.private_loop.stop)
     self.socket_manager.quit()
Example #7
0
    def __init__(self, bot, settings, redis, private_loop):
        self.bot = bot
        self.settings = settings
        self.client = CustomClient(self)

        self.private_loop = private_loop
        self.redis = redis

        self.guild = None
        if not self.redis.get("timeouts-discord") or not json.loads(self.redis.get("timeouts-discord")):
            self.redis.set("timeouts-discord", json.dumps({}))
        
        HandlerManager.add_handler("discord_ready", self.initial_unbans)
Example #8
0
    def enable(self, bot):
        if not bot:
            return

        HandlerManager.add_handler("on_message", self.on_message, priority=100)
        HandlerManager.add_handler("on_commit", self.on_commit)

        if self.db_session is not None:
            self.db_session.commit()
            self.db_session.close()
            self.db_session = None
        self.db_session = DBManager.create_session()
        self.blacklisted_links = []
        for link in self.db_session.query(BlacklistedLink):
            self.blacklisted_links.append(link)

        self.whitelisted_links = []
        for link in self.db_session.query(WhitelistedLink):
            self.whitelisted_links.append(link)
Example #9
0
    def __init__(self, config, args):
        self.config = config
        self.args = args
        self.private_loop = asyncio.get_event_loop()
        self.private_loop.set_exception_handler(custom_exception_handler)

        self.discord_token = self.config["main"]["discord_token"]

        ScheduleManager.init(self.private_loop)

        DBManager.init(self.config["main"]["db"])

        ActionParser.bot = self

        # redis
        redis_options = {}
        if "redis" in config:
            redis_options = dict(config.items("redis"))
        RedisManager.init(**redis_options)
        utils.wait_for_redis_data_loaded(RedisManager.get())

        # SQL migrations
        try:
            with DBManager.create_dbapi_connection_scope() as sql_conn:
                sql_migratable = DatabaseMigratable(sql_conn)
                sql_migration = Migration(sql_migratable,
                                          greenbot.migration_revisions.db,
                                          self)
                sql_migration.run()
        except ValueError as error:
            log.error(error)

        HandlerManager.init_handlers()

        self.movienight_api = MovieNightAPI(self, self.config["wsc"],
                                            self.config["wowza_cdn"])

        HandlerManager.add_handler("parse_command_from_message",
                                   self.parse_command_from_message)
        self.bot_name = self.config["main"]["bot_name"]
        self.command_prefix = self.config["discord"]["command_prefix"]
        self.settings = {
            "discord_token": self.discord_token,
            "bot_name": self.bot_name,
            "command_prefix": self.command_prefix,
            "discord_guild_id": self.config["discord"]["discord_guild_id"],
        }

        HandlerManager.add_handler("discord_ready", self.wait_discord_load)

        self.discord_bot = DiscordBotManager(
            bot=self,
            settings=self.settings,
            redis=RedisManager.get(),
            private_loop=self.private_loop,
        )
        self.twitter_manager = TwitterManager(self)
        self.filters = Filters(self, self.discord_bot)
        self.functions = Functions(self, self.filters)
Example #10
0
    def disable(self, bot):
        if not bot:
            return

        HandlerManager.remove_handler("discord_raw_message_edit", self.message_edit)
        HandlerManager.remove_handler("discord_raw_message_delete", self.message_delete)
        HandlerManager.remove_handler("discord_member_update", self.member_update)
        HandlerManager.remove_handler("discord_guild_role_update", self.role_update)
        HandlerManager.remove_handler("discord_guild_role_create", self.role_create)
        HandlerManager.remove_handler("discord_guild_role_delete", self.role_delete)
        HandlerManager.remove_handler("discord_voice_state_update", self.voice_change)
        HandlerManager.remove_handler("discord_member_remove", self.member_remove)
        HandlerManager.remove_handler("discord_member_join", self.member_join)
        HandlerManager.remove_handler(
            "discord_guild_channel_update", self.channel_update
        )
        HandlerManager.remove_handler(
            "discord_guild_channel_create", self.channel_create
        )
        HandlerManager.remove_handler("discord_guild_update", self.guild_update)
        HandlerManager.remove_handler("discord_guild_emojis_update", self.emoji_update)
        HandlerManager.remove_handler("discord_invite_create", self.invite_create)
        HandlerManager.remove_handler("discord_invite_delete", self.invite_delete)
        HandlerManager.remove_handler("aml_custom_log", self.custom_event_log)
Example #11
0
 def __init__(self, bot):
     self.bot = bot
     HandlerManager.add_handler("discord_message", self.on_message)
     HandlerManager.add_handler("discord_raw_message_edit",
                                self.edit_message,
                                priority=1000)
Example #12
0
 def enable(self, bot):
     if not bot:
         return
     HandlerManager.add_handler("twitter_on_status", self.on_status)
Example #13
0
 def disable(self, bot):
     if not bot:
         return
     HandlerManager.remove_handler("twitter_on_status", self.on_status)
Example #14
0
    def disable(self, bot):
        if not bot:
            return

        HandlerManager.remove_handler("movie_night_started", self.movie_night_started_event)