Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email Verification Bot for BITS Pilani Discord Server #1

Merged
merged 30 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f125dfe
delete the previous code
sattwyk Oct 25, 2024
f6c277d
uv project init
sattwyk Oct 25, 2024
ba52aff
removed README
sattwyk Oct 25, 2024
c26b165
removed hello.py
sattwyk Oct 26, 2024
6161d01
uv init
sattwyk Oct 27, 2024
85c3cc8
add discord bot
sattwyk Oct 27, 2024
d5d23da
add docker-compose for local devlopment
sattwyk Oct 27, 2024
8bb5601
refactor bot.py
sattwyk Oct 27, 2024
b72d1be
added a healthcheck port for gcr
sattwyk Oct 27, 2024
0c33b69
update dockerfile to expose 8080 port
sattwyk Oct 27, 2024
f84693b
removed .env.example
sattwyk Oct 27, 2024
03d00ba
setup infra with tf
sattwyk Oct 27, 2024
ec37dd4
expose port 8080
sattwyk Oct 27, 2024
ab73ef8
added deploy script
sattwyk Oct 27, 2024
751777f
added .gitignore
sattwyk Oct 27, 2024
74f2fa2
apply auto-approve
sattwyk Oct 27, 2024
99cb444
added https prefix to the upstash endpoint
sattwyk Oct 27, 2024
60fb7ec
increase rate limit
sattwyk Oct 27, 2024
7226153
refactor health check handler
sattwyk Oct 28, 2024
40590ae
refactor modals to defer the response
sattwyk Oct 28, 2024
063fb55
added error handler for application command errors
sattwyk Oct 28, 2024
a1bbde9
refactor signal handler
sattwyk Oct 28, 2024
de3db85
reduce the ratelimit back to 5
sattwyk Oct 28, 2024
0fd2bc6
refactor and new workflow
sattwyk Oct 28, 2024
be20e69
removed unused imports
sattwyk Oct 28, 2024
7ef17c5
no need to run uv directly run python
sattwyk Oct 28, 2024
3b31a8a
updated README.md
sattwyk Oct 28, 2024
294857d
added deployment github action workflow
sattwyk Oct 28, 2024
c79d8c2
add google auth and fix env names
sattwyk Oct 28, 2024
2a9c809
add permission and fix credentials_json
sattwyk Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .devcontainer/devcontainer.json

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy to Cloud Run

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
SERVICE_NAME: discord-bot

jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'

steps:
- uses: actions/checkout@v4

- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'

- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'

- name: Configure Docker
run: gcloud auth configure-docker

- name: Build and Push Docker image
run: |
echo "Building image for project: $PROJECT_ID"
docker build -t gcr.io/$PROJECT_ID/$SERVICE_NAME:${{ github.sha }} ./bot
docker push gcr.io/$PROJECT_ID/$SERVICE_NAME:${{ github.sha }}

- name: Deploy to Cloud Run
run: |
gcloud run deploy $SERVICE_NAME \
--image gcr.io/$PROJECT_ID/$SERVICE_NAME:${{ github.sha }} \
--platform managed \
--region asia-south1 \
--allow-unauthenticated \
--set-env-vars="DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }},RESEND_API_KEY=${{ secrets.RESEND_API_KEY }},UPSTASH_REDIS_REST_URL=${{ secrets.UPSTASH_REDIS_REST_URL }},UPSTASH_REDIS_REST_TOKEN=${{ secrets.UPSTASH_REDIS_REST_TOKEN }}"
160 changes: 1 addition & 159 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,160 +1,2 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.env.prod
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# discord-bot
# BITS Pilani Discord Email Verification Bot

source .venv/bin/activate
This bot provides email verification for the BITS Pilani Discord server, ensuring that only students with valid BITS Pilani email addresses can access certain channels.

## Features

- Email verification using BITS Pilani student email addresses
- Discord slash command for initiating verification
- Rate limiting to prevent abuse
- Automatic role assignment upon successful verification
- Docker support for easy deployment

## Local Development Setup

1. Clone the repository:
```
git clone https://github.com/bits-bsc-cs/discord-bot.git
cd discord-bot
```

2. Create a `.env` file in the root directory with the following variables:
```
DISCORD_TOKEN=your_discord_bot_token
RESEND_API_KEY=your_resend_api_key
```

3. Install Docker and Docker Compose on your system.

4. Run the bot locally using Docker Compose:
```
docker-compose up --build
```

5. The bot should now be running and connected to Discord. You can test it by using the `/verify` command in your Discord server.

## Usage

1. Invite the bot to your Discord server and ensure it has the necessary permissions.
2. Users can start the verification process by using the `/verify` command.
3. The bot will prompt users to enter their BITS Pilani email address.
4. A verification code will be sent to the provided email.
5. Users must enter the verification code to complete the process.
6. Upon successful verification, users will be assigned the "Verified" role.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
9 changes: 9 additions & 0 deletions bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.uv
.venv
.ruff_cache

.env
.env.local
.env.production


1 change: 1 addition & 0 deletions bot/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
35 changes: 35 additions & 0 deletions bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Use the official Python image as the base image
FROM python:3.10-slim-bookworm AS base

# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV UV_SYSTEM_PYTHON=1
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy
ENV PORT=8080

# Set the working directory
WORKDIR /app

# Copy only the necessary files for dependency installation
COPY pyproject.toml uv.lock ./

# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
uv pip install --system -r pyproject.toml

# Copy the rest of the application code
COPY bot.py ./

# Compile bytecode
RUN python -m compileall .

# Expose the port the app runs on
EXPOSE 8080

# Set the command to run the application
CMD ["python", "bot.py"]
Loading
Loading