Пример #1
0
    async def initial_unbans(self):
        try:
            data = json.loads(self.redis.get(f"{self.bot.bot_name}:timeouts-discord"))
            for user in data:
                unban_date = utils.parse_date(data[user]["unban_date"])
                time_now = utils.now()
                resp_timeout = data.get("resp_timeout", "")
                resp_timeout += " after " if resp_timeout else ""
                if unban_date < time_now:
                    ScheduleManager.execute_now(
                        method=self.unban,
                        args=[
                            data[user]["discord_id"],
                            f"Unbanned by timer{resp_timeout}",
                        ],
                    )
                    continue
                ScheduleManager.execute_delayed(
                    delay=(unban_date - time_now).seconds,
                    method=self.unban,
                    args=[data[user]["discord_id"], f"Unbanned by timer{resp_timeout}"],
                )
        except Exception as e:
            log.exception(e)
            self.redis.set(f"{self.bot.bot_name}:timeouts-discord", json.dumps({}))

        with DBManager.create_session_scope() as db_session:
            unnamed_users = db_session.query(User).filter_by(user_name="").all()
            for user in unnamed_users:
                member = self.get_member(int(user.discord_id))
                if not member:
                    db_session.delete(user)
                    continue

                user.user_name = str(member)
Пример #2
0
 def on_status(self, status):
     if (status.user.screen_name.lower() in self.relevant_users
             and not status.text.startswith("RT ")):
         log.info("On status from tweepy: %s", status.text)
         tweet = status
         ScheduleManager.execute_now(self.dispatch_tweet,
                                     args=[tweet])
Пример #3
0
    def run(self, bot, author, channel, message, args):
        if self.action is None:
            log.warning("This command is not available.")
            return False

        if args["user_level"] < self.level:
            # User does not have a high enough power level to run this command
            return False

        if args["whisper"] and self.can_execute_with_whisper is False:
            # This user cannot execute the command through a whisper
            return False

        cd_modifier = 0.2 if args["user_level"] >= 500 else 1.0

        cur_time = greenbot.utils.now().timestamp()
        time_since_last_run = (cur_time - self.last_run) / cd_modifier

        if (time_since_last_run < self.delay_all
                and args["user_level"] < Command.BYPASS_DELAY_LEVEL):
            log.debug(
                f"Command was run {time_since_last_run:.2f} seconds ago, waiting..."
            )
            return False

        time_since_last_run_user = (cur_time - self.last_run_by_user.get(
            str(author.id), 0)) / cd_modifier

        if (time_since_last_run_user < self.delay_user
                and args["user_level"] < Command.BYPASS_DELAY_LEVEL):
            log.debug(
                f"{author.name}#{author.discriminator} ran command {time_since_last_run_user:.2f} seconds ago, waiting..."
            )
            return False
        with DBManager.create_session_scope() as db_session:
            user = User._create_or_get_by_discord_id(db_session,
                                                     str(author.id),
                                                     str(author))
            if self.cost > 0 and not user.can_afford(self.cost):
                # User does not have enough points to use the command
                return False

            args.update(self.extra_args)
            if self.run_in_thread:
                log.debug(f"Running {self} in a thread")
                ScheduleManager.execute_now(
                    self.run_action,
                    args=[bot, author, channel, message, args])
            else:
                self.run_action(bot, author, channel, message, args)

        return True
Пример #4
0
 def initial_unbans(self):
     try:
         data = json.loads(self.redis.get("timeouts-discord"))
         for user in data:
             unban_date = data[user]["unban_date"]
             if ":" in unban_date[-5:]:
                 unban_date = f"{unban_date[:-5]}{unban_date[-5:-3]}{unban_date[-2:]}"
             unban_date = datetime.strptime(unban_date, "%Y-%m-%d %H:%M:%S.%f%z")
             time_now = utils.now()
             if unban_date < time_now:
                 ScheduleManager.execute_now(method=self.unban, args=[data[user]["discord_id"], "Unbanned by timer"])
                 continue
             ScheduleManager.execute_delayed(delay=(unban_date - time_now).seconds, method=self.unban, args=[data[user]["discord_id"], "Unbanned by timer"])
     except Exception as e:
         log.exception(e)
         self.redis.set("timeouts-discord", json.dumps({}))
Пример #5
0
    def run(self, bot, author, channel, message, args):
        extra = self.get_extra_data(author, channel, message, args)
        resp, embed = self.get_response(bot, extra)
        if self.functions:
            run_functions(
                self.functions,
                bot,
                extra,
                author,
                channel,
                args,
                self.num_urlfetch_subs,
                True,
            )

        if not resp and embed:
            return False

        if self.num_urlfetch_subs == 0:
            return bot.private_message(author, resp, embed)

        return ScheduleManager.execute_now(
            urlfetch_msg,
            args=[],
            kwargs={
                "args": [author],
                "kwargs": {},
                "method": bot.private_message,
                "bot": bot,
                "extra": extra,
                "message": resp,
                "embed": embed,
                "num_urlfetch_subs": self.num_urlfetch_subs,
            },
        )
Пример #6
0
def run_functions(
    functions, bot, extra, author, channel, args, num_urlfetch_subs, private_message
):
    for func in functions:
        final_args = get_argument_substitutions_array(
            get_substitutions_array(func.arguments, bot, extra), extra
        )
        resp, embed = func.cb(final_args, extra)
        if num_urlfetch_subs == 0:

            return (
                bot.private_message(author, resp, embed)
                if private_message
                else bot.say(channel, resp, embed)
            )

        return ScheduleManager.execute_now(
            urlfetch_msg,
            args=[],
            kwargs={
                "args": [author if private_message else channel],
                "kwargs": {},
                "method": bot.private_message if private_message else bot.say,
                "bot": bot,
                "extra": extra,
                "message": resp,
                "embed": embed,
                "num_urlfetch_subs": num_urlfetch_subs,
            },
        )
Пример #7
0
    def load_commands(self, **options):
        if not self.bot:
            return

        ScheduleManager.execute_now(self.update_manager)
Пример #8
0
 def enable(self, bot):
     if not bot:
         return
     ScheduleManager.execute_now(self.process_messages)
     self.process_messages_job = ScheduleManager.execute_every(
         3600, self.process_messages)  # Checks every hour