async def copy_warn(user_id: int, warn: models.Warn): await add_dbmember_if_not_exist(user_id) warn.id = utils.time_snowflake( utils.snowflake_time(warn.id) + datetime.timedelta(milliseconds=1)) while await get_warn(warn.id): warn.id = utils.time_snowflake( utils.snowflake_time(warn.id) + datetime.timedelta(milliseconds=1)) warn.user = user_id await warn.create()
def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int, int, int, int]): dt = utils.snowflake_time(snowflake) assert (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) == time_tuple assert utils.time_snowflake( dt, high=False) <= snowflake <= utils.time_snowflake(dt, high=True)
def test_snowflake_conversion(): from discord import utils now = datetime.datetime.utcnow() snowflake = utils.time_snowflake(now) time = utils.snowflake_time(snowflake) # Accept a millisecond or less of error assert abs((time - now).total_seconds()) <= 0.001
async def db_cleaner(self): if Configuration.get_master_var("purge_db", True): # purge all messages older then 6 weeks snowflake = time_snowflake( datetime.fromtimestamp(time() - 60 * 60 * 24 * 7 * 6)) purged_attachments = await LoggedAttachment.filter(id__lt=snowflake ).delete() purged = await LoggedMessage.filter(messageid__lt=snowflake ).delete() GearbotLogging.info( f"Purged {purged} old messages and {purged_attachments} attachments" )
def __init__(self, *args, message: discord.Message, **kwargs): d = { k: getattr(message, k, None) for k in things_for_fakemessage_to_steal } for k, v in d.items(): try: # log.debug(f"{k=} {v=}") setattr(self, k, v) except TypeError: # log.exception("This is fine") pass except AttributeError: # log.exception("This is fine") pass self.id = time_snowflake(datetime.utcnow(), high=False) # Pretend to be now self.type = discord.MessageType.default
def generate_id(): return utils.time_snowflake(datetime.datetime.now())
async def execute(self): if not self.data or not self.get_command_str(): log.warning( f"Could not execute Task[{self.name}] due to data problem: {self.data=}" ) return False guild: discord.Guild = self.bot.get_guild( self.guild_id) # used for get_prefix if guild is None: log.warning( f"Could not execute Task[{self.name}] due to missing guild: {self.guild_id}" ) return False channel: discord.TextChannel = guild.get_channel(self.channel_id) if channel is None: log.warning( f"Could not execute Task[{self.name}] due to missing channel: {self.channel_id}" ) return False author: discord.User = guild.get_member(self.author_id) if author is None: log.warning( f"Could not execute Task[{self.name}] due to missing author: {self.author_id}" ) return False actual_message: discord.Message = channel.last_message # I'd like to present you my chain of increasingly desperate message fetching attempts if actual_message is None: # log.warning("No message found in channel cache yet, skipping execution") # return actual_message = await channel.fetch_message( channel.last_message_id) if actual_message is None: # last_message_id was an invalid message I guess actual_message = await channel.history(limit=1).flatten() if not actual_message: # Basically only happens if the channel has no messages actual_message = await author.history(limit=1).flatten() if not actual_message: # Okay, the *author* has never sent a message? log.warning( "No message found in channel cache yet, skipping execution" ) return False actual_message = actual_message[0] message = FakeMessage(actual_message) # message = FakeMessage2 message.author = author message.guild = guild # Just in case we got desperate, see above message.channel = channel message.id = time_snowflake(datetime.utcnow(), high=False) # Pretend to be now message = neuter_message(message) # absolutely weird that this takes a message object instead of guild prefixes = await self.bot.get_prefix(message) if isinstance(prefixes, str): prefix = prefixes else: prefix = prefixes[0] message.content = f"{prefix}{self.get_command_str()}" if (not message.guild or not message.author or not message.content or message.content == prefix): log.warning( f"Could not execute Task[{self.name}] due to message problem: {message}" ) return False new_ctx: commands.Context = await self.bot.get_context(message) new_ctx.assume_yes = True if not new_ctx.valid: log.warning( f"Could not execute Task[{self.name}] due invalid context: " f"{new_ctx.invoked_with=} {new_ctx.prefix=} {new_ctx.command=}" ) return False await self.bot.invoke(new_ctx) return True
def get_msg_obj()->Optional[discord.Message]: msg = copy(_msg_obj) msg.id = time_snowflake(utcnow()) return msg
async def add_warn(self, user_id, issuer_id, reason): async with self.bot.holder as cur: snowflake = utils.time_snowflake(datetime.now()) await cur.execute('INSERT INTO warns VALUES(?, ?, ?, ?)', (snowflake, user_id, issuer_id, reason))