Skip to content
Snippets Groups Projects
Commit da3392f5 authored by Dennis Klein's avatar Dennis Klein
Browse files

Added highscores and leaderboard for Dorfromantik game

parent 1926b840
No related branches found
No related tags found
No related merge requests found
......@@ -120,3 +120,4 @@ GitHub.sublime-settings
/tops.json
/text_commands.json
/news.json
/highscores.json
......@@ -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
......@@ -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
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)
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
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
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment