Skip to content

Update discord py #40

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

Merged
merged 18 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Make sure that shell scripts always use LF -- Windows likes to automatically change it to CRLF
*.sh text eol=lf

# Make sure Python scripts aren't modified either
*.py text eol=lf
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Programming-Contest-Suite/
├── register/
├── static/
├── templates/
├── bot.py
└── manage.py
```

Expand All @@ -77,7 +76,6 @@ Programming-Contest-Suite/
- `register`: user account registration and team creation
- `static`: target directory for all project static files upon deployment (placeholder dir)
- `templates`: target directory for all project HTML templates upon deployment
- `bot.py`: Discord bot which assists in LFG profile verification
- `manage.py`: Django management entrypoint

## Testing & Development Server
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ django-import-export = "*"
celery = "*"
gunicorn = "*"
six = "*"
"discord.py" = "<2.0"
requests = "*"
django-hashid-field = "*"
django-celery-beat = "*"
flower = "*"
discord-py = "==2.5.0"

[dev-packages]

Expand Down
449 changes: 403 additions & 46 deletions Pipfile.lock

Large diffs are not rendered by default.

27 changes: 0 additions & 27 deletions deploy/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ services:
SQL_DATABASE: contestsuite
SQL_USER: contestadmin
SQL_PASSWORD: seminoles1!

# Discord
GUILD_ID: # Discord Server ID
ports:
- 8000:8000
volumes:
Expand All @@ -29,29 +26,6 @@ services:
- rabbitmq
- celery_worker
restart: unless-stopped
scrape_bot:
image: contestsuite:dev
command: /docker/start.sh bot
environment:
# Django
DEBUG: 'True'

# Database
SQL_DATABASE: contestsuite
SQL_USER: contestadmin
SQL_PASSWORD: seminoles1!

# Discord
BOT_CHANNEL: # Name of command channel
GUILD_ID: # Discord Server ID
SCRAPE_BOT_TOKEN: # Token for the member list scraping bot
volumes:
- ../src:/app
networks:
- contestsuite
depends_on:
- mariadb
- django
celery_worker:
image: contestsuite:dev
command: /docker/start.sh worker debug
Expand All @@ -66,7 +40,6 @@ services:

# Discord
ANNOUNCEMENT_WEBHOOK_URL: # Webhook URL of the contest announcements channel
BOT_CHANNEL_WEBHOOK_URL: # Webhook URL of the bot command channel
volumes:
- ../src:/app
networks:
Expand Down
3 changes: 0 additions & 3 deletions docs/deployment/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ The following variables, located in the main settings file[^1], map to various P
Variable | Default | Description
---|---|---
ANNOUNCEMENT_WEBHOOK_URL | None | URL of Discord server's announcement channel webhook |
BOT_CHANNEL_WEBHOOK_URL | None | URL of Discord server's bot command channel webhook |
GUILD_ID | 0 | Discord server's Guid ID |
SCRAPE_BOT_TOKEN | None | Discord bot token required by the LFG helper bot |

## Misc

Expand Down
27 changes: 0 additions & 27 deletions docs/deployment/docker/docker_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ services:
SQL_DATABASE: contestsuite
SQL_USER: contestadmin
SQL_PASSWORD: seminoles1!

# Discord
GUILD_ID: # Discord Server ID
ports:
- 8000:8000
volumes:
Expand All @@ -40,29 +37,6 @@ services:
- rabbitmq
- celery_worker
restart: unless-stopped
scrape_bot:
image: contestsuite:dev
command: /docker/start.sh bot
environment:
# Django
DEBUG: 'True'

# Database
SQL_DATABASE: contestsuite
SQL_USER: contestadmin
SQL_PASSWORD: seminoles1!

# Discord
BOT_CHANNEL: # Name of command channel
GUILD_ID: # Discord Server ID
SCRAPE_BOT_TOKEN: # Token for the member list scraping bot
volumes:
- ../src:/app
networks:
- contestsuite
depends_on:
- mariadb
- django
celery_worker:
image: contestsuite:dev
command: /docker/start.sh worker debug
Expand All @@ -77,7 +51,6 @@ services:

# Discord
ANNOUNCEMENT_WEBHOOK_URL: # Webhook URL of the contest announcements channel
BOT_CHANNEL_WEBHOOK_URL: # Webhook URL of the bot command channel
volumes:
- ../src:/app
networks:
Expand Down
3 changes: 1 addition & 2 deletions docs/deployment/docker/docker_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ REQUIREMENTS | requirements.txt | Path to the manifest file to use during image

## Run Image

The `Dockerfile` specifies an image capable of running the Django web app, Celery, or a helper Discord bot. The parameters passed to the image's start script `start.sh` determine the container's behavior.
The `Dockerfile` specifies an image capable of running the Django web app, Celery, or Flower. The parameters passed to the image's start script `start.sh` determine the container's behavior.

```bash
docker run <image_name> /docker/start.sh <PROCESS_TYPE> <MODE>
Expand All @@ -36,7 +36,6 @@ server | Yes | Gunicorn server bound to the Django app
worker | No | Celery Worker
beat | No | Celery Beat
flower | No | Celery Flower
bot | No | Discord bot

