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 argparse |
| #2 | import logging |
| #3 | import os |
| #4 | |
| #5 | from embedchain.helpers.json_serializable import register_deserializable |
| #6 | |
| #7 | from .base import BaseBot |
| #8 | |
| #9 | try: |
| #10 | import discord |
| #11 | from discord import app_commands |
| #12 | from discord.ext import commands |
| #13 | except ModuleNotFoundError: |
| #14 | raise ModuleNotFoundError( |
| #15 | "The required dependencies for Discord are not installed." "Please install with `pip install discord==2.3.2`" |
| #16 | ) from None |
| #17 | |
| #18 | |
| #19 | logger = logging.getLogger(__name__) |
| #20 | |
| #21 | intents = discord.Intents.default() |
| #22 | intents.message_content = True |
| #23 | client = discord.Client(intents=intents) |
| #24 | tree = app_commands.CommandTree(client) |
| #25 | |
| #26 | # Invite link example |
| #27 | # https://discord.com/api/oauth2/authorize?client_id={DISCORD_CLIENT_ID}&permissions=2048&scope=bot |
| #28 | |
| #29 | |
| #30 | @register_deserializable |
| #31 | class DiscordBot(BaseBot): |
| #32 | def __init__(self, *args, **kwargs): |
| #33 | BaseBot.__init__(self, *args, **kwargs) |
| #34 | |
| #35 | def add_data(self, message): |
| #36 | data = message.split(" ")[-1] |
| #37 | try: |
| #38 | self.add(data) |
| #39 | response = f"Added data from: {data}" |
| #40 | except Exception: |
| #41 | logger.exception(f"Failed to add data {data}.") |
| #42 | response = "Some error occurred while adding data." |
| #43 | return response |
| #44 | |
| #45 | def ask_bot(self, message): |
| #46 | try: |
| #47 | response = self.query(message) |
| #48 | except Exception: |
| #49 | logger.exception(f"Failed to query {message}.") |
| #50 | response = "An error occurred. Please try again!" |
| #51 | return response |
| #52 | |
| #53 | def start(self): |
| #54 | client.run(os.environ["DISCORD_BOT_TOKEN"]) |
| #55 | |
| #56 | |
| #57 | # @tree decorator cannot be used in a class. A global discord_bot is used as a workaround. |
| #58 | |
| #59 | |
| #60 | @tree.command(name="question", description="ask embedchain") |
| #61 | async def query_command(interaction: discord.Interaction, question: str): |
| #62 | await interaction.response.defer() |
| #63 | member = client.guilds[0].get_member(client.user.id) |
| #64 | logger.info(f"User: {member}, Query: {question}") |
| #65 | try: |
| #66 | answer = discord_bot.ask_bot(question) |
| #67 | if args.include_question: |
| #68 | response = f"> {question}\n\n{answer}" |
| #69 | else: |
| #70 | response = answer |
| #71 | await interaction.followup.send(response) |
| #72 | except Exception as e: |
| #73 | await interaction.followup.send("An error occurred. Please try again!") |
| #74 | logger.error("Error occurred during 'query' command:", e) |
| #75 | |
| #76 | |
| #77 | @tree.command(name="add", description="add new content to the embedchain database") |
| #78 | async def add_command(interaction: discord.Interaction, url_or_text: str): |
| #79 | await interaction.response.defer() |
| #80 | member = client.guilds[0].get_member(client.user.id) |
| #81 | logger.info(f"User: {member}, Add: {url_or_text}") |
| #82 | try: |
| #83 | response = discord_bot.add_data(url_or_text) |
| #84 | await interaction.followup.send(response) |
| #85 | except Exception as e: |
| #86 | await interaction.followup.send("An error occurred. Please try again!") |
| #87 | logger.error("Error occurred during 'add' command:", e) |
| #88 | |
| #89 | |
| #90 | @tree.command(name="ping", description="Simple ping pong command") |
| #91 | async def ping(interaction: discord.Interaction): |
| #92 | await interaction.response.send_message("Pong", ephemeral=True) |
| #93 | |
| #94 | |
| #95 | @tree.error |
| #96 | async def on_app_command_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError) -> None: |
| #97 | if isinstance(error, commands.CommandNotFound): |
| #98 | await interaction.followup.send("Invalid command. Please refer to the documentation for correct syntax.") |
| #99 | else: |
| #100 | logger.error("Error occurred during command execution:", error) |
| #101 | |
| #102 | |
| #103 | @client.event |
| #104 | async def on_ready(): |
| #105 | # TODO: Sync in admin command, to not hit rate limits. |
| #106 | # This might be overkill for most users, and it would require to set a guild or user id, where sync is allowed. |
| #107 | await tree.sync() |
| #108 | logger.debug("Command tree synced") |
| #109 | logger.info(f"Logged in as {client.user.name}") |
| #110 | |
| #111 | |
| #112 | def start_command(): |
| #113 | parser = argparse.ArgumentParser(description="EmbedChain DiscordBot command line interface") |
| #114 | parser.add_argument( |
| #115 | "--include-question", |
| #116 | help="include question in query reply, otherwise it is hidden behind the slash command.", |
| #117 | action="store_true", |
| #118 | ) |
| #119 | global args |
| #120 | args = parser.parse_args() |
| #121 | |
| #122 | global discord_bot |
| #123 | discord_bot = DiscordBot() |
| #124 | discord_bot.start() |
| #125 | |
| #126 | |
| #127 | if __name__ == "__main__": |
| #128 | start_command() |
| #129 |