repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import hashlib |
| #2 | import logging |
| #3 | import os |
| #4 | |
| #5 | from embedchain.helpers.json_serializable import register_deserializable |
| #6 | from embedchain.loaders.base_loader import BaseLoader |
| #7 | |
| #8 | logger = logging.getLogger(__name__) |
| #9 | |
| #10 | |
| #11 | @register_deserializable |
| #12 | class DiscordLoader(BaseLoader): |
| #13 | """ |
| #14 | Load data from a Discord Channel ID. |
| #15 | """ |
| #16 | |
| #17 | def __init__(self): |
| #18 | if not os.environ.get("DISCORD_TOKEN"): |
| #19 | raise ValueError("DISCORD_TOKEN is not set") |
| #20 | |
| #21 | self.token = os.environ.get("DISCORD_TOKEN") |
| #22 | |
| #23 | @staticmethod |
| #24 | def _format_message(message): |
| #25 | return { |
| #26 | "message_id": message.id, |
| #27 | "content": message.content, |
| #28 | "author": { |
| #29 | "id": message.author.id, |
| #30 | "name": message.author.name, |
| #31 | "discriminator": message.author.discriminator, |
| #32 | }, |
| #33 | "created_at": message.created_at.isoformat(), |
| #34 | "attachments": [ |
| #35 | { |
| #36 | "id": attachment.id, |
| #37 | "filename": attachment.filename, |
| #38 | "size": attachment.size, |
| #39 | "url": attachment.url, |
| #40 | "proxy_url": attachment.proxy_url, |
| #41 | "height": attachment.height, |
| #42 | "width": attachment.width, |
| #43 | } |
| #44 | for attachment in message.attachments |
| #45 | ], |
| #46 | "embeds": [ |
| #47 | { |
| #48 | "title": embed.title, |
| #49 | "type": embed.type, |
| #50 | "description": embed.description, |
| #51 | "url": embed.url, |
| #52 | "timestamp": embed.timestamp.isoformat(), |
| #53 | "color": embed.color, |
| #54 | "footer": { |
| #55 | "text": embed.footer.text, |
| #56 | "icon_url": embed.footer.icon_url, |
| #57 | "proxy_icon_url": embed.footer.proxy_icon_url, |
| #58 | }, |
| #59 | "image": { |
| #60 | "url": embed.image.url, |
| #61 | "proxy_url": embed.image.proxy_url, |
| #62 | "height": embed.image.height, |
| #63 | "width": embed.image.width, |
| #64 | }, |
| #65 | "thumbnail": { |
| #66 | "url": embed.thumbnail.url, |
| #67 | "proxy_url": embed.thumbnail.proxy_url, |
| #68 | "height": embed.thumbnail.height, |
| #69 | "width": embed.thumbnail.width, |
| #70 | }, |
| #71 | "video": { |
| #72 | "url": embed.video.url, |
| #73 | "height": embed.video.height, |
| #74 | "width": embed.video.width, |
| #75 | }, |
| #76 | "provider": { |
| #77 | "name": embed.provider.name, |
| #78 | "url": embed.provider.url, |
| #79 | }, |
| #80 | "author": { |
| #81 | "name": embed.author.name, |
| #82 | "url": embed.author.url, |
| #83 | "icon_url": embed.author.icon_url, |
| #84 | "proxy_icon_url": embed.author.proxy_icon_url, |
| #85 | }, |
| #86 | "fields": [ |
| #87 | { |
| #88 | "name": field.name, |
| #89 | "value": field.value, |
| #90 | "inline": field.inline, |
| #91 | } |
| #92 | for field in embed.fields |
| #93 | ], |
| #94 | } |
| #95 | for embed in message.embeds |
| #96 | ], |
| #97 | } |
| #98 | |
| #99 | def load_data(self, channel_id: str): |
| #100 | """Load data from a Discord Channel ID.""" |
| #101 | import discord |
| #102 | |
| #103 | messages = [] |
| #104 | |
| #105 | class DiscordClient(discord.Client): |
| #106 | async def on_ready(self) -> None: |
| #107 | logger.info("Logged on as {0}!".format(self.user)) |
| #108 | try: |
| #109 | channel = self.get_channel(int(channel_id)) |
| #110 | if not isinstance(channel, discord.TextChannel): |
| #111 | raise ValueError( |
| #112 | f"Channel {channel_id} is not a text channel. " "Only text channels are supported for now." |
| #113 | ) |
| #114 | threads = {} |
| #115 | |
| #116 | for thread in channel.threads: |
| #117 | threads[thread.id] = thread |
| #118 | |
| #119 | async for message in channel.history(limit=None): |
| #120 | messages.append(DiscordLoader._format_message(message)) |
| #121 | if message.id in threads: |
| #122 | async for thread_message in threads[message.id].history(limit=None): |
| #123 | messages.append(DiscordLoader._format_message(thread_message)) |
| #124 | |
| #125 | except Exception as e: |
| #126 | logger.error(e) |
| #127 | await self.close() |
| #128 | finally: |
| #129 | await self.close() |
| #130 | |
| #131 | intents = discord.Intents.default() |
| #132 | intents.message_content = True |
| #133 | client = DiscordClient(intents=intents) |
| #134 | client.run(self.token) |
| #135 | |
| #136 | metadata = { |
| #137 | "url": channel_id, |
| #138 | } |
| #139 | |
| #140 | messages = str(messages) |
| #141 | |
| #142 | doc_id = hashlib.sha256((messages + channel_id).encode()).hexdigest() |
| #143 | |
| #144 | return { |
| #145 | "doc_id": doc_id, |
| #146 | "data": [ |
| #147 | { |
| #148 | "content": messages, |
| #149 | "meta_data": metadata, |
| #150 | } |
| #151 | ], |
| #152 | } |
| #153 |