`MODE` | Default | Usage
---|---|---
Expand Down
8 changes: 0 additions & 8 deletions docs/usage/contest_administration/contest_dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,5 @@ The Contest Dashboard centralizes many of the administrator actions available in
- **Account Tools**
- Manually activate a user account. Useful if a registrant has issues verifying their account.
- Mark a given team as a Faculty Team
- **Discord Tools**
- Creates the Upper/Lower Division Looking For Group roles on a Discord server
- server specified by `GUILD_ID` configuration variable
- Removes the Upper/Lower Division Looking For Group roles on a Discord server
- server specified by `GUILD_ID` configuration variable
- **Clear Discord Channel**
- Clears a channel on a Discord server of all content
- server specified by `GUILD_ID` configuration variable
- **Volunteers**
- Displays all volunteers registered in the system, whether they have registered for extra credit, and if they've checked into the contest
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cron-descriptor==1.4.0
defusedxml==0.7.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
deprecated==1.2.14; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
diff-match-patch==20230430; python_version >= '3.7'
discord.py==1.7.3
discord.py==2.5.0
django==4.2.4
django-celery-beat==2.5.0
django-hashid-field==3.3.7
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cron-descriptor==1.4.0
defusedxml==0.7.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
deprecated==1.2.14; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
diff-match-patch==20230430; python_version >= '3.7'
discord.py==1.7.3
discord.py==2.5.0
django==4.2.4
django-celery-beat==2.5.0
django-hashid-field==3.3.7
Expand Down
7 changes: 2 additions & 5 deletions scripts/docker/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

