Skip to content

Commit 776405d

Browse files
committed
add a very basic bot structure
1 parent f8a64de commit 776405d

File tree

9 files changed

+80
-1
lines changed

9 files changed

+80
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DEV_CMD=cd intbot && DJANGO_ENV="dev" uv run ./manage.py
1+
DEV_CMD=cd intbot && DJANGO_ENV="dev" uv run --env-file .env -- ./manage.py
22
TEST_CMD=cd intbot && DJANGO_SETTINGS_MODULE="intbot.settings" DJANGO_ENV="test" uv run pytest --nomigrations
33

44

@@ -21,6 +21,9 @@ migrations:
2121
test:
2222
$(TEST_CMD) -s -vv
2323

24+
bot:
25+
$(DEV_CMD) run_bot
26+
2427

2528
client/send_test_webhook:
2629
uv run client/send_test_webhook.py

intbot/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DISCORD_BOT_TOKEN='asdf'

intbot/core/bot/__init__.py

Whitespace-only changes.

intbot/core/bot/main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import discord
2+
from django.conf import settings
3+
from discord.ext import commands
4+
5+
intents = discord.Intents.default()
6+
intents.members = True
7+
intents.message_content = True
8+
9+
bot = commands.Bot(command_prefix="!", intents=intents)
10+
11+
12+
@bot.event
13+
async def on_ready():
14+
print(f"Bot is ready. Logged in as {bot.user}")
15+
16+
17+
@bot.command()
18+
async def ping(ctx):
19+
await ctx.send("Pong!")
20+
21+
22+
def run_bot():
23+
bot_token = settings.DISCORD_BOT_TOKEN
24+
bot.run(bot_token)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from core.bot.main import run_bot
2+
from django.core.management.base import BaseCommand
3+
4+
5+
class Command(BaseCommand):
6+
help = "Run the discord bot"
7+
8+
def handle(self, *args, **kwargs):
9+
run_bot()

intbot/intbot/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import os
14+
import warnings
1415
from pathlib import Path
1516

1617
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -123,6 +124,16 @@
123124

124125
WEBHOOK_INTERNAL_TOKEN = "dev-token"
125126

127+
# This is only needed if you end up running the bot locally, hence it
128+
# doesn't fail explicilty – however it does emit a warning.
129+
# Please check the documentation and/or current online guides how to get
130+
# one from the developer portal.
131+
# If you run it locally, you probably want to run it against your own test
132+
# bot and a test server.
133+
DISCORD_BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN", "")
134+
if not DISCORD_BOT_TOKEN:
135+
warnings.warn("DISCORD_BOT_TOKEN not set")
136+
126137
elif DJANGO_ENV == "test":
127138
DEBUG = True
128139
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]

intbot/tests/test_bot.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from unittest.mock import AsyncMock
2+
3+
import pytest
4+
from core.bot.main import ping
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_ping_command():
9+
# Mock context
10+
ctx = AsyncMock()
11+
12+
# Call the command
13+
await ping(ctx)
14+
15+
# Assert that the command sent the expected message
16+
ctx.send.assert_called_once_with("Pong!")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ dependencies = [
1515
"pytest-django>=4.9.0",
1616
"django-extensions>=3.2.3",
1717
"httpx>=0.28.1",
18+
"pytest-asyncio>=0.25.2",
1819
]

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)