Skip to content

Overhaul Bot and utilize Discord Embed#20

Open
kphoenix137 wants to merge 11 commits intodiasurgical:mainfrom
kphoenix137:embeds
Open

Overhaul Bot and utilize Discord Embed#20
kphoenix137 wants to merge 11 commits intodiasurgical:mainfrom
kphoenix137:embeds

Conversation

@kphoenix137
Copy link
Contributor

@kphoenix137 kphoenix137 commented Oct 2, 2023

Adds embeds, as well as overhauling the whole python end of the bot.

Features:

  • Split files for maintainability
  • config.json
    • Can be modified by set bot owners using new commands!
    • Doesn't require restart to apply changes
  • More console logging
    • Helps with debugging and displays useful information
    • More data using the Debug setting in config
  • Detects the backend application (./devilutionx-gamelist) based on OS used
  • Improved error handling
  • Multiple attempts at refreshing games in case a refresh fails to avoid marking games as closed incorrectly
  • Automatic graphic for game type
    • You can add a graphic with the 4 digit code for a game type as a PNG to the images directory to automatically have that graphic display in the embed for the respective game type

image

image

@kphoenix137 kphoenix137 changed the title Change Plain Text to Embed Overhaul Bot and utilize Discord Embed Mar 16, 2025
Copy link
Member

@StephenCWills StephenCWills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how this is looking now. Here are a few more suggestions for cleaning things up a bit.

@StephenCWills
Copy link
Member

Come to think of it, it might be tough to resolve those conflicts with discord_bot.py now that you've demolished it.

@kphoenix137 kphoenix137 force-pushed the embeds branch 2 times, most recently from 9577cff to 125cefb Compare March 16, 2025 04:48
Add "Game Closed" to replace game id

Remove Players and Options as inline

Update game icons

Modify game title based on 4 letter code

Update missing game ids

Handle undefined difficulty and game speed

Change unknown to custom for tick rate

Overhaul

Revert to DevilutionX Game List channel

Update bot.py

Co-authored-by: Stephen C. Wills <[email protected]>

Update bot.py

Co-authored-by: Stephen C. Wills <[email protected]>

Update commands.py

Co-authored-by: Stephen C. Wills <[email protected]>

Update game_manager.py

Co-authored-by: Stephen C. Wills <[email protected]>

Update bot.py

Co-authored-by: Stephen C. Wills <[email protected]>

Update bot.py

Co-authored-by: Stephen C. Wills <[email protected]>

Pass config as param

Avoid using outdated config

Fix mistake

Delete discord_bot.py

Rename bot.py

Return types

Appease mypy

Change code to British

More British
@AJenbo
Copy link
Member

AJenbo commented Mar 16, 2025

Split files for maintainability

I'll have to change my setup ... but i should have done so a long time ago

@AJenbo
Copy link
Member

AJenbo commented Mar 16, 2025

@kphoenix137
Copy link
Contributor Author

Need to update this line to not lower code quality: https://github.com/diasurgical/devilutionx-gamelist/blob/main/.github/workflows/mypy.yml#L23

Not sure what you mean

Copy link
Member

@StephenCWills StephenCWills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried going through the commits that you rebased over to see if anything got lost. Most of it was either incorporated, re-implemented, or inconsequential. But I did notice a couple things.

@kphoenix137 kphoenix137 mentioned this pull request Mar 16, 2025
@StephenCWills StephenCWills dismissed their stale review March 17, 2025 00:43

Requested changes have been addressed

@kphoenix137 kphoenix137 requested a review from AJenbo March 17, 2025 03:07
Comment on lines +38 to +48
def censor_bad_words(name: str) -> str:
"""Replaces banned words in a player's name with asterisks."""
name_upper = name.upper()
for bad_word in BANNED_WORDS:
if bad_word in name_upper:
masked_word = "*" * len(bad_word) # Replace with asterisks
pattern = re.compile(
re.escape(bad_word), re.IGNORECASE
) # Case-insensitive match
name = pattern.sub(masked_word, name) # Replace in original case
return name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows people to infer what words the banlist contains, it's not currently part of the repo I assume by design.

It's also possible to have overlapping banned words, e.g. if "potato" and "tomato" are banned, the name "potatomato" will be censored as either "******mato" or "pota******" depending on the order of the banlist with this code (because you're searching in name_upper but performing the substitution in name).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... it's not currently part of the repo I assume by design.

If I were to guess, the main reason is so that AJenbo can add words ad-hoc when he feels he needs to, without having to open a PR here or commit changes to the repo.

Good points about overlapping words and being able to infer, though. Perhaps we should just censor the whole name and solve both issues?

Comment on lines +9 to +14
def debug_print(
*args: Any, sep: str = " ", end: str = "\n", file: Any = None, flush: bool = False
) -> None:
"""Prints messages only if debug mode is enabled."""
if CONFIG.get("debug", False):
print(*args, sep=sep, end=end, file=file, flush=flush)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you went back to basic print() output instead of using the existing logging config? You could still let people choose logging level via the config file (with the more widely used trace/debug/info/warning/error levels instead of just debug/warning)

Comment on lines +20 to +22
def sanitize_player_name(name: str) -> str:
"""Removes forbidden characters from a player's name."""
return re.sub(FORBIDDEN_CHARS, "", name) # Strip invalid characters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the choice to hide games with an invalid player name was deliberate as it's assumed players with these names are malicious. Any players which have a name containing one of these characters have set it using external tools. Vanilla Diablo/DevilutionX don't allow inputting these characters in a name. This is also missing the filtering/validation the current code applies to ensure that no ASCII control characters or multibyte sequences are present in a players name.

Comment on lines +28 to +30
# Run Bot
with open("./discord_bot_token", "r") as file:
token = file.readline().strip()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discord token used by the bot is currently part of the config file discord_bot.json. Renaming the config file to config.json is fine imo but I'd rather see the token remain as a config option instead of a bare file. If you'd rather split secret and non-secret options a convention used by other programs is to have config.json and config.private.json, where the latter is excluded from source control. E.g. in the current main branch discord_bot.json is listed in .gitignore and discord_bot.json.template provides an example of the expected structure. You could even put channel id in there since that's closely related to the auth token and not really of any use to other people.

Comment on lines +111 to +113
players = ", ".join(
censor_bad_words(sanitize_player_name(player)) for player in game["players"]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears you've dropped the code that escapes discord formatting characters in player names. This will cause players with valid names (e.g. "M`din") to trigger discord's markdown parser and cause odd formatting to get applied to the embed message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants