Skip to content

Commit

Permalink
Update erm.py
Browse files Browse the repository at this point in the history
fix join/leave logs
fix kill logs
fix rdm detection
(hopefully)
note: wait for pytests !
  • Loading branch information
NoahCxrest authored Nov 13, 2024
1 parent 43cb944 commit 82fab78
Showing 1 changed file with 119 additions and 135 deletions.
254 changes: 119 additions & 135 deletions erm.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,88 +882,77 @@ async def check_whitelisted_car():

@tasks.loop(seconds=120, reconnect=True)
async def iterate_prc_logs():
# This will aim to constantly update the PRC Logs
# and the relevant storage data.
async for item in bot.settings.db.find({'ERLC': {'$exists': True}}):
try:
guild = await bot.fetch_guild(item['_id'])
except discord.HTTPException:
continue

settings = await bot.settings.find_by_id(guild.id)
try:
kill_logs_channel = await fetch_get_channel(guild, item['ERLC'].get('kill_logs'))
player_logs_channel = await fetch_get_channel(guild, item['ERLC'].get('player_logs'))
except KeyError:
continue

if not kill_logs_channel and not player_logs_channel:
continue
try:
kill_logs: list[prc_api.KillLog] = await bot.prc_api.fetch_kill_logs(guild.id)
player_logs: list[prc_api.JoinLeaveLog] = await bot.prc_api.fetch_player_logs(guild.id)
except prc_api.ResponseFailure:
continue
try:
all_settings = await bot.settings.db.find({'ERLC': {'$exists': True}}).to_list(None)

for item in all_settings:
try:
guild = bot.get_guild(item['_id'])
if guild is None:
guild = await bot.fetch_guild(item['_id'])
settings = await bot.settings.find_by_id(guild.id)

try:
kill_logs_channel = await fetch_get_channel(guild, item['ERLC'].get('kill_logs'))
player_logs_channel = await fetch_get_channel(guild, item['ERLC'].get('player_logs'))
except KeyError:
continue

sorted_kill_logs = sorted(kill_logs, key=lambda x: x.timestamp, reverse=False)
sorted_player_logs = sorted(player_logs, key=lambda x: x.timestamp, reverse=False)
players = {}
current_timestamp = int(datetime.datetime.now(tz=pytz.UTC).timestamp())
if not kill_logs_channel and not player_logs_channel:
continue

if kill_logs_channel is not None:
for item in sorted_kill_logs:
if (current_timestamp - item.timestamp) > 120:
try:
kill_logs = await bot.prc_api.fetch_kill_logs(guild.id)
player_logs = await bot.prc_api.fetch_player_logs(guild.id)
except prc_api.ResponseFailure:
continue
if not players.get(item.killer_username):
players[item.killer_username] = [1, [item]]
else:
players[item.killer_username] = [players[item.killer_username][0]+1, players[item.killer_username][1] + [item]]
await kill_logs_channel.send(embed=discord.Embed(title="Kill Log", color=BLANK_COLOR, description=f"[{item.killer_username}](https://roblox.com/users/{item.killer_user_id}/profile) killed [{item.killed_username}](https://roblox.com/users/{item.killed_user_id}/profile) • <t:{int(item.timestamp)}:T>"))

sorted_kill_logs = sorted(kill_logs, key=lambda x: x.timestamp, reverse=False)
sorted_player_logs = sorted(player_logs, key=lambda x: x.timestamp, reverse=False)
players = {}
current_timestamp = int(datetime.datetime.now(tz=pytz.UTC).timestamp())

channel = ((settings or {}).get('ERLC', {}) or {}).get('rdm_channel', 0)
try:
channel = await (await bot.fetch_guild(guild.id)).fetch_channel(channel)
except discord.HTTPException:
channel = None
if kill_logs_channel is not None:
for log_item in sorted_kill_logs:
if (current_timestamp - log_item.timestamp) > 120:
continue
if not players.get(log_item.killer_username):
players[log_item.killer_username] = [1, [log_item]]
else:
players[log_item.killer_username] = [players[log_item.killer_username][0]+1, players[log_item.killer_username][1] + [log_item]]
await kill_logs_channel.send(embed=discord.Embed(title="Kill Log", color=BLANK_COLOR, description=f"[{log_item.killer_username}](https://roblox.com/users/{log_item.killer_user_id}/profile) killed [{log_item.killed_username}](https://roblox.com/users/{log_item.killed_user_id}/profile) • <t:{int(log_item.timestamp)}:T>"))

