Skip to content

Commit

Permalink
Refactor to a new format (#70)
Browse files Browse the repository at this point in the history
* Refactor the application code

* Bump to Python 3.10

* Fix issues with running in docker containers

* Set activity on start
  • Loading branch information
klaasnicolaas committed Oct 5, 2022
1 parent c1aeff3 commit 7119ef8
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 82 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BOT_TOKEN=""
BOT_DEV_TOKEN=""
OWNER_ID=""
TESTING="true"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim
FROM python:3.10-slim
LABEL Maintainer="Klaas Schoute"

WORKDIR /usr/src/bot
Expand Down
12 changes: 12 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from dotenv import load_dotenv

load_dotenv()

# Bot setup
VERSION = "0.2.0"
TESTING = os.getenv("TESTING", "false")
PREFIX = "!"
BOT_TOKEN = os.getenv("BOT_TOKEN", "")
BOT_DEV_TOKEN = os.getenv("BOT_DEV_TOKEN", "")
OWNER_ID = int(os.getenv("OWNER_ID"))
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ services:
dockerfile: Dockerfile
container_name: DDS-Bot-Dev
environment:
TOKEN: ${BOT_DEV_TOKEN}
BOT_TOKEN: ${BOT_DEV_TOKEN}
OWNER_ID: ${OWNER_ID}
restart: unless-stopped
127 changes: 49 additions & 78 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,57 @@
import discord
import os
import config

from discord.ext import commands
from dotenv import load_dotenv

import modules.quote as quote

VERSION = "0.2.0"

# load_dotenv reads from a file called .env in the same directory as the python files which should roughly look like BOT_TOKEN="1234567890"
load_dotenv()

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
print("--- Bot is online and ready to interact! ---")
print(f'Logged in as {bot.user} (ID: {bot.user.id})')

@bot.command()
async def inspire(ctx):
"""
command: !inspire
Get a inspiration quote
"""
inspire = quote.get_quote()
await ctx.reply(inspire)

@bot.command(name="hallo")
async def hello(ctx):
"""
command: !hallo
Say hello to the users
"""
await ctx.send('Hee hallo! Welkom op de server')

@bot.command(name="vliegavond")
async def trainings(ctx):
"""
command: !vliegavond
"""
await ctx.send('Een lijst van de aankomende vliegdata is te vinden op: https://dutchdronesquad.nl/trainingen en hier vind je ook uitleg, hoe je kan aanmelden voor een vliegavond.')

@bot.command()
async def track(ctx):
"""
command: !track
"""
await ctx.send('Informatie over de huidige racetrack kan je vinden op: https://dutchdronesquad.nl/racetrack, of verken de track alvast in Velocidrone.')

@bot.command()
@commands.has_permissions(administrator=True)
async def version(ctx):
"""
command: !version
admin only
"""
await ctx.send(f'Versie: {VERSION}')

@bot.event
async def on_message(message: discord.Message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username} in #{channel}: {user_message}')

if message.author == bot.user:
return

# msg = message.content

# if any(word in msg for word in sad_words):
# await message.reply(random.choice(happy_response))

# Leave this here, otherwise commands wil stop running
await bot.process_commands(message)
def main():
intents = discord.Intents.default()
intents.message_content = True

activity = discord.Activity(
type=discord.ActivityType.watching, name="de DDS server"
)

bot = commands.Bot(
command_prefix=commands.when_mentioned_or(config.PREFIX),
intents=intents,
activity=activity,
owner_id=config.OWNER_ID,
)

@bot.event
async def on_ready():
print("--- Bot is online and ready to interact! ---")
print(f'{bot.user.name} has connected to Discord (ID: {bot.user.id})')

@bot.event
async def on_message(message: discord.Message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username} in #{channel}: {user_message}')

if message.author == bot.user:
return

# msg = message.content

# if any(word in msg for word in sad_words):
# await message.reply(random.choice(happy_response))

# Leave this here, otherwise commands wil stop running
await bot.process_commands(message)

# Load all modules
for folder in os.listdir('modules'):
bot.load_extension(f'modules.{folder}')

# Run the bot
if config.TESTING == "true":
bot.run(config.BOT_DEV_TOKEN)
else:
bot.run(config.BOT_TOKEN)

# Run the bot
if __name__ == "__main__":
if os.getenv('TESTING') == "true":
bot.run(os.getenv('BOT_DEV_TOKEN'))
else:
bot.run(os.getenv('BOT_TOKEN'))
main()
Empty file removed modules/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions modules/racing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from discord.ext import commands

class Racing(commands.Cog, name="Racing"):
def __init__(self, bot: commands.Bot):
self.bot = bot

@commands.command(name="vliegavond")
async def trainings(self, ctx: commands.Context):
"""
command: !vliegavond
"""
await ctx.send('Een lijst van de aankomende vliegdata is te vinden op: https://dutchdronesquad.nl/trainingen en hier vind je ook uitleg, hoe je kan aanmelden voor een vliegavond.')

@commands.command(name="track")
async def track(self, ctx: commands.Context):
"""
command: !track
"""
await ctx.send('Informatie over de huidige racetrack kan je vinden op: https://dutchdronesquad.nl/racetrack, of verken de track alvast in Velocidrone.')

def setup(bot: commands.Bot):
bot.add_cog(Racing(bot))
37 changes: 37 additions & 0 deletions modules/system/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import config

from discord.ext import commands
from .quote import get_quote

class System(commands.Cog, name="System"):
def __init__(self, bot: commands.Bot):
self.bot = bot

@commands.command(name="hallo")
async def hello(self, ctx: commands.Context):
"""
command: !hallo
Say hello to the users
"""
await ctx.send('Hee hallo! Welkom op de server')

@commands.command(name="version")
@commands.has_permissions(administrator=True)
async def version(self, ctx: commands.Context):
"""
command: !version
admin only
"""
await ctx.send(f'Versie: {config.VERSION}')

@commands.command(name="inspire")
async def inspire(self, ctx: commands.Context):
"""
command: !inspire
Get a inspiration quote
"""
inspire = get_quote()
await ctx.reply(inspire)

def setup(bot: commands.Bot):
bot.add_cog(System(bot))
File renamed without changes.
6 changes: 4 additions & 2 deletions production.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ services:
discord-bot:
image: "ghcr.io/dutchdronesquad/dds-bot:latest"
environment:
TOKEN: ""
BOT_TOKEN: ""
OWNER_ID: ""
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
condition: on-failure
delay: 1m

0 comments on commit 7119ef8

Please sign in to comment.