|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2021-present Pycord Development |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +from typing import TYPE_CHECKING, Any |
| 26 | + |
| 27 | +from typing_extensions import Self, override |
| 28 | + |
| 29 | +from ..app.event_emitter import Event |
| 30 | +from ..raw_models import RawSoundboardSoundDeleteEvent |
| 31 | +from ..soundboard import SoundboardSound |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from ..app.state import ConnectionState |
| 35 | + |
| 36 | +__all__ = ( |
| 37 | + "SoundboardSounds", |
| 38 | + "GuildSoundboardSoundsUpdate", |
| 39 | + "GuildSoundboardSoundUpdate", |
| 40 | + "GuildSoundboardSoundCreate", |
| 41 | + "GuildSoundboardSoundDelete", |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +class SoundboardSounds(Event): |
| 46 | + __event_name__: str = "SOUNDBOARD_SOUNDS" |
| 47 | + |
| 48 | + def __init__(self, guild_id: int, sounds: list[SoundboardSound]) -> None: |
| 49 | + self.guild_id: int = guild_id |
| 50 | + self.sounds: list[SoundboardSound] = sounds |
| 51 | + |
| 52 | + @classmethod |
| 53 | + @override |
| 54 | + async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: |
| 55 | + guild_id = int(data["guild_id"]) |
| 56 | + sounds: list[SoundboardSound] = [] |
| 57 | + for sound_data in data["soundboard_sounds"]: |
| 58 | + sound = SoundboardSound(state=state, http=state.http, data=sound_data) |
| 59 | + await state.cache.store_sound(sound) |
| 60 | + sounds.append(sound) |
| 61 | + return cls(guild_id, sounds) |
| 62 | + |
| 63 | + |
| 64 | +class GuildSoundboardSoundsUpdate(Event): |
| 65 | + __event_name__: str = "GUILD_SOUNDBOARD_SOUNDS_UPDATE" |
| 66 | + |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + before_sounds: list[SoundboardSound], |
| 70 | + after_sounds: list[SoundboardSound], |
| 71 | + ) -> None: |
| 72 | + self.before: list[SoundboardSound] = before_sounds |
| 73 | + self.after: list[SoundboardSound] = after_sounds |
| 74 | + |
| 75 | + @classmethod |
| 76 | + @override |
| 77 | + async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: |
| 78 | + before_sounds: list[SoundboardSound] = [] |
| 79 | + after_sounds: list[SoundboardSound] = [] |
| 80 | + for sound_data in data["soundboard_sounds"]: |
| 81 | + after = SoundboardSound(state=state, http=state.http, data=sound_data) |
| 82 | + if before := await state.cache.get_sound(after.id): |
| 83 | + before_sounds.append(before) |
| 84 | + await state.cache.store_sound(after) |
| 85 | + after_sounds.append(after) |
| 86 | + |
| 87 | + if len(before_sounds) == len(after_sounds): |
| 88 | + return cls(before_sounds, after_sounds) |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +class GuildSoundboardSoundUpdate(Event): |
| 93 | + __event_name__: str = "GUILD_SOUNDBOARD_SOUND_UPDATE" |
| 94 | + |
| 95 | + def __init__(self, before: SoundboardSound, after: SoundboardSound) -> None: |
| 96 | + self.before: SoundboardSound = before |
| 97 | + self.after: SoundboardSound = after |
| 98 | + |
| 99 | + @classmethod |
| 100 | + @override |
| 101 | + async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: |
| 102 | + after = SoundboardSound(state=state, http=state.http, data=data) |
| 103 | + before = await state.cache.get_sound(after.id) |
| 104 | + await state.cache.store_sound(after) |
| 105 | + if before: |
| 106 | + return cls(before, after) |
| 107 | + return None |
| 108 | + |
| 109 | + |
| 110 | +class GuildSoundboardSoundCreate(Event): |
| 111 | + __event_name__: str = "GUILD_SOUNDBOARD_SOUND_CREATE" |
| 112 | + |
| 113 | + def __init__(self, sound: SoundboardSound) -> None: |
| 114 | + self.sound: SoundboardSound = sound |
| 115 | + |
| 116 | + @classmethod |
| 117 | + @override |
| 118 | + async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: |
| 119 | + sound = SoundboardSound(state=state, http=state.http, data=data) |
| 120 | + await state.cache.store_sound(sound) |
| 121 | + return cls(sound) |
| 122 | + |
| 123 | + |
| 124 | +class GuildSoundboardSoundDelete(Event): |
| 125 | + __event_name__: str = "GUILD_SOUNDBOARD_SOUND_DELETE" |
| 126 | + |
| 127 | + def __init__( |
| 128 | + self, sound: SoundboardSound | None, raw: RawSoundboardSoundDeleteEvent |
| 129 | + ) -> None: |
| 130 | + self.sound: SoundboardSound | None = sound |
| 131 | + self.raw: RawSoundboardSoundDeleteEvent = raw |
| 132 | + |
| 133 | + @classmethod |
| 134 | + @override |
| 135 | + async def __load__(cls, data: Any, state: "ConnectionState") -> Self | None: |
| 136 | + sound_id = int(data["sound_id"]) |
| 137 | + sound = await state.cache.get_sound(sound_id) |
| 138 | + if sound is not None: |
| 139 | + await state.cache.delete_sound(sound_id) |
| 140 | + raw = RawSoundboardSoundDeleteEvent(data) |
| 141 | + return cls(sound, raw) |
0 commit comments