Skip to content

Commit

Permalink
fixed bot crashing if trying to send message without having the permi…
Browse files Browse the repository at this point in the history
…ssions; updated exception messages; updated readme
  • Loading branch information
9-FS committed Nov 2, 2023
1 parent a1747af commit 1014095
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "CC BY-NC-SA 4.0"
name = "x" # can't leave empty because of bug with `poetry install` from poetry.lock file
readme = "readme.md"
repository = "https://github.com/9-FS/2022-01-26-Metric-METAR-for-Discord"
version = "1.1.1"
version = "1.1.2"

[tool.poetry.dependencies]
discord = "^2.3.2"
Expand Down
6 changes: 2 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This discord bot provides realtime meteorological aerodrome reports (METAR) and
METAR and TAF will be provided:

- **primarily in a more readable or even completely decoded way in which all units are stated and converted to SI**. This custom standard strives to be a more modern and better compromise between being able to quickly brief the relevant information and enhancing its readability.
Important: While the code has been proven reliable over 1 year of daily private use, **it is not certified to be used in real flight operations.**
Important: While the code has been proven reliable over years of daily private use, **it is not certified to be used in real flight operations.** Please [open an issue on Github](https://github.com/9-FS/2022-01-26-Metric-METAR-for-Discord/issues) if you find any bugs or have any suggestions.
- secondarily in their original format which is directly forwarded from [NOAA](https://www.noaa.gov/)[^1], mainly for cross-checking

Once a station is requested, 2 things happen:
Expand Down Expand Up @@ -132,7 +132,7 @@ This method is recommended if you just want to use the bot out of the box and se
1. Make sure to have a text channel named `bots`, `botspam`, or `metar` on your server. **The bot will only react in these specific channels.**

If you need another channel name the bot reacts to, either contact me or follow the guideline in [chapter 3.](#3-how-to-install-by-setting-up-your-own-bot) to set up your own bot instance.
It is recommended to only have 1 of the beforementioned channels on your server.
It is recommended to only have 1 of the beforementioned channels on your server, because I have not tested what happens if there are multiple.

## 3. How to Install by Setting Up Your Own Bot

Expand Down Expand Up @@ -163,8 +163,6 @@ Once set up, using the bot is dead-easy. Write one of the following commands int

These are the features I am thinking about implementing in the future. Their stages vary widely from "hmm might be nice to have that" to "I have it almost done, but there is this 1 problem I can't solve.". So don't get your hopes up too high, but I'm very much open for feature requests and discussions.

- aerodrome information command
- download VFR charts command
- switch from open source database https://ourairports.com/data/ to official database, currently no idea how to get free access
- customisable weather minimums for each server

Expand Down
Binary file modified readme.pdf
Binary file not shown.
18 changes: 9 additions & 9 deletions src/init_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def init_DB(DB_type: DB_Type, DB: pandas.DataFrame, now_DT: dt.datetime, DOWNLOA
logging.warning(f"Loading {DB_type.name} database failed, because \"{DB_TODAY_FILEPATH}\" does not exist.")
except pandas.errors.EmptyDataError:
logging.warning(f"Loaded {DB_type.name} database, but it is empty.")
except pandas.errors.ParserError:
logging.warning(f"Loaded {DB_type.name} database, but parsing failed.")
except pandas.errors.ParserError as e:
logging.warning(f"Loaded {DB_type.name} database, but parsing failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
else: # if loading database successful:
logging.info(f"\rLoaded {DB_type.name} database from \"{DB_TODAY_FILEPATH}\".")
return DB
Expand All @@ -44,21 +44,21 @@ def init_DB(DB_type: DB_Type, DB: pandas.DataFrame, now_DT: dt.datetime, DOWNLOA
DB=requests.get(DB_type.value, timeout=DOWNLOAD_TIMEOUT).text # type:ignore
DB=pandas.read_csv(io.StringIO(DB)) # type:ignore
except requests.ConnectionError as e:
logging.warning(f"Downloading {DB_type.name} database failed with {KFSfstr.full_class_name(e)}.")
logging.warning(f"Downloading {DB_type.name} database failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
except requests.ReadTimeout:
logging.warning(f"Downloading {DB_type.name} database timed out after 10s.")
except pandas.errors.EmptyDataError:
logging.warning(f"Downloaded {DB_type.name} database is empty.")
except pandas.errors.ParserError:
logging.warning(f"Downloaded {DB_type.name} database, but parsing failed.")
except pandas.errors.ParserError as e:
logging.warning(f"Downloaded {DB_type.name} database, but parsing failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
else: # downloading successful, save
logging.info(f"\rDownloaded and formatted {DB_type.name} database.")

logging.info(f"Saving {DB_type.name} database in \"{DB_TODAY_FILEPATH}\"...") # save downloaded database
try: # save database
DB.to_csv(DB_TODAY_FILEPATH, index=False, mode="wt")
except OSError:
logging.warning(f"Saving {DB_type.name} database in \"{DB_TODAY_FILEPATH}\" failed.")
except OSError as e:
logging.warning(f"Saving {DB_type.name} database in \"{DB_TODAY_FILEPATH}\" failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
else:
logging.info(f"\rSaved {DB_type.name} database in \"{DB_TODAY_FILEPATH}\".")
return DB
Expand All @@ -73,7 +73,7 @@ def init_DB(DB_type: DB_Type, DB: pandas.DataFrame, now_DT: dt.datetime, DOWNLOA
try:
os.remove(DB_filepath)
except OSError as e:
logging.warning(f"Removing \"{DB_filepath}\" failed with {KFSfstr.full_class_name(e)}.")
logging.warning(f"Removing \"{DB_filepath}\" failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
logging.info(f"\rRemoved \"{DB_filepath}\".")

# if database empty: downloading unsuccessful, load from archive
Expand All @@ -88,7 +88,7 @@ def init_DB(DB_type: DB_Type, DB: pandas.DataFrame, now_DT: dt.datetime, DOWNLOA
try:
DB=pandas.read_csv(DB_filepath)
except OSError as e:
logging.warning(f"Loading {DB_type.name} database from \"{DB_filepath}\" failed with {KFSfstr.full_class_name(e)}.")
logging.warning(f"Loading {DB_type.name} database from \"{DB_filepath}\" failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
else: # loading successful
logging.info(f"\rLoaded {DB_type.name} database from \"{DB_filepath}\".")
return DB
Expand Down
11 changes: 7 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,17 @@ def __exit__(self, exc_type, exc_value, exc_traceback): # up

try:
await discord_bot.get_channel(channel_id).send(message_send) # send message to discord # type:ignore
except AttributeError: # get_channel already returned None, bot has probably been removed from server
logging.error("Sending message to discord failed. Assuming bot has been removed from server.")
except AttributeError as e: # get_channel already returned None, bot has probably been removed from server
logging.error(f"Sending message to discord failed with {KFSfstr.full_class_name(e)}. Assuming bot has been removed from server.")
logging.info(f"Deleting server {server.name} ({server.id}) from servers list...")
servers=[s for s in servers if s.id!=server.id] # delete from list
logging.info(f"\rDeleted server {server.name} ({server.id}) from servers list.")
return
except discord.errors.DiscordServerError: # send failed
logging.error("Sending message to discord failed.")
except discord.errors.Forbidden as e: # bot has no permission to send message
logging.error(f"Sending message to discord failed with {KFSfstr.full_class_name(e)}. Bot has no permission to send message.")
return
except discord.errors.DiscordServerError as e: # send failed
logging.error(f"Sending message to discord failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
return
if append_TAF==False:
logging.info("\rSent METAR and original METAR.")
Expand Down
8 changes: 3 additions & 5 deletions src/process_METAR_TAF.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from change_format import change_format # change information format
from change_format_RMK import change_format_RMK # in remark section change information format
from Doc_Type import Doc_Type # processing METAR or TAF?
from KFSfstr import KFSfstr
from Server import Server
from Station import Station

Expand Down Expand Up @@ -40,11 +41,8 @@ def process_METAR_TAF(doc_type: Doc_Type, station: Station, RWY_DB: pandas.DataF
logging.info(f"Downloading {doc_type.name}...")
try:
METAR_TAF_o=requests.get(METAR_TAF_URL, timeout=DOWNLOAD_TIMEOUT) # download METAR or TAF
except requests.ConnectTimeout: # if unsuccessful: abort
logging.error(f"\rDownloading {doc_type.name} timed out after {DOWNLOAD_TIMEOUT}s.")
raise
except requests.ConnectionError: # if unsuccessful: abort
logging.error(f"\rDownloading {doc_type.name} failed.")
except requests.ConnectionError as e: # if unsuccessful: abort
logging.error(f"\rDownloading {doc_type.name} failed with {KFSfstr.full_class_name(e)}. Error message: {e.args}")
raise
else:
logging.info(f"\rDownloaded {doc_type.name}.")
Expand Down

0 comments on commit 1014095

Please sign in to comment.