update V1
This commit is contained in:
37
bot.py
37
bot.py
@ -13,6 +13,7 @@ load_dotenv()
|
|||||||
TOKEN = os.getenv("DISCORD_TOKEN")
|
TOKEN = os.getenv("DISCORD_TOKEN")
|
||||||
GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi
|
GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi
|
||||||
|
|
||||||
|
|
||||||
class QuizClient(discord.Client):
|
class QuizClient(discord.Client):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -40,10 +41,9 @@ class QuizClient(discord.Client):
|
|||||||
self.matchers["scores$"] = self.show_scores
|
self.matchers["scores$"] = self.show_scores
|
||||||
self.matchers["players$"] = self.show_players
|
self.matchers["players$"] = self.show_players
|
||||||
|
|
||||||
### Voicelines
|
# Voicelines
|
||||||
|
|
||||||
|
# Helpers ------------------------------------------------------------------------------------
|
||||||
### Helpers ------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def _help_text(self):
|
def _help_text(self):
|
||||||
"""
|
"""
|
||||||
@ -73,7 +73,7 @@ class QuizClient(discord.Client):
|
|||||||
"""
|
"""
|
||||||
return re.match(self.prefix_regex + matcher, message.content, re.IGNORECASE)
|
return re.match(self.prefix_regex + matcher, message.content, re.IGNORECASE)
|
||||||
|
|
||||||
### Events -------------------------------------------------------------------------------------
|
# Events -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
print(f"{self.user} (id: {self.user.id}) has connected to Discord!")
|
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)
|
await self.matchers[matcher](message)
|
||||||
break
|
break
|
||||||
|
|
||||||
### Commands -----------------------------------------------------------------------------------
|
# Commands -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
async def help(self, message):
|
async def help(self, message):
|
||||||
"""
|
"""
|
||||||
@ -100,7 +100,6 @@ class QuizClient(discord.Client):
|
|||||||
"""
|
"""
|
||||||
await message.channel.send(self._help_text())
|
await message.channel.send(self._help_text())
|
||||||
|
|
||||||
|
|
||||||
async def reset_quiz(self, message):
|
async def reset_quiz(self, message):
|
||||||
"""
|
"""
|
||||||
Quiz, reset - Gesetzte Werte zurücksetzen (zum neu Initialisieren)
|
Quiz, reset - Gesetzte Werte zurücksetzen (zum neu Initialisieren)
|
||||||
@ -115,14 +114,12 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
await message.channel.send("Finished.")
|
await message.channel.send("Finished.")
|
||||||
|
|
||||||
|
|
||||||
def is_init(self):
|
def is_init(self):
|
||||||
return not (self.quiz == None or
|
return not (self.quiz == None or
|
||||||
self.channel == None or
|
self.channel == None or
|
||||||
self.quizmaster == None or
|
self.quizmaster == None or
|
||||||
self.players == dict())
|
self.players == dict())
|
||||||
|
|
||||||
|
|
||||||
async def init_quiz(self, message):
|
async def init_quiz(self, message):
|
||||||
"""
|
"""
|
||||||
Quiz, init: [NAME] - Initialisiere ein neues Quiz.
|
Quiz, init: [NAME] - Initialisiere ein neues Quiz.
|
||||||
@ -152,7 +149,8 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
# Set self.players
|
# Set self.players
|
||||||
await self.channel.send("Determining 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("✅")
|
await react_message.add_reaction("✅")
|
||||||
|
|
||||||
def check_confirm_players(reaction, user):
|
def check_confirm_players(reaction, user):
|
||||||
@ -160,7 +158,7 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
await self.wait_for("reaction_add", check=check_confirm_players)
|
await self.wait_for("reaction_add", check=check_confirm_players)
|
||||||
react_message = discord.utils.get(client.cached_messages, id=react_message.id)
|
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
|
# Get players from emojis
|
||||||
for reaction in react_message.reactions:
|
for reaction in react_message.reactions:
|
||||||
@ -171,7 +169,7 @@ class QuizClient(discord.Client):
|
|||||||
if user == self.quizmaster:
|
if user == self.quizmaster:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.players[reaction.emoji] = user # TODO: key value which order?
|
self.players[reaction.emoji] = user # TODO: key value which order?
|
||||||
|
|
||||||
# Send starting message
|
# Send starting message
|
||||||
await self.channel.send("Quiz will start in channel \"" + self.channel.name + "\"")
|
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(str(emoji) + ": " + str(player.display_name))
|
||||||
await self.channel.send("-" * 80)
|
await self.channel.send("-" * 80)
|
||||||
|
|
||||||
|
|
||||||
async def run_quiz(self, message):
|
async def run_quiz(self, message):
|
||||||
"""
|
"""
|
||||||
Quiz, run - Starte das Quiz
|
Quiz, run - Starte das Quiz
|
||||||
@ -197,10 +194,10 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
# post question to players
|
# post question to players
|
||||||
for player in self.players.values():
|
for player in self.players.values():
|
||||||
await player.send("Frage: **" + question + "**")
|
await player.send("**Frage:** " + question)
|
||||||
|
|
||||||
# post question to channel for confirmation
|
# post question to channel for confirmation
|
||||||
await self.channel.send("Frage: **" + question + "**")
|
await self.channel.send("**Frage:** " + question)
|
||||||
|
|
||||||
# wait for answers from all players
|
# wait for answers from all players
|
||||||
for player in self.players.values():
|
for player in self.players.values():
|
||||||
@ -222,9 +219,10 @@ class QuizClient(discord.Client):
|
|||||||
# Antworten
|
# Antworten
|
||||||
await self.channel.send("**Antworten:**")
|
await self.channel.send("**Antworten:**")
|
||||||
for emoji, player in self.players.items():
|
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("✅")
|
await amsg.add_reaction("✅")
|
||||||
for emoji, player in self.players.items():
|
for emoji, player in self.players.items():
|
||||||
await amsg.add_reaction(emoji)
|
await amsg.add_reaction(emoji)
|
||||||
@ -235,7 +233,7 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
await self.wait_for("reaction_add", check=check_confirm_points)
|
await self.wait_for("reaction_add", check=check_confirm_points)
|
||||||
amsg = discord.utils.get(client.cached_messages, id=amsg.id)
|
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()
|
turn_scores = list()
|
||||||
for reaction in amsg.reactions:
|
for reaction in amsg.reactions:
|
||||||
@ -257,7 +255,6 @@ class QuizClient(discord.Client):
|
|||||||
|
|
||||||
await self.channel.send("Quiz vorbei!")
|
await self.channel.send("Quiz vorbei!")
|
||||||
|
|
||||||
|
|
||||||
async def show_scores(self, message):
|
async def show_scores(self, message):
|
||||||
"""
|
"""
|
||||||
Quiz, scores - Zeigt den aktuellen Punktestand
|
Quiz, scores - Zeigt den aktuellen Punktestand
|
||||||
@ -270,7 +267,7 @@ class QuizClient(discord.Client):
|
|||||||
await self.channel.send("Kein QuizMaster keine Punkte!")
|
await self.channel.send("Kein QuizMaster keine Punkte!")
|
||||||
return
|
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]
|
flat_scores = [player for round in self.scores for player in round]
|
||||||
score_dict = dict()
|
score_dict = dict()
|
||||||
for emoji, _ in self.players.items():
|
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]):
|
for emoji, score in sorted(score_dict.items(), key=lambda item: item[1]):
|
||||||
await self.channel.send(str(emoji) + ": " + str(score) + " Punkte")
|
await self.channel.send(str(emoji) + ": " + str(score) + " Punkte")
|
||||||
|
|
||||||
|
|
||||||
async def show_players(self, message):
|
async def show_players(self, message):
|
||||||
"""
|
"""
|
||||||
Quiz, players - Zeigt die Spielerliste
|
Quiz, players - Zeigt die Spielerliste
|
||||||
@ -297,5 +293,6 @@ class QuizClient(discord.Client):
|
|||||||
for emoji, player in self.players.items():
|
for emoji, player in self.players.items():
|
||||||
await self.channel.send(str(emoji) + ": " + str(player.display_name))
|
await self.channel.send(str(emoji) + ": " + str(player.display_name))
|
||||||
|
|
||||||
|
|
||||||
client = QuizClient()
|
client = QuizClient()
|
||||||
client.run(TOKEN)
|
client.run(TOKEN)
|
||||||
|
|||||||
1
quiz.py
1
quiz.py
@ -10,7 +10,6 @@ class Quiz(object):
|
|||||||
question = tuple(string.strip() for string in tuple(line.split(" ")))
|
question = tuple(string.strip() for string in tuple(line.split(" ")))
|
||||||
self.questions.append(question)
|
self.questions.append(question)
|
||||||
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
def questions_gen(questions):
|
def questions_gen(questions):
|
||||||
current = 0
|
current = 0
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
python-dotenv
|
python-dotenv
|
||||||
rich
|
rich
|
||||||
|
matplotlib
|
||||||
discord.py
|
discord.py
|
||||||
pynacl
|
pynacl
|
||||||
|
|||||||
Reference in New Issue
Block a user