async def on_member_update(before: discord.Member, after: discord.Member, **kargs: Any) -> None: inc_statistics_better(before.guild.id, "on-member-update", kargs["kernel_ramfs"]) username_log = load_message_config(before.guild.id, kargs["ramfs"], datatypes=join_leave_user_logs)["username-log"] if username_log and (channel := kargs["client"].get_channel(int(username_log))): if before.nick == after.nick: return message_embed = discord.Embed(title="Nickname updated", color=load_embed_color(before.guild, embed_colors.edit, kargs["ramfs"])) message_embed.set_author(name=f"{before} ({before.id})", icon_url=user_avatar_url(before)) message_embed.add_field(name=("Before" + " | False" * (not before.nick)), value=str(before.nick)) message_embed.add_field(name=("After" + " | False" * (not after.nick)), value=str(after.nick)) message_embed.timestamp = ts = datetime_now() message_embed.set_footer(text=f"unix: {int(ts.timestamp())}") await catch_logging_error(channel, message_embed)
async def avatar_function(message: discord.Message, args: List[str], client: discord.Client, **kwargs: Any) -> Any: if not message.guild: return 1 user, _ = await parse_user_member_noexcept(message, args, client, default_self=True) embed = discord.Embed(description=f"{user.mention}'s Avatar", color=load_embed_color(message.guild, embed_colors.primary, kwargs["ramfs"])) embed.set_image(url=user_avatar_url(user)) embed.timestamp = datetime_now() try: await message.channel.send(embed=embed) except discord.errors.Forbidden: raise lib_sonnetcommands.CommandError(constants.sonnet.error_embed)
async def grab_an_adult(discord_message: discord.Message, guild: discord.Guild, client: discord.Client, mconf: Dict[str, Any], ramfs: lexdpyk.ram_filesystem) -> None: if mconf["regex-notifier-log"] and (notify_log := client.get_channel(int(mconf["regex-notifier-log"]))): if not isinstance(notify_log, discord.TextChannel): return message_content = generate_reply_field(discord_message) # Message has been grabbed, start generating embed message_embed = discord.Embed(title=f"Auto Flagged Message in #{discord_message.channel}", description=message_content, color=load_embed_color(guild, embed_colors.primary, ramfs)) message_embed.set_author(name=str(discord_message.author), icon_url=user_avatar_url(discord_message.author)) message_embed.timestamp = discord_message.created_at # Grab files async awaitobjs = [asyncio.create_task(i.to_file()) for i in discord_message.attachments] fileobjs = [await i for i in awaitobjs] await catch_logging_error(notify_log, message_embed, fileobjs)
async def build_starboard_embed(message: discord.Message) -> discord.Embed: # Generate replies message_content = generate_reply_field(message) # Generate embed starboard_embed = discord.Embed(title="Starred message", description=message_content, color=0xffa700) for i in message.attachments: if any( i.url.endswith(ext) for ext in [".png", ".bmp", ".jpg", ".jpeg", ".gif", ".webp"]): starboard_embed.set_image(url=i.url) starboard_embed.set_author(name=str(message.author), icon_url=user_avatar_url(message.author)) starboard_embed.timestamp = message.created_at starboard_embed.set_footer(text=f"#{message.channel}") return starboard_embed
async def on_member_join(member: discord.Member, **kargs: Any) -> None: client: discord.Client = kargs["client"] ramfs: lexdpyk.ram_filesystem = kargs["ramfs"] inc_statistics_better(member.guild.id, "on-member-join", kargs["kernel_ramfs"]) notifier_cache = load_message_config(member.guild.id, ramfs, datatypes=join_notifier) issues: List[str] = [] # Handle notifer logging if member.id in notifier_cache["notifier-log-users"]: issues.append("User") if abs(discord_datetime_now().timestamp() - member.created_at.timestamp()) < int(notifier_cache["notifier-log-timestamp"]): issues.append("Timestamp") if int(notifier_cache["notifier-log-defaultpfp"]) and has_default_avatar(member): issues.append("Default pfp") if issues: asyncio.create_task(notify_problem(member, issues, notifier_cache["regex-notifier-log"], client, ramfs)) joinlog = load_message_config(member.guild.id, ramfs, datatypes=join_leave_user_logs)["join-log"] # Handle join logs if joinlog and (logging_channel := client.get_channel(int(joinlog))): embed = discord.Embed(title=f"{member} joined.", description=f"*{member.mention} joined the server.*", color=load_embed_color(member.guild, embed_colors.creation, ramfs)) embed.set_thumbnail(url=user_avatar_url(member)) embed.timestamp = ts = datetime_now() embed.set_footer(text=f"uid: {member.id}, unix: {int(ts.timestamp())}") embed.add_field(name="Created", value=parsedate(member.created_at), inline=True) if isinstance(logging_channel, discord.TextChannel): asyncio.create_task(catch_logging_error(logging_channel, embed))
async def on_member_remove(member: discord.Member, **kargs: Any) -> None: inc_statistics_better(member.guild.id, "on-member-remove", kargs["kernel_ramfs"]) log_channels = load_message_config(member.guild.id, kargs["ramfs"], datatypes=join_leave_user_logs) # Try for leave-log, default to join-log if (joinlog := (log_channels["leave-log"] or log_channels["join-log"])): if logging_channel := kargs["client"].get_channel(int(joinlog)): # Only run if in a TextChannel if not isinstance(logging_channel, discord.TextChannel): return embed = discord.Embed(title=f"{member} left.", description=f"*{member.mention} left the server.*", color=load_embed_color(member.guild, embed_colors.deletion, kargs["ramfs"])) embed.set_thumbnail(url=user_avatar_url(member)) embed.timestamp = ts = datetime_now() embed.set_footer(text=f"uid: {member.id}, unix: {int(ts.timestamp())}") embed.add_field(name="Created", value=parsedate(member.created_at), inline=True) embed.add_field(name="Joined", value=parsedate(member.joined_at), inline=True) await catch_logging_error(logging_channel, embed)
async def profile_function(message: discord.Message, args: List[str], client: discord.Client, **kwargs: Any) -> Any: if not message.guild: return 1 user, member = await parse_user_member_noexcept(message, args, client, default_self=True) # Status hashmap status_map = { "online": "🟢 (online)", "offline": "⚫ (offline)", "idle": "🟡 (idle)", "dnd": "🔴 (dnd)", "do_not_disturb": "🔴 (dnd)", "invisible": "⚫ (offline)" } embed = discord.Embed(title="User Information", description=f"User information for {user.mention}:", color=load_embed_color(message.guild, embed_colors.primary, kwargs["ramfs"])) embed.set_thumbnail(url=user_avatar_url(user)) embed.add_field(name="Username", value=str(user), inline=True) embed.add_field(name="User ID", value=str(user.id), inline=True) if member: embed.add_field(name="Status", value=status_map[member.raw_status], inline=True) embed.add_field(name="Highest Rank", value=f"{member.top_role.mention}", inline=True) embed.add_field(name="Created", value=parsedate(user.created_at), inline=True) if member: embed.add_field(name="Joined", value=parsedate(member.joined_at), inline=True) # Parse adding infraction count with db_hlapi(message.guild.id) as db: viewinfs = parse_boolean( db.grab_config("member-view-infractions") or "0") moderator = await parse_permissions(message, kwargs["conf_cache"], "moderator", verbose=False) if moderator or (viewinfs and user.id == message.author.id): embed.add_field( name="Infractions", value=f"{db.grab_filter_infractions(user=user.id, count=True)}" ) embed.timestamp = datetime_now() try: await message.channel.send(embed=embed) except discord.errors.Forbidden: raise lib_sonnetcommands.CommandError(constants.sonnet.error_embed)
async def on_message_delete(message: discord.Message, **kargs: Any) -> None: client: discord.Client = kargs["client"] kernel_ramfs: lexdpyk.ram_filesystem = kargs["kernel_ramfs"] ramfs: lexdpyk.ram_filesystem = kargs["ramfs"] # Ignore bots if parse_skip_message(client, message): return elif not message.guild: return files: Optional[List[discord.File]] = grab_files(message.guild.id, message.id, kernel_ramfs, delete=True) inc_statistics_better(message.guild.id, "on-message-delete", kernel_ramfs) # Add to log with db_hlapi(message.guild.id) as db: message_log = db.grab_config("message-log") try: if not (message_log and (log_channel := client.get_channel(int(message_log)))): return except ValueError: try: await message.channel.send("ERROR: message-log config is corrupt in database, please reset") except discord.errors.Forbidden: pass return if not isinstance(log_channel, discord.TextChannel): return message_embed = discord.Embed( title=f"Message deleted in #{message.channel}", description=message.content[:constants.embed.description], color=load_embed_color(message.guild, embed_colors.deletion, ramfs) ) # Parse for message lengths >2048 (discord now does 4000 hhhhhh) if len(message.content) > constants.embed.description: limend = constants.embed.description + constants.embed.field.value message_embed.add_field(name="(Continued)", value=message.content[constants.embed.description:limend]) if len(message.content) > limend: flimend = limend + constants.embed.field.value message_embed.add_field(name="(Continued further)", value=message.content[limend:flimend]) message_embed.set_author(name=f"{message.author} ({message.author.id})", icon_url=user_avatar_url(message.author)) if (r := message.reference) and (rr := r.resolved) and isinstance(rr, discord.Message): message_embed.add_field(name="Replying to:", value=f"{rr.author.mention} [(Link)]({rr.jump_url})")
async def on_message_edit(old_message: discord.Message, message: discord.Message, **kargs: Any) -> None: client: discord.Client = kargs["client"] ramfs: lexdpyk.ram_filesystem = kargs["ramfs"] kernel_ramfs: lexdpyk.ram_filesystem = kargs["kernel_ramfs"] # Ignore bots if parse_skip_message(client, message): return elif not message.guild: return inc_statistics_better(message.guild.id, "on-message-edit", kernel_ramfs) # Add to log with db_hlapi(message.guild.id) as db: message_log_str = db.grab_config("message-edit-log") or db.grab_config("message-log") # Skip logging if message is the same or mlog doesnt exist if message_log_str and not (old_message.content == message.content): if message_log := client.get_channel(int(message_log_str)): if not isinstance(message_log, discord.TextChannel): return lim: int = constants.embed.field.value message_embed = discord.Embed(title=f"Message edited in #{message.channel}", color=load_embed_color(message.guild, embed_colors.edit, ramfs)) message_embed.set_author(name=f"{message.author} ({message.author.id})", icon_url=user_avatar_url(message.author)) old_msg = (old_message.content or "NULL") message_embed.add_field(name="Old Message", value=(old_msg)[:lim], inline=False) if len(old_msg) > lim: message_embed.add_field(name="(Continued)", value=(old_msg)[lim:lim * 2], inline=False) msg = (message.content or "NULL") message_embed.add_field(name="New Message", value=(msg)[:lim], inline=False) if len(msg) > lim: message_embed.add_field(name="(Continued)", value=(msg)[lim:lim * 2], inline=False) message_embed.set_footer(text=f"Message ID: {message.id}") message_embed.timestamp = datetime_now() asyncio.create_task(catch_logging_error(message_log, message_embed, None))
async def grab_guild_message(message: discord.Message, args: List[str], client: discord.Client, ctx: CommandCtx) -> Any: if not message.guild: return 1 discord_message, nargs = await parse_channel_message_noexcept( message, args, client) if not discord_message.guild: await message.channel.send("ERROR: Message not in any guild") return 1 sendraw = False for arg in args[nargs:]: if arg in ["-r", "--raw"]: sendraw = True break # Generate replies message_content = generate_reply_field(discord_message) # Message has been grabbed, start generating embed message_embed = discord.Embed( title=f"Message in #{discord_message.channel}", description=message_content, color=load_embed_color(message.guild, embed_colors.primary, ctx.ramfs)) message_embed.set_author(name=str(discord_message.author), icon_url=user_avatar_url(discord_message.author)) message_embed.timestamp = discord_message.created_at # Grab files from cache fileobjs = grab_files(discord_message.guild.id, discord_message.id, ctx.kernel_ramfs) # Grab files async if not in cache if not fileobjs: awaitobjs = [ asyncio.create_task(i.to_file()) for i in discord_message.attachments ] fileobjs = [await i for i in awaitobjs] if sendraw: file_content = io.BytesIO(discord_message.content.encode("utf8")) fileobjs.append( discord.File( file_content, filename= f"{discord_message.id}.at.{int(datetime_now().timestamp())}.txt" )) try: await message.channel.send(embed=message_embed, files=fileobjs) except discord.errors.HTTPException: try: await message.channel.send( "There were files attached but they exceeded the guild filesize limit", embed=message_embed) except discord.errors.Forbidden: await message.channel.send(constants.sonnet.error_embed) return 1
c = client.get_channel(chan) log_channel = c if isinstance(c, discord.TextChannel) else None # Send infraction to database db.add_infraction(generated_id, str(user.id), str(moderator.id), i_type, i_reason, int(timestamp.timestamp())) if log_channel: log_embed = discord.Embed(title=BOT_NAME, description=f"New infraction for {user}:", color=load_embed_color( message.guild, embed_colors.creation, ramfs)) log_embed.set_thumbnail(url=user_avatar_url(user)) log_embed.add_field(name="Infraction ID", value=generated_id) log_embed.add_field(name="Moderator", value=moderator.mention) log_embed.add_field(name="User", value=user.mention) log_embed.add_field(name="Type", value=i_type) log_embed.add_field(name="Reason", value=i_reason) log_embed.set_footer( text=f"uid: {user.id}, unix: {int(timestamp.timestamp())}") asyncio.create_task(catch_logging_error(log_embed, log_channel)) if not to_dm: return generated_id, None dm_embed = discord.Embed(