|
| 1 | +from shinobu.beacon.models import (user as beacon_user, channel as beacon_channel, server as beacon_server, |
| 2 | + member as beacon_member, webhook as beacon_webhook, message as beacon_message, |
| 3 | + messageable as beacon_messageable) |
| 4 | + |
| 5 | +class BeaconDriverUnsupported(Exception): |
| 6 | + def __init__(self): |
| 7 | + super().__init__("Driver does not support this feature.") |
| 8 | + |
| 9 | +class BeaconDriverWebhookCache: |
| 10 | + def __init__(self): |
| 11 | + self._data: dict = {} |
| 12 | + |
| 13 | + def store_webhook(self, webhook: beacon_webhook.BeaconWebhook): |
| 14 | + if webhook.id in self._data: |
| 15 | + raise KeyError("Webhook already in cache") |
| 16 | + |
| 17 | + self._data.update({webhook.id: webhook}) |
| 18 | + |
| 19 | + def store_webhooks(self, webhooks: list): |
| 20 | + for webhook in webhooks: |
| 21 | + self.store_webhook(webhook) |
| 22 | + |
| 23 | + def get_webhook(self, webhook_id: str): |
| 24 | + return self._data.get(webhook_id) |
| 25 | + |
| 26 | +class BeaconDriver: |
| 27 | + """A class representing a platform driver for the Beacon bridge protocol.""" |
| 28 | + |
| 29 | + def __init__(self, platform: str, bot): |
| 30 | + self._platform: str = platform |
| 31 | + self._webhooks: BeaconDriverWebhookCache = BeaconDriverWebhookCache() |
| 32 | + self._bot = bot |
| 33 | + |
| 34 | + @property |
| 35 | + def platform(self) -> str: |
| 36 | + """The platform the driver enables support for.""" |
| 37 | + return self._platform |
| 38 | + |
| 39 | + def get_user(self, user_id: str) -> beacon_user.BeaconUser: |
| 40 | + """Gets a user.""" |
| 41 | + raise BeaconDriverUnsupported() |
| 42 | + |
| 43 | + async def fetch_user(self, user_id: str) -> beacon_user.BeaconUser: |
| 44 | + """Fetches a user from the platform API.""" |
| 45 | + raise BeaconDriverUnsupported() |
| 46 | + |
| 47 | + def get_member(self, server: beacon_server.BeaconServer, member_id: str) -> beacon_member.BeaconMember: |
| 48 | + """Gets a member from a server.""" |
| 49 | + raise BeaconDriverUnsupported() |
| 50 | + |
| 51 | + def get_channel(self, server: beacon_server.BeaconServer, channel_id: str) -> beacon_channel.BeaconChannel: |
| 52 | + """Gets a channel from a server.""" |
| 53 | + raise BeaconDriverUnsupported() |
| 54 | + |
| 55 | + def get_server(self, server_id: str) -> beacon_server.BeaconServer: |
| 56 | + """Gets a server.""" |
| 57 | + raise BeaconDriverUnsupported() |
| 58 | + |
| 59 | + async def fetch_server(self, server_id: str) -> beacon_server.BeaconServer: |
| 60 | + """Fetches a server from the platform API.""" |
| 61 | + raise BeaconDriverUnsupported() |
| 62 | + |
| 63 | + def get_webhook(self, webhook_id: str) -> beacon_webhook.BeaconWebhook: |
| 64 | + """Gets a webhook.""" |
| 65 | + |
| 66 | + # Many libraries don't have a built-in webhook cache, so Beacon provides its own. |
| 67 | + # However, this method can still be overwritten as needed. |
| 68 | + |
| 69 | + return self._webhooks.get_webhook(webhook_id) |
| 70 | + |
| 71 | + async def fetch_webhook(self, webhook_id: str) -> beacon_webhook.BeaconWebhook: |
| 72 | + """Fetches a webhook from the platform API.""" |
| 73 | + |
| 74 | + # Preferrably this should call self._webhooks.store_webhook(webhook) after fetching the webhook |
| 75 | + # to store the webhook to cache, unless the library has a webhook cache of its own. |
| 76 | + |
| 77 | + raise BeaconDriverUnsupported() |
| 78 | + |
| 79 | + async def send(self, destination: beacon_messageable.BeaconMessageable, |
| 80 | + content: beacon_message.BeaconMessageContent, send_as: beacon_user.BeaconUser): |
| 81 | + """Sends a message to a given destination.""" |
| 82 | + raise BeaconDriverUnsupported() |
0 commit comments