-
Notifications
You must be signed in to change notification settings - Fork 0
Bluetooth #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Raphsinai
wants to merge
6
commits into
master
Choose a base branch
from
bluetooth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Bluetooth #102
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c9e5c5
added override status to whois
e809dff
fix missing +
94f5142
added is_trained and individual mac_addr per person
53328ae
added add mac address command
51df84b
added trained_member present to commands
112d781
update versions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [project] | ||
| name = "ICRS Discord Bot" | ||
| version = "1.0.6" | ||
| version = "1.0.7" | ||
| description = "Main ICRS Discord Bot." | ||
| requires-python = ">=3.8" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| __all__ = [ | ||
| "add_mac_address", | ||
| "trained_member_present" | ||
| ] | ||
|
|
||
| import logging | ||
| import discord | ||
| import requests | ||
| from datetime import datetime | ||
|
|
||
| from src.utils import SERVER_IP | ||
| import src.utils as utils | ||
|
|
||
| async def add_mac_address( | ||
| interaction: discord.Interaction, | ||
| user: str | discord.User | discord.Member, | ||
| mac_address: str, | ||
| ): | ||
| logging.info(f"Add MAC Address: {user}") | ||
|
|
||
| if isinstance(user, str): | ||
| if not utils.is_shortcode(user): | ||
| logging.info(f"add MAC Address shortcode invalid: {user}") | ||
| return await interaction.response.send_message( | ||
| embed=utils.invalid_shortcode(), ephemeral=True | ||
| ) | ||
|
|
||
| discord_id = utils.get_discord_from_shortcode(user) | ||
| shortcode = user | ||
|
|
||
| else: | ||
| discord_id = str(user.id) | ||
| logging.info(f"add MAC address Discord ID: {discord_id}") | ||
|
|
||
| shortcode = utils.get_shortcode_from_discord(discord_id) | ||
|
|
||
| if not shortcode: | ||
| return await interaction.response.send_message( | ||
| embed=utils.error_msg( | ||
| f"Couldn't get short code for <@{discord_id}>", | ||
| "Add mac address Warning"), | ||
| ephemeral=True) | ||
|
|
||
| if not utils.is_mac_address(mac_address): | ||
| logging.info("invalid mac address format") | ||
| return await interaction.response.send_message( | ||
| embed=utils.error_msg( | ||
| f"Invalid mac address {mac_address}", | ||
| "Mac address invalid warning" | ||
| ), | ||
| ephemeral=True | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| response = requests.post( | ||
| SERVER_IP + "/member/mac_addresses/add", | ||
| params={ | ||
| "shortcode": shortcode, | ||
| "mac_address": mac_address | ||
| } | ||
| ) | ||
|
|
||
| if response.status_code == 200: | ||
| embed = discord.Embed( | ||
| title="Add mac address", | ||
| description=f"Successfully added {mac_address} to {shortcode}" | ||
| ) | ||
| return await interaction.response.send_message( | ||
| embed=embed, | ||
| ephemeral=True, | ||
| ) | ||
|
|
||
| return await interaction.response.send_message( | ||
| embed=utils.error_msg( | ||
| "Failed to add to db", | ||
| "MAC Address not added" | ||
| ), | ||
| ephemeral=True, | ||
| ) | ||
|
|
||
| async def trained_member_present( | ||
| interaction: discord.Interaction, | ||
| timestamp: str, | ||
| interval: int | ||
| ): | ||
| try: | ||
| timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M") | ||
| except: | ||
| return await interaction.response.send_message( | ||
| embed=utils.error_msg( | ||
| "Format should be YYYY-MM-DD HH:MM", | ||
| "Invalid timestamp format" | ||
| ), | ||
| ephemeral=True | ||
| ) | ||
| response = requests.get( | ||
| SERVER_IP + "/access/trained_member_present", | ||
| params= { | ||
| "timestamp": str(timestamp), | ||
| "interval": interval | ||
| } | ||
| ) | ||
|
|
||
| if len(response.json()) > 0: | ||
| embed = discord.Embed( | ||
| title="Committee Present", | ||
| description="\n".join(response.json()) | ||
| ) | ||
| return await interaction.response.send_message( | ||
| embed=embed, | ||
| ephemeral=True | ||
| ) | ||
| else: | ||
| return await interaction.response.send_message( | ||
| embed=utils.error_msg( | ||
| "No trained members present in that time interval", | ||
| "NO TRAINED MEMBER" | ||
| ), | ||
| ephemeral=True | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| "get_shortcode_from_discord", | ||
| "get_perms_from_shortcode", | ||
| "MemberPermissions", | ||
| "get_mac_addresses_from_shortcode" | ||
| ] | ||
|
|
||
| """ | ||
|
|
@@ -97,6 +98,7 @@ class MemberPermissions: | |
| card_id: str = "" | ||
| resin: bool = False | ||
| printer_override: bool = False | ||
| is_lab_trained: bool = False | ||
|
|
||
|
|
||
| def get_perms_from_shortcode(shortcode: str) -> MemberPermissions: | ||
|
|
@@ -136,6 +138,19 @@ def get_stats_from_discord(discord_id: str): | |
|
|
||
| return Stats() | ||
|
|
||
| def get_mac_addresses_from_shortcode(shortcode: str): | ||
| res = requests.get( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generally best to return response obj directly - allows you to handle different status codes differently in different scenarios |
||
| SERVER_IP + "/member/mac_addresses", | ||
| params={ | ||
| "shortcode": shortcode | ||
| }, | ||
| auth=BASIC_AUTH | ||
| ) | ||
|
|
||
| if res.status_code == 200: | ||
| return res.json() | ||
|
|
||
| return None | ||
|
|
||
| @dataclass | ||
| class Print: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| "validate_card_uid", | ||
| "validate_shortcode", | ||
| "verified_member", | ||
| "is_mac_address" | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -25,6 +26,7 @@ | |
| SHORTCODE_REGEX = r'^[a-z]{2,3}\d{2,5}$' | ||
| UID_REGEX = r'^[0-9A-F]{8,14}$' | ||
| DISCORD_ID_REGEX = r'^<@[0-9]{18,19}>$' | ||
| MAC_ADDR_REGEX = r'^([0-9A-F]{2}:){5}[0-9A-F]{2}' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
|
|
||
|
|
||
| def is_shortcode(message: str) -> bool: | ||
|
|
@@ -63,6 +65,10 @@ def is_not_committee(author: discord.User | discord.Member): | |
| def is_member(author: discord.User | discord.Member): | ||
| return "verified member" in [y.name.lower() for y in author.roles] | ||
|
|
||
| def is_mac_address(mac_addr : str): | ||
| mac_addr = mac_addr.upper() | ||
| found = re.findall(MAC_ADDR_REGEX, mac_addr) | ||
| return any(found) | ||
|
|
||
| def committee_command(function: Callable): | ||
| @functools.wraps(function) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed? might be nicer to have a status page + webhook like how the printer streaming stuff is done?