This repository has been archived on 2024-08-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
discord-bot/discord_bot.py
2022-08-16 21:00:17 +02:00

81 lines
2.2 KiB
Python

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)