Update handling of "None"

This commit is contained in:
2023-12-09 15:48:19 +01:00
parent 13b3e9910a
commit 9d78352ea5

58
bot.py
View File

@ -5,7 +5,7 @@ from discord import app_commands
from discord.app_commands import Choice from discord.app_commands import Choice
from functools import reduce from functools import reduce
from dotenv import load_dotenv from dotenv import load_dotenv
from typing import Dict, List, Optional, Union from typing import Dict, List, Optional, Union, Callable, Any
# TODO: Reenable + extend textgen # TODO: Reenable + extend textgen
# from textgen import textgen # from textgen import textgen
@ -76,7 +76,7 @@ class HeidiClient(discord.Client):
# if the predicate is true, the action is performed # if the predicate is true, the action is performed
self.on_voice_state_triggers = { self.on_voice_state_triggers = {
lambda m, b, a: b.channel != a.channel lambda m, b, a: b.channel != a.channel
and a.channel != None and a.channel is not None
and isinstance(a.channel, discord.VoiceChannel): self._play_entrance_sound, and isinstance(a.channel, discord.VoiceChannel): self._play_entrance_sound,
} }
@ -112,7 +112,7 @@ class HeidiClient(discord.Client):
self.tree.copy_global_to(guild=TEST_GUILD) self.tree.copy_global_to(guild=TEST_GUILD)
await self.tree.sync(guild=TEST_GUILD) await self.tree.sync(guild=TEST_GUILD)
def update_to_default_user_config(self): def update_to_default_user_config(self) -> None:
""" """
Adds config keys to the config, if they don't exist yet. Adds config keys to the config, if they don't exist yet.
""" """
@ -125,7 +125,7 @@ class HeidiClient(discord.Client):
self.write_user_config() self.write_user_config()
def print_user_config(self): def print_user_config(self) -> None:
print("Read persistent configuration:\n") print("Read persistent configuration:\n")
for section in self.user_config.sections(): for section in self.user_config.sections():
@ -135,7 +135,7 @@ class HeidiClient(discord.Client):
print("") print("")
def write_user_config(self): def write_user_config(self) -> None:
if not os.path.exists(f"{CONFIGPATH}/{USERCONFIGNAME}"): if not os.path.exists(f"{CONFIGPATH}/{USERCONFIGNAME}"):
print(f"Error: {CONFIGPATH}/{USERCONFIGNAME} doesn't exist!") print(f"Error: {CONFIGPATH}/{USERCONFIGNAME} doesn't exist!")
return return
@ -179,14 +179,14 @@ class HeidiClient(discord.Client):
# await message.add_reaction("❤") # await message.add_reaction("❤")
@staticmethod @staticmethod
async def _autoreact_to_jeremy(message: discord.Message): async def _autoreact_to_jeremy(message: discord.Message) -> None:
""" """
🧀 Jeremy 🧀 Jeremy
""" """
await message.add_reaction("🧀") await message.add_reaction("🧀")
@staticmethod @staticmethod
async def _autoreact_to_kardashian(message: discord.Message): async def _autoreact_to_kardashian(message: discord.Message) -> None:
""" """
💄 Kardashian 💄 Kardashian
""" """
@ -197,12 +197,12 @@ class HeidiClient(discord.Client):
member: discord.Member, member: discord.Member,
before: discord.VoiceState, before: discord.VoiceState,
after: discord.VoiceState, after: discord.VoiceState,
): ) -> None:
soundpath: Union[str, None] = self.user_config["ENTRANCE.SOUND"].get( soundpath: Union[str, None] = self.user_config["ENTRANCE.SOUND"].get(
member.name, None member.name, None
) )
if soundpath == None: if soundpath is None:
print(f"User {member.name} has not set an entrance sound") print(f"User {member.name} has not set an entrance sound")
return return
@ -233,15 +233,15 @@ client = HeidiClient(intents=intents)
@client.event @client.event
async def on_ready(): async def on_ready() -> None:
if client.user != None: if client.user is not None:
print(f"{client.user} (id: {client.user.id}) has connected to Discord!") print(f"{client.user} (id: {client.user.id}) has connected to Discord!")
else: else:
print("client.user is None!") print("client.user is None!")
@client.event @client.event
async def on_message(message: discord.Message): async def on_message(message: discord.Message) -> None:
# Skip Heidis own messages # Skip Heidis own messages
if message.author == client.user: if message.author == client.user:
return return
@ -258,7 +258,7 @@ async def on_message(message: discord.Message):
@client.event @client.event
async def on_voice_state_update( async def on_voice_state_update(
member: discord.Member, before: discord.VoiceState, after: discord.VoiceState member: discord.Member, before: discord.VoiceState, after: discord.VoiceState
): ) -> None:
# Skip Heidis own voice state updates (e.g. on /say) # Skip Heidis own voice state updates (e.g. on /say)
if member._user == client.user: if member._user == client.user:
return return
@ -267,7 +267,7 @@ async def on_voice_state_update(
# python iterates over the keys of a map # python iterates over the keys of a map
for predicate in client.on_voice_state_triggers: for predicate in client.on_voice_state_triggers:
if predicate(member, before, after): if predicate(member, before, after):
action = client.on_voice_state_triggers[predicate] action: Callable = client.on_voice_state_triggers[predicate]
print(f"on_voice_state_update: calling {action.__name__}") print(f"on_voice_state_update: calling {action.__name__}")
await action(member, before, after) await action(member, before, after)
@ -338,7 +338,7 @@ def user_entrance_sound_autocomplete(
@app_commands.autocomplete(config_value=user_config_value_autocomplete) @app_commands.autocomplete(config_value=user_config_value_autocomplete)
async def user_config( async def user_config(
interaction: discord.Interaction, config_key: str, config_value: str interaction: discord.Interaction, config_key: str, config_value: str
): ) -> None:
# Only Members can set settings # Only Members can set settings
if not isinstance(interaction.user, discord.Member): if not isinstance(interaction.user, discord.Member):
print("User not a member") print("User not a member")
@ -362,7 +362,7 @@ async def user_config(
name="giblinkbruder", name="giblinkbruder",
description="Heidi hilft mit dem Link zu deiner Lieblingsshow im Qualitätsfernsehen.", description="Heidi hilft mit dem Link zu deiner Lieblingsshow im Qualitätsfernsehen.",
) )
async def show_link(interaction: discord.Interaction): async def show_link(interaction: discord.Interaction) -> None:
link_pro7 = "https://www.prosieben.de/tv/germanys-next-topmodel/livestream" link_pro7 = "https://www.prosieben.de/tv/germanys-next-topmodel/livestream"
link_joyn = "https://www.joyn.de/serien/germanys-next-topmodel" link_joyn = "https://www.joyn.de/serien/germanys-next-topmodel"
@ -372,7 +372,7 @@ async def show_link(interaction: discord.Interaction):
@client.tree.command(name="heidi", description="Heidi!") @client.tree.command(name="heidi", description="Heidi!")
async def heidi_exclaim(interaction: discord.Interaction): async def heidi_exclaim(interaction: discord.Interaction) -> None:
messages = [ messages = [
"Die sind doch fast 18!", "Die sind doch fast 18!",
"Heidi!", "Heidi!",
@ -388,7 +388,7 @@ async def heidi_exclaim(interaction: discord.Interaction):
@client.tree.command(name="miesmuschel", description="Was denkt Heidi?") @client.tree.command(name="miesmuschel", description="Was denkt Heidi?")
@app_commands.rename(question="frage") @app_commands.rename(question="frage")
@app_commands.describe(question="Heidi wird es beantworten!") @app_commands.describe(question="Heidi wird es beantworten!")
async def magic_shell(interaction: discord.Interaction, question: str): async def magic_shell(interaction: discord.Interaction, question: str) -> None:
choices = [ choices = [
"Ja!", "Ja!",
"Jo.", "Jo.",
@ -412,7 +412,7 @@ async def magic_shell(interaction: discord.Interaction, question: str):
@app_commands.describe(option_a="Ist es vielleicht dies?") @app_commands.describe(option_a="Ist es vielleicht dies?")
@app_commands.rename(option_b="oder") @app_commands.rename(option_b="oder")
@app_commands.describe(option_b="Oder doch eher das?") @app_commands.describe(option_b="Oder doch eher das?")
async def choose(interaction: discord.Interaction, option_a: str, option_b: str): async def choose(interaction: discord.Interaction, option_a: str, option_b: str) -> None:
options = [option_a.strip(), option_b.strip()] options = [option_a.strip(), option_b.strip()]
await interaction.response.send_message( await interaction.response.send_message(
f"{options[0]} oder {options[1]}?\nHeidi sagt: {random.choice(options)}" f"{options[0]} oder {options[1]}?\nHeidi sagt: {random.choice(options)}"
@ -488,7 +488,7 @@ async def sound_autocomplete(
@app_commands.describe(sound="Was soll Heidi sagen?") @app_commands.describe(sound="Was soll Heidi sagen?")
@app_commands.autocomplete(board=board_autocomplete) @app_commands.autocomplete(board=board_autocomplete)
@app_commands.autocomplete(sound=sound_autocomplete) @app_commands.autocomplete(sound=sound_autocomplete)
async def say_voiceline(interaction: discord.Interaction, board: str, sound: str): async def say_voiceline(interaction: discord.Interaction, board: str, sound: str) -> None:
# Only Members can access voice channels # Only Members can access voice channels
if not isinstance(interaction.user, discord.Member): if not isinstance(interaction.user, discord.Member):
print("User not a member") print("User not a member")
@ -507,7 +507,7 @@ async def say_voiceline(interaction: discord.Interaction, board: str, sound: str
@client.tree.context_menu(name="beleidigen") @client.tree.context_menu(name="beleidigen")
async def insult( async def insult(
interaction: discord.Interaction, member: discord.Member interaction: discord.Interaction, member: discord.Member
): # with message: discord.Message this can be called on a message ) -> None: # with message: discord.Message this can be called on a message
if not member.dm_channel: if not member.dm_channel:
await member.create_dm() await member.create_dm()
@ -546,18 +546,18 @@ async def play_voice_line(
voice_channel: discord.VoiceChannel, voice_channel: discord.VoiceChannel,
board: str, board: str,
sound: str, sound: str,
): ) -> None:
try: try:
open(f"{SOUNDDIR}/{board}/{sound}.mkv") open(f"{SOUNDDIR}/{board}/{sound}.mkv")
except IOError: except IOError:
print("Error: Invalid soundfile!") print("Error: Invalid soundfile!")
if interaction != None: if interaction is not None:
await interaction.response.send_message( await interaction.response.send_message(
f'Heidi sagt: "{board}/{sound}" kanninich finden bruder' f'Heidi sagt: "{board}/{sound}" kanninich finden bruder'
) )
return return
if interaction != None: if interaction is not None:
await interaction.response.send_message(f'Heidi sagt: "{board}/{sound}"') await interaction.response.send_message(f'Heidi sagt: "{board}/{sound}"')
audio_source = discord.FFmpegPCMAudio( audio_source = discord.FFmpegPCMAudio(
@ -577,16 +577,16 @@ async def play_voice_line_for_member(
member: discord.Member, member: discord.Member,
board: str, board: str,
sound: str, sound: str,
): ) -> None:
# Member needs to be in voice channel to hear audio (Heidi needs to know the channel to join) # Member needs to be in voice channel to hear audio (Heidi needs to know the channel to join)
if ( if (
member == None member is None
or member.voice == None or member.voice is None
or member.voice.channel == None or member.voice.channel is None
or not isinstance(member.voice.channel, discord.VoiceChannel) or not isinstance(member.voice.channel, discord.VoiceChannel)
): ):
print("User not in (valid) voice channel!") print("User not in (valid) voice channel!")
if interaction != None: if interaction is not None:
await interaction.response.send_message("Heidi sagt: Komm in den Channel!") await interaction.response.send_message("Heidi sagt: Komm in den Channel!")
return return