Skip to content

Commit

Permalink
Merge pull request #2 from Qwizi/ruff
Browse files Browse the repository at this point in the history
Ruff
  • Loading branch information
Qwizi authored Mar 12, 2024
2 parents 5bd5659 + 3eac393 commit 4c588f8
Show file tree
Hide file tree
Showing 26 changed files with 3,115 additions and 1,236 deletions.
13 changes: 12 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
TOKEN=
API_URL=
API_TOKEN=
TESTING=False
TESTING=False
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=0
REDIS_PASSWORD=
SENTRY_DSN=
GUILD_ID=
LOBBY_CHANNEL_ID=
TEAM1_CHANNEL_ID=
TEAM2_CHANNEL_ID=
TEAM1_ROLE_ID=
TEAM2_ROLE_ID=
3 changes: 2 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Publish Backend Docker image

on:
workflow_dispatch:
release:
types: [published]


jobs:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ poetry.toml
# LSP config files
pyrightconfig.json

# End of https://www.toptal.com/developers/gitignore/api/python
# End of https://www.toptal.com/developers/gitignore/api/python
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: "https://github.com/astral-sh/ruff-pre-commit"
rev: "v0.1.5"
hooks:
- id: "ruff"
name: "Run the Ruff linter"
types_or: ["python", "pyi"]
args: ["--fix"]
- id: "ruff-format"
name: "Run the Ruff formatter"
types_or: ["python", "pyi"]
- repo: "https://github.com/python-poetry/poetry"
rev: "1.7.0"
hooks:
- id: "poetry-lock"
stages: ["push"]
name: "Run Poetry lock hook"
args: ["--no-update"]
- id: "poetry-check"
stages: ["push"]
name: "Run Poetry check hook"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ RUN chown -R fastapiuser:fastapi /app
# Switch to the dedicated user
USER fastapiuser

# Start the application
CMD ["python", "main.py"]
# Start the bot.
CMD ["task", "start"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## CS2 Battle Discord Bot
1 change: 1 addition & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Main bot package."""
66 changes: 66 additions & 0 deletions bot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""CS2 Battle Bot main module."""

import discord
import redis
import sentry_sdk
from dotenv import load_dotenv
from redis import ConnectionError, TimeoutError

from bot.cogs.match import MatchCog
from bot.logger import logger
from bot.settings import settings

load_dotenv()

bot = discord.Bot(intents=discord.Intents.all())
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
environment="production" if not settings.TESTING else "development",
)


@bot.event
async def on_ready() -> None:
"""Print a message when the bot is ready."""
logger.debug(f"We have logged in as {bot.user}")


@bot.event
async def on_application_command_error(
ctx: discord.ApplicationContext, error: discord.DiscordException
) -> None:
"""
Handle errors that occur while processing a command.
Args:
----
ctx (discord.ApplicationContext): The context in which the command was invoked.
error (discord.DiscordException): The error that occurred.
Returns:
-------
None: This function does not return anything.
"""
logger.error(repr(error))
sentry_sdk.capture_exception(error)
await ctx.respond("An error occurred while processing the command.")


try:
_redis = redis.StrictRedis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
db=settings.REDIS_DB,
password=settings.REDIS_PASSWORD if settings.REDIS_PASSWORD else None,
)
response = _redis.ping()
pubsub = _redis.pubsub()
pubsub.psubscribe("event.*")
bot.add_cog(MatchCog(bot, pubsub))
logger.debug(f"Connected to Redis: {response}")
except (ConnectionError, TimeoutError) as e:
logger.error(f"Redis connection error: {e}")
# Handle the error appropriately, e.g., retrying or logging
sentry_sdk.capture_exception(e)
bot.run(settings.TOKEN)
Loading

0 comments on commit 4c588f8

Please sign in to comment.