Skip to content

Commit

Permalink
Fixing some issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikey committed May 4, 2024
1 parent 9fbc708 commit 7e82aea
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 95 deletions.
13 changes: 11 additions & 2 deletions cogs/ActivityMonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytz
from discord import app_commands
from discord.ext import commands

import typing
from menus import CustomExecutionButton
from utils.constants import BLANK_COLOR, GREEN_COLOR, RED_COLOR
from erm import is_management
Expand Down Expand Up @@ -33,7 +33,8 @@ async def activity(self, ctx: commands.Context):
)
@is_management()
@require_settings()
async def activity_show(self, ctx: commands.Context, duration: str):
async def activity_show(self, ctx: commands.Context, duration: str, selected_role: typing.Optional[discord.Role]):

settings = await self.bot.settings.find_by_id(ctx.guild.id)
if not settings.get('shift_management').get('enabled'):
return await ctx.send(
Expand Down Expand Up @@ -76,6 +77,9 @@ async def activity_show(self, ctx: commands.Context, duration: str):
if not member:
continue
roles = member.roles
if selected_role is not None:
if selected_role not in roles:
continue
sorted_roles = sorted(member.roles, key=lambda x: x.position)
selected_quota = 0
for role in sorted_roles:
Expand All @@ -92,6 +96,11 @@ async def activity_show(self, ctx: commands.Context, duration: str):
else:
all_staff[shift_document['UserID']][0] += shift_time

if selected_role is not None:
for item in selected_role.members:
if item.id not in all_staff.keys():
all_staff[item.id] = [0, settings.get('shift_management').get('quota', 0)]

sorted_all_staff = sorted(all_staff.items(), key= lambda x: x[1], reverse=True)
# print(sorted_all_staff)
sorted_staff = dict(zip([item[0] for item in sorted_all_staff], [item[1] for item in sorted_all_staff]))
Expand Down
2 changes: 1 addition & 1 deletion cogs/OAuth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def link_roblox(self, ctx: commands.Context):
description="**To link your account with ERM, click the button below.**\nIf you encounter an error, please contact ERM Support by running `/support`.",
color=BLANK_COLOR
),
"view": LinkView(label="Link Roblox", url=f"https://authorize.roblox.com/?client_id=9037343251472961537&response_type=code&redirect_uri=https://verify.ermbot.xyz/auth&scope=openid+profile&state={ctx.author.id}")
"view": LinkView(label="Link Roblox", url=f"https://authorize.roblox.com/?client_id=5489705006553717980&response_type=code&redirect_uri=https://verify.ermbot.xyz/auth&scope=openid+profile&state={ctx.author.id}")
}

await self.bot.pending_oauth2.db.insert_one({
Expand Down
180 changes: 88 additions & 92 deletions erm.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,9 @@ async def iterate_prc_logs():
continue


# if not kill_logs_channel and not player_logs_channel:
# 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)
Expand Down Expand Up @@ -660,78 +661,75 @@ async def iterate_prc_logs():
players = {}


for item in sorted_kill_logs:
if (current_timestamp - item.timestamp) > 75:
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]]
if kill_logs_channel is not None:
for item in sorted_kill_logs:
if (current_timestamp - item.timestamp) > 75:
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(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>")


if kill_logs_channel is not None:
await kill_logs_channel.send(
embed=discord.Embed(
title="Kill Log",
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>",
color=BLANK_COLOR
)
)


# Check for kill logs amount
for username, value in players.items():
count = value[0]
items = value[1]
if count > 3:
settings = await bot.settings.find_by_id(guild.id)
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 channel:
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(
', '.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())}>"
settings = await bot.settings.find_by_id(guild.id)
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 channel:
# Check for kill logs amount
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(
', '.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())}>"
),
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)}>"
),
inline=False
).set_thumbnail(
url=thumbnail
),
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)}>"
allowed_mentions=discord.AllowedMentions(
everyone=True,
users=True,
roles=True,
replied_user=True,
),
inline=False
).set_thumbnail(
url=thumbnail
),
allowed_mentions=discord.AllowedMentions(
everyone=True,
users=True,
roles=True,
replied_user=True,
),
view=RDMActions(bot)
)
view=RDMActions(bot)
)

