update V1

This commit is contained in:
ChUrl
2021-06-01 23:36:29 +02:00
parent 70b72e9bbc
commit 948a700dfd
4 changed files with 19 additions and 21 deletions

37
bot.py
View File

@ -13,6 +13,7 @@ load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi
class QuizClient(discord.Client):
def __init__(self):
@ -40,10 +41,9 @@ class QuizClient(discord.Client):
self.matchers["scores$"] = self.show_scores
self.matchers["players$"] = self.show_players
### Voicelines
# Voicelines
### Helpers ------------------------------------------------------------------------------------
# Helpers ------------------------------------------------------------------------------------
def _help_text(self):
"""
@ -73,7 +73,7 @@ class QuizClient(discord.Client):
"""
return re.match(self.prefix_regex + matcher, message.content, re.IGNORECASE)
### Events -------------------------------------------------------------------------------------
# Events -------------------------------------------------------------------------------------
async def on_ready(self):
print(f"{self.user} (id: {self.user.id}) has connected to Discord!")
@ -92,7 +92,7 @@ class QuizClient(discord.Client):
await self.matchers[matcher](message)
break
### Commands -----------------------------------------------------------------------------------
# Commands -----------------------------------------------------------------------------------
async def help(self, message):
"""
@ -100,7 +100,6 @@ class QuizClient(discord.Client):
"""
await message.channel.send(self._help_text())
async def reset_quiz(self, message):
"""
Quiz, reset - Gesetzte Werte zurücksetzen (zum neu Initialisieren)
@ -115,14 +114,12 @@ class QuizClient(discord.Client):
await message.channel.send("Finished.")
def is_init(self):
return not (self.quiz == None or
self.channel == None or
self.quizmaster == None or
self.players == dict())
async def init_quiz(self, message):
"""
Quiz, init: [NAME] - Initialisiere ein neues Quiz.
@ -152,7 +149,8 @@ class QuizClient(discord.Client):
# Set self.players
await self.channel.send("Determining players:")
react_message = await self.channel.send("Hier mit individuellem Emoji reagieren, am Ende mit dem Haken bestätigen!")
react_message = await self.channel.send(
"Hier mit individuellem Emoji reagieren, am Ende mit dem Haken bestätigen!")
await react_message.add_reaction("")
def check_confirm_players(reaction, user):
@ -160,7 +158,7 @@ class QuizClient(discord.Client):
await self.wait_for("reaction_add", check=check_confirm_players)
react_message = discord.utils.get(client.cached_messages, id=react_message.id)
assert isinstance(react_message, Message), "This should be a Message!" # silence pyright
assert isinstance(react_message, Message), "This should be a Message!" # silence pyright
# Get players from emojis
for reaction in react_message.reactions:
@ -171,7 +169,7 @@ class QuizClient(discord.Client):
if user == self.quizmaster:
continue
self.players[reaction.emoji] = user # TODO: key value which order?
self.players[reaction.emoji] = user # TODO: key value which order?
# Send starting message
await self.channel.send("Quiz will start in channel \"" + self.channel.name + "\"")
@ -180,7 +178,6 @@ class QuizClient(discord.Client):
await self.channel.send(str(emoji) + ": " + str(player.display_name))
await self.channel.send("-" * 80)
async def run_quiz(self, message):
"""
Quiz, run - Starte das Quiz
@ -197,10 +194,10 @@ class QuizClient(discord.Client):
# post question to players
for player in self.players.values():
await player.send("Frage: **" + question + "**")
await player.send("**Frage:** " + question)
# post question to channel for confirmation
await self.channel.send("Frage: **" + question + "**")
await self.channel.send("**Frage:** " + question)
# wait for answers from all players
for player in self.players.values():
@ -222,9 +219,10 @@ class QuizClient(discord.Client):
# Antworten
await self.channel.send("**Antworten:**")
for emoji, player in self.players.items():
await self.channel.send(str(emoji) + ": " + str((await player.dm_channel.history(limit=1).flatten())[0].content))
await self.channel.send(
str(emoji) + ": " + str((await player.dm_channel.history(limit=1).flatten())[0].content))
amsg = await self.channel.send("Korrekte Antwort: " + answer)
amsg = await self.channel.send("**Korrekte Antwort:** " + answer)
await amsg.add_reaction("")
for emoji, player in self.players.items():
await amsg.add_reaction(emoji)
@ -235,7 +233,7 @@ class QuizClient(discord.Client):
await self.wait_for("reaction_add", check=check_confirm_points)
amsg = discord.utils.get(client.cached_messages, id=amsg.id)
assert isinstance(amsg, Message), "This should be a Message!" # silence pyright
assert isinstance(amsg, Message), "This should be a Message!" # silence pyright
turn_scores = list()
for reaction in amsg.reactions:
@ -257,7 +255,6 @@ class QuizClient(discord.Client):
await self.channel.send("Quiz vorbei!")
async def show_scores(self, message):
"""
Quiz, scores - Zeigt den aktuellen Punktestand
@ -270,7 +267,7 @@ class QuizClient(discord.Client):
await self.channel.send("Kein QuizMaster keine Punkte!")
return
# scores = [[A, B], [A], [B, C], ...]
# scores: [[A, B], [A], [B, C], ...]
flat_scores = [player for round in self.scores for player in round]
score_dict = dict()
for emoji, _ in self.players.items():
@ -280,7 +277,6 @@ class QuizClient(discord.Client):
for emoji, score in sorted(score_dict.items(), key=lambda item: item[1]):
await self.channel.send(str(emoji) + ": " + str(score) + " Punkte")
async def show_players(self, message):
"""
Quiz, players - Zeigt die Spielerliste
@ -297,5 +293,6 @@ class QuizClient(discord.Client):
for emoji, player in self.players.items():
await self.channel.send(str(emoji) + ": " + str(player.display_name))
client = QuizClient()
client.run(TOKEN)

View File

@ -10,7 +10,6 @@ class Quiz(object):
question = tuple(string.strip() for string in tuple(line.split(" ")))
self.questions.append(question)
def __iter__(self):
def questions_gen(questions):
current = 0

View File

@ -1,4 +1,5 @@
python-dotenv
rich
matplotlib
discord.py
pynacl

View File

@ -28,6 +28,7 @@ in
mkShell {
buildInputs = [
jetbrains.pycharm-professional
myPython
nodePackages.pyright # LSP
pipenv # Doom