From edbe2df9076b92f178776f751616a71d99b76cb4 Mon Sep 17 00:00:00 2001 From: Siphalor Date: Mon, 7 Dec 2020 17:30:47 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + .idea/.gitignore | 8 + .idea/discord.xml | 6 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 11 ++ .idea/modules.xml | 8 + .idea/och-bot.iml | 14 ++ Dockerfile | 2 + bot.py | 153 ++++++++++++++++++ config.json | 1 + 10 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/discord.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/och-bot.iml create mode 100644 Dockerfile create mode 100644 bot.py create mode 100644 config.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..4527cef --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../:\coding\python\och-bot\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..9079745 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..16541a3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f8cde07 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/och-bot.iml b/.idea/och-bot.iml new file mode 100644 index 0000000..8c4c2e2 --- /dev/null +++ b/.idea/och-bot.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2c9712d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,2 @@ +FROM python:3 +WORKDIR diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..96497f9 --- /dev/null +++ b/bot.py @@ -0,0 +1,153 @@ +import asyncio +import json +import os +import random +import re +from typing import Optional + +import discord +from discord.ext import commands +from dotenv import load_dotenv + +load_dotenv() +TOKEN = os.getenv('DISCORD_TOKEN') +CONFIG = 'config.json' + +PING_REGEX = re.compile(r'\b(?:ge)?ping', re.IGNORECASE) +TOPFIT_REGEX = re.compile(r'\b(topfit|fit|top|micro|virtual reality|vr|ä+h*m*|hä*)\b', re.IGNORECASE) +LOEH_REGEX = re.compile(r'Och L(?:oe|ö)h!?', re.IGNORECASE) + +TOPFIT_WORDS = ( + "äh", + "ähm", + "ääh", + "äääh", + "ä", + "..", + "...", + "Mi", + "Mic", + "Micro", + "Microsoft", + "Microservices", + "top", + "vortual reality" +) + +#LOEH_ID = 327126546970312739 +LOEH_ID = 254265844928872448 +SIP_ID = 327126546970312739 + +config: dict = { + 'loeh-timeout': 10, + 'prefix': 'och!' +} + + +def load_config() -> dict: + try: + config_file = open(CONFIG, 'r') + config.update(json.load(config_file)) + except (json.JSONDecodeError, FileNotFoundError): + pass + return config + + +def save_config(): + with open(CONFIG, 'w') as config_file: + json.dump(config, config_file) + + +load_config() +bot = commands.Bot(command_prefix=config.get('prefix')) + + +async def get_loeh(guild: discord.Guild) -> Optional[discord.Member]: + try: + return await guild.fetch_member(LOEH_ID) + except discord.NotFound: + return None + + +@bot.event +async def on_ready(): + print(f'{bot.user} has connected to Discord!') + + +@bot.event +async def on_message(message: discord.Message): + if not message.author.bot: + if LOEH_REGEX.match(message.content): + if message.author.id == LOEH_ID: + await message.channel.send("https://siphalor.de/img/spidy-is-that-you.jpg") + else: + loeh = await get_loeh(message.guild) + if loeh is None: + await message.channel.send('404: Löh not found!') + else: + try: + await loeh.edit(mute=True) + sleeper = asyncio.sleep(config.get('loeh-timeout')) + + message: Optional[discord.Message] = await message.channel.send('Zu Befehl!') + await sleeper + await loeh.edit(mute=False) + if message is not None: + await message.edit(content="~~Zu Befehl!~~\nEs sei ihm verziehen.") + except (discord.Forbidden, discord.HTTPException) as error: + await message.channel.send('Failed to complete your command, Sir') + return + elif PING_REGEX.search(message.content): + embed = discord.Embed( + title="*pinken, schwaches Verb*", + description="ein Netzwerkgerät testweise ansprechen.\nOft falsch geschrieben als pin__g__en" + ) + embed.add_field(name='ich', value='pinke') + embed.add_field(name='du', value='pinkst') + embed.add_field(name='er|sie|es', value='pinkt') + embed.add_field(name='wir', value='pinken') + embed.add_field(name='ihr', value='pinkt') + embed.add_field(name='sie', value='pinken') + embed.add_field(name='Partizip 2', value='gepinkt') + await message.channel.send(embed=embed) + else: + match = TOPFIT_REGEX.search(message.content) + if match is not None: + text = '' + for i in range(0, random.randint(7, 13)): + text += random.choice(TOPFIT_WORDS) + " " + text += match.group() + await message.channel.send(text) + await bot.process_commands(message) + + +@bot.command(name='config') +async def config_prefix_command(ctx: commands.Context, cmd: str = '', key: str = '', *, val: str = ''): + if ctx.author.id != SIP_ID: + await ctx.send('You\'re not allowed to touch my most valuable piece') + return + if cmd == '': + await ctx.send('Use `config get` or `config set` to query for or update config values. Use `config list` to ' + 'see all config options') + elif cmd == 'list': + await ctx.send('Available config options:\n' + ' - `prefix`: Set the bot prefix\n' + ' - `loeh-timeout`: The timeout for Löh') + elif cmd == 'get': + if key in config: + await ctx.send('`' + key + '` is set to ' + str(config.get(key))) + else: + await ctx.send('Unknown config option "' + key + '"') + elif cmd == 'set': + try: + if key == 'prefix': + config['prefix'] = val + elif key == 'loeh-timeout': + config['loeh-timeout'] = int(val) + save_config() + await ctx.send('Config successfully updated') + except (ValueError, TypeError) as error: + await ctx.send('Invalid value "' + val + '" for config `' + key + '`') + + +bot.run(TOKEN) diff --git a/config.json b/config.json new file mode 100644 index 0000000..5e6b007 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"loeh-timeout": 15, "prefix": "och!"} \ No newline at end of file