if [ $# -eq 0 ]; then
echo "Usage: start.sh [PROCESS_TYPE](server | worker | beat | flower | bot)"
echo "Usage: start.sh [PROCESS_TYPE](server | worker | beat | flower)"
exit 1
fi

Expand Down Expand Up @@ -74,10 +74,7 @@ elif [ "$PROCESS_TYPE" = "flower" ]; then
--loglevel INFO \
--conf=contestsuite/flowerconfig.py
fi
elif [ "$PROCESS_TYPE" = "bot" ]; then
python \
bot.py
else
echo "Invalid [PROCESS_TYPE](server | worker | beat | flower | bot)"
echo "Invalid [PROCESS_TYPE](server | worker | beat | flower)"
exit 1
fi
31 changes: 13 additions & 18 deletions src/announcements/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from celery import shared_task
from celery.utils.log import get_task_logger

from discord import Webhook, RequestsWebhookAdapter, Embed, InvalidArgument
from discord import SyncWebhook, Embed

from .models import Announcement
from contestsuite.settings import ANNOUNCEMENT_WEBHOOK_URL, DEFAULT_FROM_EMAIL, ALLOWED_HOSTS
Expand Down Expand Up @@ -61,22 +61,17 @@ def discord_announcement(id):
except:
logger.error(f'Failed to fetch announcement with id {id}')
else:
try:
# Initializing webhook
webhook = Webhook.from_url(
ANNOUNCEMENT_WEBHOOK_URL, adapter=RequestsWebhookAdapter())
except InvalidArgument:
logger.error('Failed to connect to announcement webhook')
# Initializing webhook
webhook = SyncWebhook.from_url(ANNOUNCEMENT_WEBHOOK_URL)
url = 'https://'+ALLOWED_HOSTS[0]+announcement.get_absolute_url()

if len(announcement.content) <= 140:
content = announcement.content
else:
url = 'https://'+ALLOWED_HOSTS[0]+announcement.get_absolute_url()

if len(announcement.content) <= 140:
content = announcement.content
else:
content = announcement.content[:140] + '...'

# Initializing an Embed
embed = Embed(title=announcement.title, description=content, url=url)
content = announcement.content[:140] + '...'

# Initializing an Embed
embed = Embed(title=announcement.title, description=content, url=url)

# Executing webhook.
webhook.send(embed=embed)
# Executing webhook.
webhook.send(embed=embed)
4 changes: 0 additions & 4 deletions src/contestadmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class Meta:
fields = ['results']


class ClearChannelForm(forms.Form):
channel_id = forms.IntegerField()


class UpdateProfileRoleForm(forms.ModelForm):
username = forms.CharField(
max_length=150,
Expand Down
62 changes: 2 additions & 60 deletions src/contestadmin/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from itertools import islice
from math import ceil, log10

from discord import Webhook, RequestsWebhookAdapter, InvalidArgument

from django.contrib.auth.models import User
from django.core.mail import send_mass_mail
from django.db import transaction
Expand All @@ -15,7 +13,7 @@
from celery import shared_task
from celery.utils.log import get_task_logger

from contestsuite.settings import MEDIA_ROOT, DEFAULT_FROM_EMAIL, BOT_CHANNEL_WEBHOOK_URL
from contestsuite.settings import MEDIA_ROOT, DEFAULT_FROM_EMAIL
from contestadmin.models import Contest
from core.utils import make_random_password
from manager.models import Course, Faculty, Profile
Expand Down Expand Up @@ -389,60 +387,4 @@ def process_contest_results():

logger.info(f"Processed contest results for {num_teams} teams")
else:
logger.error("No Team objects exist in database.")


@shared_task
def clear_discord_channel(id):
"""
Celery task to send a clear channel command message to a target Discord server.
- Command used by the ACM-FSU/pcs-bot

id: ID of the target channel on the Discord server which will be cleared
"""

try:
webhook = Webhook.from_url(
BOT_CHANNEL_WEBHOOK_URL, adapter=RequestsWebhookAdapter())
except InvalidArgument:
logger.error('Failed to connect to bot channel webhook.')
else:
message = '$clear '+str(id)
# Executing webhook.
webhook.send(content=message)


@shared_task
def create_discord_lfg_roles():
"""
Celery task to send a create roles command message to a target Discord server.
- Command used by the ACM-FSU/pcs-bot
"""

try:
webhook = Webhook.from_url(
BOT_CHANNEL_WEBHOOK_URL, adapter=RequestsWebhookAdapter())
except InvalidArgument:
logger.error('Failed to connect to bot channel webhook.')
else:
message = '$create_roles'
# Executing webhook.
webhook.send(content=message)


@shared_task
def remove_all_discord_lfg_roles():
"""
Celery task to send a remove roles command message to a target Discord server.
- Command used by the ACM-FSU/pcs-bot
"""

try:
webhook = Webhook.from_url(
BOT_CHANNEL_WEBHOOK_URL, adapter=RequestsWebhookAdapter())
except InvalidArgument:
logger.error('Failed to connect to bot channel webhook.')
else:
message = '$remove_all_roles'
# Executing webhook.
webhook.send(content=message)
logger.error("No Team objects exist in database.")
Loading