staff_roles = []
settings = await bot.settings.find_by_id(guild.id)
Expand Down Expand Up @@ -784,32 +782,30 @@ async def iterate_prc_logs():
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

for item in sorted_player_logs:
if (current_timestamp - item.timestamp) > 75:
continue
if player_logs_channel is not None:
for item in sorted_player_logs:
if (current_timestamp - item.timestamp) > 75:
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)

if not player_logs_channel:
break

await player_logs_channel.send(
embed=discord.Embed(
title="Player Join/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
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="Player Join/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 Exception as error:
channel = await bot.fetch_channel(1213523576603410452)
await channel.send(content=f"[2] {str(error)=}")
Expand Down
126 changes: 126 additions & 0 deletions menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8709,3 +8709,129 @@ async def finish(self, interaction: discord.Interaction, _: discord.Button):
await self.bot.punishments.upsert(self.dataset)
self.cancelled = False
self.stop()


class CompleteVerification(discord.ui.View):
def __init__(self, user: discord.Member):
self.user = user
super().__init__(timeout=600.0)

@discord.ui.button(label="I have changed my description", style=discord.ButtonStyle.success)
async def changed(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.user:
return await interaction.response.send_message(
embed=discord.Embed(
title="Not Permitted",
description="You are not permitted to utilise these buttons.",
color=BLANK_COLOR
),
ephemeral=True
)

await interaction.response.defer(thinking=False, ephemeral=False)
self.stop()


class AccountLinkingMenu(discord.ui.View):
def __init__(self, user: discord.Member):
self.user = user
self.mode = "OAuth2"
self.associated = None

super().__init__(timeout=600.0)
self.add_item(discord.ui.Button(label="Link Roblox", url="https://authorize.roblox.com/?client_id=5489705006553717980&response_type=code&redirect_uri=https://verify.ermbot.xyz/auth&scope=openid+profile&state={ctx.author.id}"))

@discord.ui.button(label="Code Verification")
async def code_verification(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.user:
await interaction.response.send_message(
embed=discord.Embed(
title="Not Authorized",
description="You are not authorized to utilise this menu.",
color=BLANK_COLOR
),
ephemeral=True
)
return

msg = interaction.message
await interaction.response.send_modal(
(modal := CustomModal(
"Legacy Code Verification",
[
(
"username",
(
discord.ui.TextInput(
label="Roblox Username",
placeholder="Roblox Username (e.g. i_iMikey)",
required=True,
)
),
)
],
))
)
timeout = await modal.wait()
if timeout:
return
if not modal.username.value:
return

try:
user = await bot.roblox.get_user_by_username(modal.username.value)
except:
return

available_string_subsets = [
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Zero"
]

full_string = f"ERM {' '.join([random.choice(available_string_subsets) for _ in range(6)])}"

await msg.edit(
embed=discord.Embed(
title="Legacy Code Verification",
description=f"To utilise this verification for **{user.name}**, put the following code in your Roblox account description.\n`{full_string}`",
color=BLANK_COLOR
),
view=(view := CompleteVerification(interaction.user))
)
timeout = await view.wait()
if timeout:
return

try:
new_user = await bot.roblox.get_user_by_username(modal.username.value)
except:
return

if full_string.lower() in new_user.description.lower():
await bot.pending_oauth2.db.delete_one({"discord_id": interaction.user.id})
await bot.oauth2_users.db.insert_one({"roblox_id": new_user.id, "discord_id": interaction.user.id})

self.mode = "Code"
self.username = new_user.name
await interaction.message.edit(
embed=discord.Embed(
title="<:check:1163142000271429662> Successfully Linked",
description=f"You have been successfully linked to **{new_user.name}**.",
color=GREEN_COLOR
)
)
else:
await interaction.message.edit(
embed=discord.Embed(
title="Not Linked",
description="You did not include the code in your description. Please try again later."
)
)

0 comments on commit 7e82aea

Please sign in to comment.