From ce462433c1caabb9ab4e044c96da154a64d0e05d Mon Sep 17 00:00:00 2001 From: Marvin Scham Date: Tue, 16 Aug 2022 21:00:17 +0200 Subject: [PATCH] Init --- .env | 2 ++ Dockerfile | 10 +++++++ discord_bot.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 discord_bot.py diff --git a/.env b/.env new file mode 100644 index 0000000..241f83b --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +TOKEN= +LOGLEVEL=INFO/ERROR \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..114b803 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3 +FROM gorialis/discord.py + +RUN pip install --upgrade python-dotenv + +WORKDIR /usr/src/bot + +COPY . . + +CMD ["python3", "discord_bot.py"] \ No newline at end of file diff --git a/discord_bot.py b/discord_bot.py new file mode 100644 index 0000000..235fd71 --- /dev/null +++ b/discord_bot.py @@ -0,0 +1,80 @@ +import os, discord, aiohttp +from discord.ext import commands +from dotenv import load_dotenv +import logging + +load_dotenv() +TOKEN = os.getenv("TOKEN") +LOGLEVEL = os.getenv("LOGLEVEL", "INFO").upper() + +log = logging.getLogger("discord") +log.setLevel(LOGLEVEL) +handler = logging.StreamHandler() +handler.setFormatter( + logging.Formatter("%(asctime)s : %(levelname)s : %(name)s : %(message)s") +) +log.addHandler(handler) + + +async def post_request(session, user, message): + url = "https://schmoekerei.com/mail.php" + + async with session.post( + url, + data={ + "username": user, + "email": "discord@schmoekerei.com", + "request": f"Anfrage über Discord: {message}", + }, + ) as response: + data = await response.text() + log.info(f"POST response: {data.splitlines()[0]}") + + +bot = commands.Bot( + command_prefix=".", + activity=discord.Activity(name=".request", type=discord.ActivityType.listening), +) + + +@bot.event +async def on_ready(): + log.info(f"Bot is ready! Loglevel: {LOGLEVEL}") + + +@bot.event +async def on_command_error(ctx, error): + if isinstance(error, discord.ext.commands.errors.MissingRole): + log.info("Denied {0.author} usage of {0.command}: Missing Role".format(ctx)) + await ctx.send( + f"Du brauchst zur Nutzung dieses Befehls die Rolle {error.missing_role}.", + delete_after=10.0, + ) + else: + log.info( + f"Command error occurred: User {ctx.author} / {error.__class__.__name__} : {error}" + ) + + +@bot.command() +@commands.has_role("Admin") +async def clear(ctx, amount=1): + """[Admin] Löscht x Nachrichten""" + log.info("Clearing {0} Messages".format(amount)) + await ctx.channel.purge(limit=amount) + + +@bot.command() +async def request(ctx, *args): + """Erstellt ein Support-Ticket mit deiner Nachricht.""" + if ctx.channel.name in ["requests", "system"]: + if " ".join(args) == "test": + await ctx.message.add_reaction("👍") + else: + async with aiohttp.ClientSession() as session: + await post_request(session, ctx.author, " ".join(args)) + + await ctx.message.add_reaction("✅") + + +bot.run(TOKEN)