if channel:
for username, value in players.items():
count = value[0]
items = value[1]
if count > 3:
roblox_player = await bot.roblox.get_user_by_username(username)
thumbnails = await bot.roblox.thumbnails.get_user_avatar_thumbnails([roblox_player], size=(420, 420))
thumbnail = thumbnails[0].image_url
pings = []
pings = [((guild.get_role(role_id)).mention) if guild.get_role(role_id) else None for role_id in (settings or {}).get('ERLC', {}).get('rdm_mentionables', [])]
pings = list(filter(lambda x: x is not None, pings))

await channel.send(
rdm_channel = ((settings or {}).get('ERLC', {}) or {}).get('rdm_channel', 0)
try:
channel = await guild.fetch_channel(rdm_channel)
except discord.HTTPException:
channel = None

if channel:
for username, value in players.items():
count = value[0]
items = value[1]
if count > 3:
roblox_player = await bot.roblox.get_user_by_username(username)
thumbnails = await bot.roblox.thumbnails.get_user_avatar_thumbnails([roblox_player], size=(420, 420))
thumbnail = thumbnails[0].image_url
pings = [((guild.get_role(role_id)).mention) if guild.get_role(role_id) else None for role_id in (settings or {}).get('ERLC', {}).get('rdm_mentionables', [])]
pings = list(filter(lambda x: x is not None, pings))

await channel.send(
', '.join(pings) if pings not in [[], None] else '',
embed=discord.Embed(
title="<:security:1169804198741823538> RDM Detected",
color=BLANK_COLOR
).add_field(
name="User Information",
value=(
f"> **Username:** {roblox_player.name}\n"
f"> **User ID:** {roblox_player.id}\n"
f"> **Profile Link:** [Click here](https://roblox.com/users/{roblox_player.id}/profile)\n"
f"> **Account Created:** <t:{int(roblox_player.created.timestamp())}>"
),
value=f"> **Username:** {roblox_player.name}\n> **User ID:** {roblox_player.id}\n> **Profile Link:** [Click here](https://roblox.com/users/{roblox_player.id}/profile)\n> **Account Created:** <t:{int(roblox_player.created.timestamp())}>",
inline=False
).add_field(
name="Abuse Information",
value=(
f"> **Type:** Mass RDM\n"
f"> **Individuals Affected [{count}]:** {', '.join([f'[{i.killed_username}](https://roblox.com/users/{i.killed_user_id}/profile)' for i in items])}\n"
f"> **At:** <t:{int(items[0].timestamp)}>"
),
value=f"> **Type:** Mass RDM\n> **Individuals Affected [{count}]:** {', '.join([f'[{i.killed_username}](https://roblox.com/users/{i.killed_user_id}/profile)' for i in items])}\n> **At:** <t:{int(items[0].timestamp)}>",
inline=False
).set_thumbnail(
url=thumbnail
),
).set_thumbnail(url=thumbnail),
allowed_mentions=discord.AllowedMentions(
everyone=True,
users=True,
Expand All @@ -972,79 +961,74 @@ async def iterate_prc_logs():
),
view=RDMActions(bot)
)
staff_roles = []
if settings["staff_management"].get("role"):
if isinstance(settings["staff_management"]["role"], int):
staff_roles.append(settings["staff_management"]["role"])
elif isinstance(settings["staff_management"]["role"], list):
for role in settings["staff_management"]["role"]:
staff_roles.append(role)

if settings["staff_management"].get("management_role"):
if isinstance(settings["staff_management"]["management_role"], int):
staff_roles.append(settings["staff_management"]["management_role"])
elif isinstance(settings["staff_management"]["management_role"], list):
for role in settings["staff_management"]["management_role"]:
staff_roles.append(role)

staff_roles = []
if settings.get("staff_management", {}).get("role"):
if isinstance(settings["staff_management"]["role"], int):
staff_roles.append(settings["staff_management"]["role"])
elif isinstance(settings["staff_management"]["role"], list):
staff_roles.extend(settings["staff_management"]["role"])

if settings.get("staff_management", {}).get("management_role"):
if isinstance(settings["staff_management"]["management_role"], int):
staff_roles.append(settings["staff_management"]["management_role"])
elif isinstance(settings["staff_management"]["management_role"], list):
staff_roles.extend(settings["staff_management"]["management_role"])

await guild.chunk()
staff_roles = [guild.get_role(role) for role in staff_roles if guild.get_role(role) is not None]

added_staff = []
perm_staff = [m for m in guild.members if (m.guild_permissions.manage_messages or m.guild_permissions.manage_guild or m.guild_permissions.administrator) and not m.bot]

