diff --git a/.gitignore b/.gitignore index bc8ec9a7a78447db9143e2dcbc8742e7b70ccba0..76c43dd7faee932eaa0afa803a265e01fadf5c01 100644 --- a/.gitignore +++ b/.gitignore @@ -120,3 +120,4 @@ GitHub.sublime-settings /tops.json /text_commands.json /news.json +/highscores.json diff --git a/.idea/misc.xml b/.idea/misc.xml index caee6171d367bfa01301c898c88046b2ed20449c..1917bfcf22e74f779ee91123dfed3d02ba4ed0ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ <component name="JavaScriptSettings"> <option name="languageLevel" value="ES6" /> </component> - <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (fernuni-bot)" project-jdk-type="Python SDK" /> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (strolly)" project-jdk-type="Python SDK" /> </project> \ No newline at end of file diff --git a/.idea/strolly.iml b/.idea/strolly.iml index 25a185e695239cbdab2d17b412f7aa9fafb5ffaa..2f48cc78245a9e83abe0a6a18208b06d2f3ff85c 100644 --- a/.idea/strolly.iml +++ b/.idea/strolly.iml @@ -4,7 +4,7 @@ <content url="file://$MODULE_DIR$"> <excludeFolder url="file://$MODULE_DIR$/venv" /> </content> - <orderEntry type="jdk" jdkName="Python 3.8 (fernuni-bot)" jdkType="Python SDK" /> + <orderEntry type="jdk" jdkName="Python 3.8 (strolly)" jdkType="Python SDK" /> <orderEntry type="sourceFolder" forTests="false" /> </component> </module> \ No newline at end of file diff --git a/bati_cog.py b/bati_cog.py deleted file mode 100644 index 4c81532529ddad81997632802458f9a9fc2b6e22..0000000000000000000000000000000000000000 --- a/bati_cog.py +++ /dev/null @@ -1,23 +0,0 @@ -import os -import random -from time import time, sleep - -from discord.ext import commands - - -class BatiCog(commands.Cog): - def __init__(self, bot): - self.bot = bot - self.bati_id = int(os.getenv("BATI_ID")) - self.last_bati = 0 - self.bati_probability = float(os.getenv("BATI_PROBABILITY")) - self.bati_delay = int(os.getenv("BATI_DELAY")) - - @commands.Cog.listener() - async def on_message(self, message): - if message.author.id == self.bati_id: - if random.random() < self.bati_probability and time() >= self.last_bati + (self.bati_delay * 3600): - sleep(random.random() * 2) - await message.channel.send("bati") - self.last_bati = time() - print(message.content) diff --git a/leaderboard.py b/leaderboard.py new file mode 100644 index 0000000000000000000000000000000000000000..7acff71903cc80bdc92256972bf2ada4d22d6a8b --- /dev/null +++ b/leaderboard.py @@ -0,0 +1,68 @@ +import json + +from discord.ext import commands + + +class Leaderboard(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.highscores = self.load() + + def load(self): + """ Load highscores from json file """ + highscore_file = open("highscores.json", mode="r") + return json.load(highscore_file) + + def save(self): + """ Save highscores to json file """ + highscore_file = open("highscores.json", mode="w") + json.dump(self.highscores, highscore_file) + + @commands.command(name="highscore") + async def cmd_highscore(self, ctx, score: int): + """ Add highscore for Dorfromantik leaderboard """ + + if highscore := self.highscores.get(str(ctx.author.id)): + self.highscores[str(ctx.author.id)] = max(highscore, score) + else: + self.highscores[str(ctx.author.id)] = score + self.save() + + await ctx.send(f"Vielen Dank für deine Einreichung. Du bist damit auf Platz {self.get_place(ctx.author.id)} der Rangliste gelandet.") + + + + @commands.command(name="romantikboard") + async def cmd_romantikboard(self, ctx): + place = 0 + max = 10 + ready = False + message = "```fix\nDorfromantik Leaderboard\n\n" + message += " {:^3} | {:^37} | {:^9}\n".format("#", "Name", "Punkte") + message += "-----|---------------------------------------|-----------\n" + for key, value in sorted(self.highscores.items(), key=lambda item: item[1], reverse=True): + try: + member = await ctx.guild.fetch_member(key) + place += 1 + + if place > max: + if ready: + break + elif str(ctx.author.id) != key: + continue + message += "{:>4} | {:<37} | {:>9}\n".format(str(place), + f"{member.display_name}#{member.discriminator}", value) + if str(ctx.author.id) == key: + ready = True + except: + pass + + message += "```" + await ctx.send(message) + + def get_place(self, id): + place = 0 + for key, value in sorted(self.highscores.items(), key=lambda item: item[1], reverse=True): + place += 1 + if key == str(id): + return place \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 52b4608833fbf8dc220c309246a2f36b9b530006..07ab343476a9cb4a474f91901beeca338df5b597 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,10 @@ -beautifulsoup4 -requests -discord.py==1.5.1 -python-dotenv==0.15.0 \ No newline at end of file +aiohttp==3.7.4.post0 +async-timeout==3.0.1 +attrs==20.3.0 +chardet==4.0.0 +discord.py==1.6.0 +idna==3.1 +multidict==5.1.0 +python-dotenv==0.16.0 +typing-extensions==3.7.4.3 +yarl==1.6.3 diff --git a/strolly.py b/strolly.py index 3b009f113c5758fc20964383c544fb0d8b2080c8..76eac1bc5e764e2c5db89c8da7af52bf250ac2a3 100644 --- a/strolly.py +++ b/strolly.py @@ -4,41 +4,26 @@ import discord from discord.ext import commands from dotenv import load_dotenv -from bati_cog import BatiCog from poll_cog import PollCog from roll_cog import RollCog +from leaderboard import Leaderboard # .env file is necessary in the same directory, that contains several strings. load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') ACTIVITY = os.getenv('DISCORD_ACTIVITY') -# HELP_FILE = os.getenv('DISCORD_HELP_FILE') intents = discord.Intents.default() intents.members = True bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(ACTIVITY), intents=intents) bot.add_cog(PollCog(bot)) bot.add_cog(RollCog(bot)) -bot.add_cog(BatiCog(bot)) - - -# @bot.command(name="help") -# async def cmd_help(ctx): -# """ Send help message as DM """ -# -# help_file = open(HELP_FILE, mode='r') -# help_dict = json.load(help_file) -# embed = discord.Embed.from_dict(help_dict) -# await utils.send_dm(ctx.author, "", embed=embed) +bot.add_cog(Leaderboard(bot)) @bot.event async def on_ready(): print("Client started!") - # channel = await bot.fetch_channel(682590504948334684) - # await channel.send("!poll \"Wie kluk bin ich?\" Sehr") - -# bot.run(TOKEN)