Skip to content

Commit

Permalink
Formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
timpehoerig committed Jul 30, 2023
1 parent d3b77c6 commit e4d5a96
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 72 deletions.
7 changes: 5 additions & 2 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .env import env
from .bot import state


# openid connect client for laurel
laurel = OAuth()
laurel.register(
Expand Down Expand Up @@ -39,7 +40,8 @@ async def callback(request: Request):
async def logout(request: Request):
# logout locally
request.session.clear()
# logging out by logging out of all laurel services and redirect to login here
# logging out by logging out of all laurel services and
# redirect to login here
return RedirectResponse(
env.laurel_auth_url
+ "auth/logout?redirect="
Expand All @@ -53,7 +55,8 @@ async def token(request: Request):
return RedirectResponse(request.url_for("auth_login"))
token = str(uuid4())

# stores authorization token along user information in thread shared state for bot to access
# stores authorization token along user information in
# thread shared state for bot to access
state[token] = user

# key is valid for 5 minutes
Expand Down
94 changes: 66 additions & 28 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
account_embed,
ChannelRequestInput,
channels_request_button,
channel_request_send,
channel_view,
channel_embed,
ChannelRequestAcceptInput,
Expand Down Expand Up @@ -75,7 +74,7 @@
# thread shared state between fastapi and discord bot
state = Manager().dict()

## Bot ##################################################################################
# Bot #########################################################################


class Bot(Client):
Expand Down Expand Up @@ -139,7 +138,10 @@ async def on_update(input: AccountTokenInput, interaction: Interaction):
# opens token modal for sync
async def account_token_modal(interaction: Interaction):
account_token_input = AccountTokenInput()
account_token_input.on_submit = MethodType(on_update, account_token_input)
account_token_input.on_submit = MethodType(
on_update,
account_token_input
)
await interaction.response.send_modal(account_token_input)

account_update_button.callback = account_token_modal
Expand All @@ -151,7 +153,10 @@ async def on_rename(input: AccountNameInput, interaction: Interaction):
# opens name modal for new name
async def account_name_modal(interaction: Interaction):
account_name_input = AccountNameInput()
account_name_input.on_submit = MethodType(on_rename, account_name_input)
account_name_input.on_submit = MethodType(
on_rename,
account_name_input
)
await interaction.response.send_modal(account_name_input)

account_name_button.callback = account_name_modal
Expand All @@ -160,7 +165,9 @@ async def account_name_modal(interaction: Interaction):

async def channels(self):
# get channels channel
message = await last_or_new_channel_message(channel_by_name(self, "channels"))
message = await last_or_new_channel_message(
channel_by_name(self, "channels")
)

# opens the request modal
async def channel_request_modal(interaction: Interaction):
Expand All @@ -176,7 +183,9 @@ async def offtopic_channel_request_modal(interaction: Interaction):
offtopic_channel_request_input.on_submit = MethodType(
on_request, offtopic_channel_request_input
)
await interaction.response.send_modal(offtopic_channel_request_input)
await interaction.response.send_modal(
offtopic_channel_request_input
)

# sends the request to admin channel
async def on_request(
Expand All @@ -192,11 +201,17 @@ async def on_request(
channels_request_button.callback = channel_request_modal
offtopic_request_button.callback = offtopic_channel_request_modal

await message.edit(content="", embed=channel_embed, view=channel_view())
await message.edit(
content="",
embed=channel_embed,
view=channel_view()
)

async def support(self):
# get support channel
message = await last_or_new_channel_message(channel_by_name(self, "support"))
message = await last_or_new_channel_message(
channel_by_name(self, "support")
)

# opens the request modal
async def support_request_modal(interaction: Interaction):
Expand All @@ -215,10 +230,16 @@ async def on_request(

support_request_button.callback = support_request_modal

await message.edit(content="", embed=support_embed, view=support_view())

await message.edit(content="",
embed=support_embed,
view=support_view()
)

async def voice(self, member: Member, before: VoiceState, after: VoiceState):
async def voice(self,
member: Member,
before: VoiceState,
after: VoiceState
):
create = utils.get(member.guild.voice_channels, name="create")
category = utils.get(member.guild.categories, name="voice")
category_support = utils.get(member.guild.categories, name="support")
Expand Down Expand Up @@ -269,18 +290,22 @@ async def voice(self, member: Member, before: VoiceState, after: VoiceState):
await member.move_to(channel)
if (
before.channel != after.channel
and (before.channel.category == category or before.channel.category == category_support)
and (before.channel.category == category or
before.channel.category == category_support)
and not before.channel.members
and before.channel.id != int(env.create_voice_channel_id)
):
# delete empty channels except #create
await before.channel.delete()


## Functionality ########################################################################
# Functionality ###############################################################


async def authorize_token(token: str, interaction: Interaction, message=True) -> bool:
async def authorize_token(token: str,
interaction: Interaction,
message=True
) -> bool:
# token is valid
if token in state:
# get user information
Expand Down Expand Up @@ -316,7 +341,9 @@ async def authorize_token(token: str, interaction: Interaction, message=True) ->
# token is invalid
else:
# send failure message
await send_decaying_response_message(interaction.response, auth_login_failure)
await send_decaying_response_message(interaction.response,
auth_login_failure
)
return False


Expand All @@ -339,7 +366,9 @@ async def disconnect_account(interaction: Interaction, message=True) -> bool:
pass

if message:
await send_decaying_response_message(interaction.response, auth_logout_success)
await send_decaying_response_message(interaction.response,
auth_logout_success
)
return True


Expand Down Expand Up @@ -399,14 +428,18 @@ async def on_accept(
category=utils.get(
accept_interaction.user.guild.categories, name="channels"
),
topic=f"**[{channel_request_accept_input.kind_of_event.value}]** {channel_request_accept_input.name_of_lecture.value}",
topic=f"""**[{
channel_request_accept_input.kind_of_event.value}]** {
channel_request_accept_input.name_of_lecture.value}""",
)

await request_interaction.user.send(
channel_request_accepted(channel_request_accept_input.name_of_channel.value)
channel_request_accepted(
channel_request_accept_input.name_of_channel.value)
)
embed = accept_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Accepted by {accept_interaction.user.nick}")
embed = embed.set_footer(
text=f"Accepted by {accept_interaction.user.nick}")
await accept_interaction.message.edit(embed=embed, view=None)
await send_decaying_response_message(
accept_interaction.response, accept_channel_send
Expand All @@ -420,7 +453,8 @@ async def on_decline(
await request_interaction.user.send(decline_message)

embed = decline_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Declined by {decline_interaction.user.nick}")
embed = embed.set_footer(
text=f"Declined by {decline_interaction.user.nick}")
await decline_interaction.message.edit(embed=embed, view=None)
await send_decaying_response_message(
decline_interaction.response, decline_channel_send
Expand Down Expand Up @@ -486,7 +520,8 @@ async def on_accept(
)
)
embed = accept_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Accepted by {accept_interaction.user.nick}")
embed = embed.set_footer(
text=f"Accepted by {accept_interaction.user.nick}")
await accept_interaction.message.edit(embed=embed, view=None)
await send_decaying_response_message(
accept_interaction.response, accept_offtopic_channel_send
Expand All @@ -500,7 +535,8 @@ async def on_decline(
await request_interaction.user.send(decline_message)

embed = decline_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Declined by {decline_interaction.user.nick}")
embed = embed.set_footer(
text=f"Declined by {decline_interaction.user.nick}")
await decline_interaction.message.edit(embed=embed, view=None)
await send_decaying_response_message(
decline_interaction.response, decline_offtopic_channel_send
Expand All @@ -517,7 +553,7 @@ async def on_decline(
request_interaction.response, accept_channel_request_send
)

## Support ##############################################################################
# Support ####################################################################


async def forward_support_request(
Expand Down Expand Up @@ -566,7 +602,8 @@ async def on_accept(
)
)
embed = accept_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Accepted by {accept_interaction.user.nick}")
embed = embed.set_footer(
text=f"Accepted by {accept_interaction.user.nick}")
invite = await channel.create_invite(max_age=120, max_uses=2)
await request_interaction.user.send(
support_request_accepted(request_interaction.user.nick, invite)
Expand All @@ -584,7 +621,8 @@ async def on_decline(
await request_interaction.user.send(decline_message)

embed = decline_interaction.message.embeds[0]
embed = embed.set_footer(text=f"Declined by {decline_interaction.user.nick}")
embed = embed.set_footer(
text=f"Declined by {decline_interaction.user.nick}")
await decline_interaction.message.edit(embed=embed, view=None)
await send_decaying_response_message(
decline_interaction.response, decline_support_send
Expand All @@ -601,15 +639,15 @@ async def on_decline(
request_interaction.response, accept_support_request_send
)

## Utils ################################################################################


# Utils #######################################################################
async def get_or_create_role(guild: Any, name: str) -> Role:
role = utils.get(guild.roles, name=name)
if role is None:
role = await guild.create_role(
name=name,
colour=Colour.from_rgb(randint(0, 255), randint(0, 255), randint(0, 255)),
colour=Colour.from_rgb(
randint(0, 255), randint(0, 255), randint(0, 255)),
hoist=True,
)
await role.edit(position=2)
Expand Down
2 changes: 0 additions & 2 deletions src/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from multiprocessing import Manager

# maps degree identities returned by laurel (i.e. ldap) to actual names
degrees = {
"05-968": "Ältere deutsche Literatur und Sprache",
Expand Down
5 changes: 3 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

app.add_middleware(SessionMiddleware, secret_key=env.secret_key, max_age=94608000)
app.add_middleware(SessionMiddleware,
secret_key=env.secret_key, max_age=94608000)

# Events

Expand Down Expand Up @@ -58,7 +59,7 @@ async def auth_callback(request: Request):
return await callback(request)


## Discord Bot ##########################################################################
# Discord Bot #################################################################

bot = Bot()

Expand Down
Loading

0 comments on commit e4d5a96

Please sign in to comment.