for role in staff_roles:
added_staff.extend([m for m in role.members if not m.bot and m not in added_staff])

added_staff.extend([m for m in perm_staff if m not in added_staff])

automatic_shifts_enabled = ((settings.get('ERLC', {}) or {}).get('automatic_shifts', {}) or {}).get('enabled', False)
automatic_shift_type = ((settings.get('ERLC', {}) or {}).get('automatic_shifts', {}) or {}).get('shift_type', '')

roblox_to_discord = {}
for staff_member in added_staff:
oauth_data = await bot.oauth2_users.db.find_one({"discord_id": staff_member.id})
if oauth_data:
roblox_to_discord[int(oauth_data.get("roblox_id", 0))] = staff_member

if player_logs_channel is not None:
for log_item in sorted_player_logs:
if (current_timestamp - log_item.timestamp) > 120:
continue

await guild.chunk()
staff_roles = [guild.get_role(role) for role in staff_roles]
added_staff = []

for role in staff_roles.copy():
if role is None:
staff_roles.remove(role)

perm_staff = list(
filter(
lambda m: (
m.guild_permissions.manage_messages
or m.guild_permissions.manage_guild
or m.guild_permissions.administrator
if log_item.user_id in roblox_to_discord:
if automatic_shifts_enabled:
discord_user = roblox_to_discord[log_item.user_id]
consent_item = await bot.consent.find_by_id(discord_user.id)

if (consent_item or {}).get('auto_shifts', True):
shift = await bot.shift_management.get_current_shift(discord_user, guild.id)

if log_item.type == 'join' and not shift:
await bot.shift_management.add_shift_by_user(discord_user, automatic_shift_type, [], guild.id, timestamp=log_item.timestamp)
elif log_item.type != 'join' and shift:
await bot.shift_management.end_shift(shift['_id'], guild.id, timestamp=log_item.timestamp)

await player_logs_channel.send(
embed=discord.Embed(
title=f"Player {'Join' if log_item.type == 'join' else 'Leave'} Log",
description=f"[{log_item.username}](https://roblox.com/users/{log_item.user_id}/profile) {'joined the server' if log_item.type == 'join' else 'left the server'} • <t:{int(log_item.timestamp)}:T>",
color=GREEN_COLOR if log_item.type == 'join' else RED_COLOR
)
and not m.bot,
guild.members
)
)

for role in staff_roles:
for member in role.members:
if not member.bot and member not in added_staff:
added_staff.append(member)

for member in perm_staff:
if member not in added_staff:
added_staff.append(member)

automatic_shifts_enabled = ((settings.get('ERLC', {}) or {}).get('automatic_shifts', {}) or {}).get('enabled', False)
automatic_shift_type = ((settings.get('ERLC', {}) or {}).get('automatic_shifts', {}) or {}).get('shift_type', '')
roblox_to_discord = {}
for item in perm_staff:
roblox_to_discord[int(((await bot.oauth2_users.db.find_one({"discord_id": item.id})) or {}).get("roblox_id", 0))] = item

if player_logs_channel is not None:
for item in sorted_player_logs:
if (current_timestamp - item.timestamp) > 120:
continue
if item.user_id in roblox_to_discord.keys():
if automatic_shifts_enabled:
consent_item = await bot.consent.find_by_id(roblox_to_discord[item.user_id].id)
if (consent_item or {}).get('auto_shifts', True) is True:
shift = await bot.shift_management.get_current_shift(roblox_to_discord[item.user_id], guild.id)
if item.type == 'join':
if not shift:
await bot.shift_management.add_shift_by_user(roblox_to_discord[item.user_id], automatic_shift_type, [], guild.id, timestamp=item.timestamp)
else:
if shift:
await bot.shift_management.end_shift(shift['_id'], guild.id, timestamp=item.timestamp)

await player_logs_channel.send(
embed=discord.Embed(
title=f"Player {'Join' if item.type == 'join' else 'Leave'} Log",
description=f"[{item.username}](https://roblox.com/users/{item.user_id}/profile) {'joined the server' if item.type == 'join' else 'left the server'} • <t:{int(item.timestamp)}:T>",
color=GREEN_COLOR if item.type == 'join' else RED_COLOR
)
)
except discord.HTTPException:
continue
except Exception as e:
print(f"Error processing guild {item['_id']}: {e}")
continue

except Exception as e:
print(f"Error in iterate_prc_logs: {e}")

@iterate_prc_logs.before_loop
async def anti_fetch_measure():
Expand Down

0 comments on commit 82fab78

Please sign in to comment.