diff --git a/.gitignore b/.gitignore index f10862a..ad1ca0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /.env +/__pycache__/ +/geckodriver.log diff --git a/bot.py b/bot.py index c8e547c..a22162b 100644 --- a/bot.py +++ b/bot.py @@ -3,59 +3,160 @@ import os import re import random -import discord +from functools import reduce + from dotenv import load_dotenv +import discord +from discord import Intents +from discord.ext import commands + +from scraper import Girls + load_dotenv() TOKEN = os.getenv("DISCORD_TOKEN") GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi -client = discord.Client() + +class HeidiClient(discord.Client): + + def __init__(self): + super().__init__(intents=Intents.default(), status="Nur eine kann Germany's next Topmodel werden!") + + self.prefix = "Heidi, " + self.prefix_regex = "^" + self.prefix + + self.girls = Girls() # scraped model list + + self.triggers = {} # automatic actions + self.triggers[lambda m: m.author.nick in self.girls.get_in_names()] = self.autoreact_to_girls + + self.matchers = {} # react to messages + self.matchers["Hilfe$"] = self.show_help + self.matchers["Heidi!$"] = self.say_name + self.matchers["wer ist dabei\\?$"] = self.list_models_in + self.matchers["wer ist raus\\?$"] = self.list_models_out + self.matchers[".+, ja oder nein\\?$"] = self.magic_shell + self.matchers["wähle: (.+,)+"] = self.choose + self.matchers["gib Bild von .+"] = self.show_model_picture -@client.event -async def on_ready(): - print(f"{client.user} has connected to Discord!") + ### Helpers ------------------------------------------------------------------------------------ - guild = discord.utils.get(client.guilds, name=GUILD) - print(f"{client.user} is connected to the following guild:") - print(f"{guild.name} (id: {guild.id})") + def _help_text(self): + """ + Generate help-string from docstrings of matchers and triggers + """ + docstrings_triggers = [" - " + func.__doc__.strip() for func in self.triggers.values()] + docstrings_matchers = [" - " + func.__doc__.strip() for func in self.matchers.values()] + + response = "Präfix: \"" + self.prefix + "\" (mit Leerzeichen)\n" + response += "--------------------------------------------------\n" + + response += "Das mache ich automatisch:\n" + response += "\n".join(docstrings_triggers) + + response += "\n\nIch höre auf diese Befehle:\n" + response += "\n".join(docstrings_matchers) + + return response -heidis_girls = ["Ana", "Soulin", "Alysha", "Luca", "Maria"] - -cmd_prefix = "^Heidi, " -cmd_listing = {"HEIDI!": "Ich sage enthusiastisch meinen Namen", "*?": "Ich beantworte eine Frage"} + def _match(self, matcher, message): + """ + Check if a string matches against prefix + matcher (case-insensitive) + """ + return re.match(self.prefix_regex + matcher, message.content, re.IGNORECASE) -@client.event -async def on_message(message): - if message.author == client.user: - return + ### Events ------------------------------------------------------------------------------------- - ### Passive Actions ---------------------------------------------------------------------------- + async def on_ready(self): + print(f"{self.user} (id: {self.user.id}) has connected to Discord!") + + + async def on_message(self, message): + if message.author == client.user: + return + + + for trigger in self.triggers: + if trigger(message): + await self.triggers[trigger](message) + break + + + for matcher in self.matchers: + if self._match(matcher, message): + await self.matchers[matcher](message) + break - # React to girls message - if message.author.nick in heidis_girls: - await message.add_reaction("❤") ### Commands ----------------------------------------------------------------------------------- - # Help: Heidi, Hilfe - if re.match(cmd_prefix + "Hilfe$", message.content): - response = "Befehle für Heidi:\n" + str(cmd_listing) - await message.channel.send(response) - - # Say my name: Heidi, Heidi! - elif re.match(cmd_prefix + "Heidi!$", message.content): - response = "HEIDI!" - await message.channel.send(response) - - # Magic Conch Shell - elif re.match(cmd_prefix + ".+\\?$", message.content): - choices = ["Ja!", "Jo.", "Total!", "Hab ich selbst gesehen!", "Nein!", "Nö.", "Nä.", "Niemals!", "Twitch Prime?"] - response = choices[random.randint(0, len(choices) - 1)] - await message.channel.send(response) + async def show_help(self, message): + """ + Hilfe (Ich höre auf diese Befehle, Senpai UwU) + """ + await message.channel.send(self._help_text()) + async def say_name(self, message): + """ + Heidi! (Ich sag meinen Namen) + """ + await message.channel.send("HEIDI!") + + + async def list_models_in(self, message): + """ + wer ist dabei? (Liste der Models welche noch GNTM werden können) + """ + await message.channel.send("\n".join(self.girls.get_in_names())) + + + async def list_models_out(self, message): + """ + wer ist raus? (Liste der Keks welche ich ge*ickt hab) + """ + await message.channel.send("\n".join(self.girls.get_out_names())) + + + async def show_model_picture(self, message): + """ + gib Bild von (Zeigt ein Bild des entsprechenden Models) + """ + name = message.content.split()[-1] + picture = discord.Embed() + picture.set_image(url=self.girls.get_image(name)) + picture.set_footer(text=name) + await message.channel.send(embed=picture) + + + async def magic_shell(self, message): + """ + , ja oder nein? (Ich beantworte dir eine Frage) + """ + choices = ["Ja!", "Jo.", "Total!", "Natürlich.", "Nein!", "Nö.", "Nä.", "Niemals!"] + await message.channel.send(random.choice(choices)) + + + async def choose(self, message): + """ + wähle: