Skip to content

Commit

Permalink
Add pre-commit hooks for better consistency (#62)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the Title above -->

## Description
<!--- Describe your changes in detail -->
Add pre-commit hooks to make committing code more consistent overall

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
  • Loading branch information
dragid10 committed Nov 20, 2023
1 parent 0bbff5d commit 5f59ed3
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,4 @@ config-template.ini
docker-compose.yml
.flake8
.idea/*
!.git
!.git
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.

20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.6
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 # Use the ref you want to point at
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-toml
- id: end-of-file-fixer
- id: name-tests-test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ deploy:
flyctl deploy --detach --push --now --no-cache

deploy-status:
flyctl status
flyctl status
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ to receive alerts and keep track of players).
- session day: Day of the session.
- session time: Time of the session in 24h, HH:MM format.
- first alert: First _alert_ from the bot reminding players to RSVP.
- second alert: Second RSVP reminder.
- second alert: Second RSVP reminder.
72 changes: 31 additions & 41 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from subprocess import check_output

import discord
from discord import Embed, Intents, app_commands, ScheduledEvent
from discord import Embed, Intents, ScheduledEvent, app_commands
from discord.ext import commands, tasks
from discord.ext.commands import Context
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from pytz import timezone

import helpers
from helpers import adjacent_days, plist, Weekdays, Emojis
from helpers import Emojis, Weekdays, adjacent_days, plist
from mongo_tracker import Tracker
from tasks import BotTasks

Expand All @@ -38,7 +38,6 @@
discord_vc = bot_config["discord"]["vc"]
except KeyError:
# Fall back to environment variables
from os import environ
from decouple import config

token = config("token")
Expand All @@ -53,7 +52,7 @@
discord_vc = config("discordVC")

# Bot init
tz = timezone('US/Eastern')
tz = timezone("US/Eastern")
intents = Intents.all()
intents.members = True
intents.message_content = True
Expand Down Expand Up @@ -81,7 +80,7 @@ async def on_ready():
async def status(ctx: Context):
try:
# Try a quick ping to make sure things can connect
mongo_client['admin'].command("ping")
mongo_client["admin"].command("ping")
except ConnectionFailure as ce:
db_status = "offline"
logging.exception(ce)
Expand All @@ -104,9 +103,11 @@ async def config(ctx: Context):
:param ctx: Context of the discord bot
:return:
"""
questions: list[tuple] = [("session-day", "What day is the session typically had?"),
("first-alert", "When would you like to send the first alert?"),
("second-alert", "When would you like to send the second alert?")]
questions: list[tuple] = [
("session-day", "What day is the session typically had?"),
("first-alert", "When would you like to send the first alert?"),
("second-alert", "When would you like to send the second alert?"),
]
answers = [await ask_for_day(ctx, q) for q in questions]
session_vc_id = discord.utils.get(ctx.guild.voice_channels, name=discord_vc)
session_vc_id = session_vc_id.id
Expand All @@ -117,10 +118,7 @@ def map_emoji_to_day_value(emoji):
return Weekdays[e.name].value

mapped_answers = [map_emoji_to_day_value(a) for a in answers]
config = {
questions[i][0]: mapped_answers[i]
for i in range(len(mapped_answers))
}
config = {questions[i][0]: mapped_answers[i] for i in range(len(mapped_answers))}
config["session-time"] = await ask_for_time(ctx)
tracker.create_guild_config(
guild_id=ctx.guild.id,
Expand Down Expand Up @@ -189,16 +187,13 @@ async def register(ctx: Context):
async def players(ctx: Context):
players = tracker.get_players_for_guild(ctx.guild.id)
if not players:
await ctx.message.channel.send(f"No players registered!")
await ctx.message.channel.send("No players registered!")
else:
await ctx.message.channel.send(
embed=Embed().from_dict(
{
"title": "Registered Players",
"fields": [
{"name": player["name"], "value": f"ID: {player['id']}"}
for player in players
],
"fields": [{"name": player["name"], "value": f"ID: {player['id']}"} for player in players],
}
)
)
Expand All @@ -210,9 +205,7 @@ async def cmds(ctx: Context):
embed=Embed().from_dict(
{
"title": "Available Commands",
"fields": [
{"name": cmd.name, "value": f"`{cmd.name}`"} for cmd in bot.commands
],
"fields": [{"name": cmd.name, "value": f"`{cmd.name}`"} for cmd in bot.commands],
}
)
)
Expand Down Expand Up @@ -259,34 +252,32 @@ async def cancel(ctx: Context):

# If player calling command isn't DM, then tell them so and return
if not tracker.is_player_dm(guild_id, player_id):
await ctx.message.reply(f"Sorry this is a DM-only command. Have the DM run this instead")
await ctx.message.reply("Sorry this is a DM-only command. Have the DM run this instead")
return

was_cancelled = tracker.cancel_session(guild_id)
if was_cancelled:
await ctx.message.channel.send(f"The upcoming session has been cancelled!")
await ctx.message.channel.send("The upcoming session has been cancelled!")
else:
await ctx.message.reply(f"Ran into an error cancelling the session. Please try again")
await ctx.message.reply("Ran into an error cancelling the session. Please try again")


# Support rsvp [accept|decline]
@bot.group()
async def rsvp(ctx: Context):
if ctx.invoked_subcommand is None:
await ctx.message.reply(
f"Please use either `{bot_prefix}rsvp accept` or `{bot_prefix}rsvp decline`."
)
await ctx.message.reply(f"Please use either `{bot_prefix}rsvp accept` or `{bot_prefix}rsvp decline`.")


@rsvp.command(name="accept")
async def _accept(ctx: Context):
guild_id = ctx.guild.id
if tracker.is_session_cancelled(guild_id):
await ctx.message.reply(f"The upcoming session has been cancelled, so no need to RSVP")
await ctx.message.reply("The upcoming session has been cancelled, so no need to RSVP")
return

if not tracker.is_registered_player(ctx.guild.id, ctx.author):
await ctx.message.reply(f"You are not a registered player in this campaign, so you can not rsvp")
await ctx.message.reply("You are not a registered player in this campaign, so you can not rsvp")
else:
tracker.add_attendee_for_guild(ctx.guild.id, ctx.author)
await ctx.message.reply(
Expand All @@ -310,18 +301,19 @@ async def _accept(ctx: Context):
if tracker.is_full_group(ctx.guild.id):
sess_event = await _create_session_event(ctx)
await ctx.message.channel.send(
f"All players have confirmed attendance, so I've automatically created an event: {sess_event.url}")
f"All players have confirmed attendance, so I've automatically created an event: {sess_event.url}"
)


@rsvp.command(name="decline")
async def _decline(ctx: Context):
guild_id = ctx.guild.id
if tracker.is_session_cancelled(guild_id):
await ctx.message.reply(f"The upcoming session has been cancelled, so no need to RSVP")
await ctx.message.reply("The upcoming session has been cancelled, so no need to RSVP")
return

if not tracker.is_registered_player(ctx.guild.id, ctx.author):
await ctx.message.reply(f"You are not a registered player in this campaign so you can not rsvp")
await ctx.message.reply("You are not a registered player in this campaign so you can not rsvp")
else:
tracker.add_decliner_for_guild(ctx.guild.id, ctx.author)
await ctx.message.reply(
Expand All @@ -344,9 +336,7 @@ async def _decline(ctx: Context):
@bot.group()
async def vote(ctx: Context):
if ctx.invoked_subcommand is None:
await ctx.message.channel.send(
f"Please `{bot_prefix}vote cancel`"
)
await ctx.message.channel.send(f"Please `{bot_prefix}vote cancel`")


@vote.command(name="cancel")
Expand Down Expand Up @@ -384,7 +374,7 @@ async def _create_session_event(ctx: Context) -> ScheduledEvent:
start_time=next_sess,
channel=session_vc,
entity_type=discord.EntityType.voice,
reason="D&D Session"
reason="D&D Session",
)


Expand All @@ -393,24 +383,24 @@ async def _create_session_event(ctx: Context) -> ScheduledEvent:

@tasks.loop(hours=1)
async def alert_dispatcher(force=False):
logging.info(f"Checking to see if it is time to remind players")
logging.debug(f"Logging into Discord")
logging.info("Checking to see if it is time to remind players")
logging.debug("Logging into Discord")
await bot.login(token)

# See if it's time to send message asking if players are available
if int(datetime.now(tz).strftime("%H")) != alert_time and force is False:
logging.debug(f"It is not yet time to alert")
logging.debug("It is not yet time to alert")
return

logging.debug(f"It IS time to alert")
logging.debug("It IS time to alert")
today = datetime.now(tz).weekday()
day_before, _ = adjacent_days(today)

# Check if all players have registered for the upcoming session (on the first day)
for config in tracker.get_first_alert_configs(today):
guild_id = config["guild"]
if tracker.is_session_cancelled(guild_id):
logging.debug(f"Next session was cancelled! Won't alert")
logging.debug("Next session was cancelled! Won't alert")
await bt.cancel_alert_msg(config)
return

Expand All @@ -423,7 +413,7 @@ async def alert_dispatcher(force=False):
for config in tracker.get_second_alert_configs(today):
guild_id = config["guild"]
if tracker.is_session_cancelled(guild_id):
logging.debug(f"Next session was cancelled! Won't alert")
logging.debug("Next session was cancelled! Won't alert")
await bt.cancel_alert_msg(config)
return

Expand Down
2 changes: 1 addition & 1 deletion config-template.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ time =

[campaign]
name =
alias =
alias =
2 changes: 1 addition & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def get_next_session_day(session_day, session_time) -> datetime:


def callable_username(username: str):
return f"<@{username}>".strip()
return f"<@{username}>".strip()

0 comments on commit 5f59ed3

Please sign in to comment.