Skip to content

Commit

Permalink
User-specific information no longer hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
ntvmb committed Apr 19, 2024
1 parent a0f78b4 commit 07aeb02
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
CycloMonitor changelog

# 2024.4.19
* Added: New configuration file parameters: `client_id`, `github`, `emojis`
* Added: Docs for configuration file
* **Changed: Client ID, GitHub link, and custom Emojis are no longer hardcoded.**

# 2024.4.15
* Added: command-line arguments
* Added: support for configuration files
Expand Down
95 changes: 95 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Configuring CycloMonitor
First off, make sure your configuration is in JSON format, as shown in `example_config.json`.

To run CycloMonitor with a configuration file:
`python3 -m cyclomonitor -b -c CONFIG.json`

`token`: A string containing your bot's token. This field is required.
```json
{
"token": "your.token.here"
}
```

`log_file`: If set, CycloMonitor will log to the specified file, relative to the working directory.
```json
{
"log_file": "bot.log"
}
```

`client_id`: The ID of your bot. This is used by the `/invite` command.
```json
{
"client_id": 1107462705004167230
}
```

`github`: A link to a git repository containing the bot's source code (it doesn't have to be GitHub).
```json
{
"github": "https://github.com/ntvmb/cyclomonitor"
}
```

`emojis`: A dictionary consisting of emojis to use when designating TCs. If any one of them is not set, a default emoji is used.
* `ex`: Extratropical cyclone
* `low`: Post-tropical low/invest
* `remnants`: Remnant low
* `td`: Tropical depression
* `sd`: Subtropical depression
* `ts`: Tropical storm
* `ss`: Subtropical storm
* `cat1`: Category 1 (SSHWS)
* `cat2`: Category 2 (SSHWS)
* `cat3`: Category 3 (SSHWS)
* `cat4`: Category 4 (SSHWS)
* `cat5`: Category 5 (137-152 kt)
* `cat5intense`: Category 5 (153-167 kt)
* `cat5veryintense`: Category 5 (>167 kt)
```json
{
"emojis": {
"ex": "<:ex:1109994645431259187>",
"low": "<:low:1109997033227558923>",
"remnants": "<:remnants:1109994646932836386>",
"td": "<:td:1109994651169079297>",
"sd": "<:sd:1109994648300163142>",
"ts": "<:ts:1109994652368650310>",
"ss": "<:ss:1109994649654935563>",
"cat1": "<:cat1:1109994357257420902>",
"cat2": "<:cat2:1109994593895862364>",
"cat3": "<:cat3:1109994641094357024>",
"cat4": "<:cat4:1109994643057295401>",
"cat5": "<:cat5:1109994644386893864>",
"cat5intense": "<:cat5intense:1111376977664954470>",
"cat5veryintense": "<:cat5veryintense:1111378049448026126>"
}
}
```

Full example:
```json
{
"token": "your.token.here",
"log_file": "bot.log",
"client_id": 1107462705004167230,
"github": "https://github.com/ntvmb/cyclomonitor",
"emojis": {
"ex": "<:ex:1109994645431259187>",
"low": "<:low:1109997033227558923>",
"remnants": "<:remnants:1109994646932836386>",
"td": "<:td:1109994651169079297>",
"sd": "<:sd:1109994648300163142>",
"ts": "<:ts:1109994652368650310>",
"ss": "<:ss:1109994649654935563>",
"cat1": "<:cat1:1109994357257420902>",
"cat2": "<:cat2:1109994593895862364>",
"cat3": "<:cat3:1109994641094357024>",
"cat4": "<:cat4:1109994643057295401>",
"cat5": "<:cat5:1109994644386893864>",
"cat5intense": "<:cat5intense:1111376977664954470>",
"cat5veryintense": "<:cat5veryintense:1111378049448026126>"
}
}
```
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ Windows users: You might want to replace `python3` with `python`.
If you're using a virtual environment, ensure it is active before running the bot.
CycloMonitor logs to stdout by default. To log to a file, specify the parameter `-l LOGFILE`, where `LOGFILE` is the path to the log file.

## Configuring the bot
See CONFIGURATION.md for details.

## CLI
Coming soon

Supports Python 3.8+, but Python 3.11+ is recommended.
22 changes: 20 additions & 2 deletions example_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
{
"token": "your.token.here",
"log_file": "bot.log"
}
"log_file": "bot.log",
"client_id": 1107462705004167230,
"github": "https://github.com/ntvmb/cyclomonitor",
"emojis": {
"ex": "<:ex:1109994645431259187>",
"low": "<:low:1109997033227558923>",
"remnants": "<:remnants:1109994646932836386>",
"td": "<:td:1109994651169079297>",
"sd": "<:sd:1109994648300163142>",
"ts": "<:ts:1109994652368650310>",
"ss": "<:ss:1109994649654935563>",
"cat1": "<:cat1:1109994357257420902>",
"cat2": "<:cat2:1109994593895862364>",
"cat3": "<:cat3:1109994641094357024>",
"cat4": "<:cat4:1109994643057295401>",
"cat5": "<:cat5:1109994644386893864>",
"cat5intense": "<:cat5intense:1111376977664954470>",
"cat5veryintense": "<:cat5veryintense:1111378049448026126>"
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import pathlib

__version__ = "2024.4.15"
__version__ = "2024.4.19"

here = pathlib.Path(__file__).parent.resolve()

Expand Down
49 changes: 29 additions & 20 deletions src/cyclomonitor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
"""
logname = "bot.log"
# PLEASE CHANGE THESE LINKS IF YOU ARE FORKING THIS PROJECT.
INVITE = "https://discord.com/api/oauth2/authorize?client_id=1107462705004167230&permissions=67496000&scope=bot"
GITHUB = "https://github.com/ntvmb/cyclomonitor"
INVITE = "https://discord.com/api/oauth2/authorize?client_id={0}&permissions=67496000&scope=bot"
GITHUB = "GITHUB_LINK"
languages = ["C", "en_US"]
emojis = {}

# increase compatibility with python<3.11
if not hasattr(datetime, "UTC"):
Expand Down Expand Up @@ -212,10 +213,9 @@ async def on_ibtracs_update_error(self, error):
self.is_best_track_updating = False


# this function needs to be a coroutine since other coroutines are called
async def update_guild(guild: int, to_channel: int):
set_locale(server_vars.get("lang", guild))
"""Given a guild ID and channel ID, post ATCF data."""
set_locale(server_vars.get("lang", guild))
logging.info(LOG_UPDATE_GUILD.format(guild))
channel = bot.get_channel(to_channel)
enabled_basins = server_vars.get("basins", guild)
Expand Down Expand Up @@ -293,35 +293,35 @@ async def update_guild(guild: int, to_channel: int):
if tc_class == "EX":
if not name == "INVEST":
tc_class = CLASS_PTC
emoji = "<:ex:1109994645431259187>"
emoji = emojis.get("ex")
elif tc_class == "LO" or tc_class == "INVEST":
if not name == "INVEST":
tc_class = CLASS_PTC
emoji = "<:low:1109997033227558923>"
emoji = emojis.get("low")
elif tc_class == "DB" or tc_class == "WV":
if not name == "INVEST":
tc_class = CLASS_RL
emoji = "<:remnants:1109994646932836386>"
emoji = emojis.get("remnants")
elif wind < 35:
if not (tc_class == "SD" or name == "INVEST"):
# ignored if invest in case of autoflagging
# ATCF will autoflag a system to be a TD once it has attained 1-minute sustained winds of between 23 and 33 kt
tc_class = CLASS_TD
emoji = "<:td:1109994651169079297>"
emoji = emojis.get("td")
elif name == "INVEST" and not tc_class == "SD":
emoji = "<:low:1109997033227558923>"
emoji = emojis.get("low")
else:
tc_class = CLASS_SD
emoji = "<:sd:1109994648300163142>"
emoji = emojis.get("sd")
elif wind > 34 and wind < 65:
if not (tc_class == "SS" or name == "INVEST"):
tc_class = CLASS_TS
emoji = "<:ts:1109994652368650310>"
emoji = emojis.get("ts")
elif name == "INVEST" and not tc_class == "SS":
emoji = "<:low:1109997033227558923>"
emoji = emojis.get("low")
else:
tc_class = CLASS_SS
emoji = "<:ss:1109994649654935563>"
emoji = emojis.get("ss")
else:
# determine the term to use based on the basin
# we assume at this point that the system is either a TC or extratropical
Expand All @@ -341,23 +341,25 @@ async def update_guild(guild: int, to_channel: int):
# for custom emoji to work, the bot needs to be in the server it's from
# you also need the emoji's ID
if wind < 85:
emoji = "<:cat1:1109994357257420902>"
emoji = emojis.get("cat1")
elif wind > 84 and wind < 100:
emoji = "<:cat2:1109994593895862364>"
emoji = emojis.get("cat2")
elif wind > 99 and wind < 115:
emoji = "<:cat3:1109994641094357024>"
emoji = emojis.get("cat3")
elif wind > 114 and wind < 140:
emoji = "<:cat4:1109994643057295401>"
emoji = emojis.get("cat4")
elif wind > 139 and wind < 155:
emoji = "<:cat5:1109994644386893864>"
emoji = emojis.get("cat5")
elif wind > 154 and wind < 170:
emoji = "<:cat5intense:1111376977664954470>"
emoji = emojis.get("cat5intense")
else:
emoji = "<:cat5veryintense:1111378049448026126>"
emoji = emojis.get("cat5veryintense")
if name == "INVEST":
name = display_name = cyc_id
else:
display_name = f"{cyc_id} ({name})"
if emoji is None:
emoji = ":cyclone:"
# update TC records
if current_TC_record is not None:
if (wind > int(current_TC_record[5])) or (
Expand Down Expand Up @@ -980,6 +982,7 @@ def main():
import argparse
import json

global GITHUB, INVITE
locale_init()
parser = argparse.ArgumentParser(
prog="cyclomonitor",
Expand Down Expand Up @@ -1025,6 +1028,12 @@ def main():
log_params["filename"] = config["log_file"]
log_params["filemode"] = "a"
logging.captureWarnings(True)
if config.get("github") is not None:
GITHUB = config["github"]
if config.get("client_id") is not None:
INVITE = INVITE.format(config["client_id"])
if isinstance(config.get("emojis"), dict):
emojis.update(config["emojis"])
if args.verbose:
log_params["level"] = logging.DEBUG
else:
Expand Down

0 comments on commit 07aeb02

Please sign in to comment.