Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Discord bot (#605)

Ruslan Serebriakovc875d257cc3d
diff --git a/camel/bots/__init__.py b/camel/bots/__init__.py
new file mode 100644
index 000000000..0a95ed0d0
--- /dev/null
+++ b/camel/bots/__init__.py
@@ -0,0 +1,19 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from .discord_bot import DiscordBot
+
+__all__ = [
+ 'DiscordBot',
+]
diff --git a/camel/bots/discord_bot.py b/camel/bots/discord_bot.py
new file mode 100644
index 000000000..323e6d337
--- /dev/null
+++ b/camel/bots/discord_bot.py
@@ -0,0 +1,103 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+import os
+from typing import TYPE_CHECKING, List, Optional
+
+from camel.agents import ChatAgent
+from camel.messages import BaseMessage
+
+if TYPE_CHECKING:
+ from discord import Message
+
+
+class DiscordBot:
+ r"""Represents a Discord bot that is powered by an agent.
+
+ Attributes:
+ chat_agent (ChatAgent): Chat agent that will power the bot.
+ channel_ids (List[int], optional): The channel IDs that the bot will
+ listen to.
+ discord_token (str, optional): The bot token.
+ """
+
+ def __init__(
+ self,
+ chat_agent: ChatAgent,
+ channel_ids: Optional[List[int]] = None,
+ discord_token: Optional[str] = None,
+ ) -> None:
+ self.chat_agent = chat_agent
+ self.token = discord_token or os.getenv('DISCORD_TOKEN')
+ self.channel_ids = channel_ids
+
+ if not self.token:
+ raise ValueError(
+ "`DISCORD_TOKEN` not found in environment variables. Get it"
+ " here: `https://discord.com/developers/applications`."
+ )
+
+ try:
+ import discord
+ except ImportError:
+ raise ImportError(
+ "Please install `discord` first. You can install it by running"
+ " `python3 -m pip install -U discord.py`."
+ )
+ intents = discord.Intents.default()
+ intents.message_content = True
+ self.client = discord.Client(intents=intents)
+
+ # Register event handlers
+ self.client.event(self.on_ready)
+ self.client.event(self.on_message)
+
+ def run(self) -> None:
+ r"""Start the Discord bot using its token.
+
+ This method starts the Discord bot by running the client with the
+ provided token.
+ """
+ self.client.run(self.token) # type: ignore[arg-type]
+
+ async def on_ready(self) -> None:
+ r"""This method is called when the bot has successfully connected to
+ the Discord server.
+
+ It prints a message indicating that the bot has logged in and displays
+ the username of the bot.
+ """
+ print(f'We have logged in as {self.client.user}')
+
+ async def on_message(self, message: 'Message') -> None:
+ r"""Event handler for when a message is received.
+
+ Args:
+ message (discord.Message): The message object received.
+ """
+ if message.author == self.client.user:
+ return
+
+ if self.channel_ids and message.channel.id not in self.channel_ids:
+ return
+
+ if not self.client.user or not self.client.user.mentioned_in(message):
+ return
+
+ self.chat_agent.reset()
+
+ user_msg = BaseMessage.make_user_message(
+ role_name="User", content=message.content
+ )
+ assistant_response = self.chat_agent.step(user_msg)
+ await message.channel.send(assistant_response.msg.content)
diff --git a/examples/bots/discord_bot_example.py b/examples/bots/discord_bot_example.py
new file mode 100644
index 000000000..52d2b1e3d
--- /dev/null
+++ b/examples/bots/discord_bot_example.py
@@ -0,0 +1,32 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from camel.agents import ChatAgent
+from camel.bots.discord_bot import DiscordBot
+from camel.messages import BaseMessage
+
+
+def main(model=None) -> None:
+ assistant_sys_msg = BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content="You are a helpful assistant.",
+ )
+
+ agent = ChatAgent(assistant_sys_msg, model_type=model)
+ bot = DiscordBot(agent)
+ bot.run()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/poetry.lock b/poetry.lock
index 8d5436e00..cc2b46684 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -998,6 +998,26 @@ files = [
[package.extras]
graph = ["objgraph (>=1.7.2)"]
+[[package]]
+name = "discord-py"
+version = "2.3.2"
+description = "A Python wrapper for the Discord API"
+optional = true
+python-versions = ">=3.8.0"
+files = [
+ {file = "discord.py-2.3.2-py3-none-any.whl", hash = "sha256:9da4679fc3cb10c64b388284700dc998663e0e57328283bbfcfc2525ec5960a6"},
+ {file = "discord.py-2.3.2.tar.gz", hash = "sha256:4560f70f2eddba7e83370ecebd237ac09fbb4980dc66507482b0c0e5b8f76b9c"},
+]
+
+[package.dependencies]
+aiohttp = ">=3.7.4,<4"
+
+[package.extras]
+docs = ["sphinx (==4.4.0)", "sphinxcontrib-trio (==1.1.2)", "sphinxcontrib-websupport", "typing-extensions (>=4.3,<5)"]
+speed = ["Brotli", "aiodns (>=1.1)", "cchardet (==2.1.7)", "orjson (>=3.5.4)"]
+test = ["coverage[toml]", "pytest", "pytest-asyncio", "pytest-cov", "pytest-mock", "typing-extensions (>=4.3,<5)"]
+voice = ["PyNaCl (>=1.3.0,<1.6)"]
+
[[package]]
name = "distlib"
version = "0.3.8"
@@ -1074,22 +1094,22 @@ files = [
[[package]]
name = "duckduckgo-search"
-version = "6.1.5"
+version = "6.1.6"
description = "Search for words, documents, images, news, maps and text translation using the DuckDuckGo.com search engine."
optional = true
python-versions = ">=3.8"
files = [
- {file = "duckduckgo_search-6.1.5-py3-none-any.whl", hash = "sha256:f0a18fe5f20323ba6bb11865ce32d4520bb90086a6ae62f5da510865f5a7dca8"},
- {file = "duckduckgo_search-6.1.5.tar.gz", hash = "sha256:10e5c4d09a4243fd9d85007dc4fe637456c3c3995fd2e1e9b49ffd6f75bb0afb"},
+ {file = "duckduckgo_search-6.1.6-py3-none-any.whl", hash = "sha256:6139ab17579e96ca7c5ed9398365245a36ecca8e7432545e3115ef90a9304eb7"},
+ {file = "duckduckgo_search-6.1.6.tar.gz", hash = "sha256:42c83d58f4f1d717a580b89cc86861cbae59e46e75288243776c53349d006bf1"},
]
[package.dependencies]
click = ">=8.1.7"
-orjson = ">=3.10.3"
+orjson = ">=3.10.4"
pyreqwest-impersonate = ">=0.4.7"
[package.extras]
-dev = ["mypy (>=1.10.0)", "pytest (>=8.2.0)", "pytest-asyncio (>=0.23.6)", "ruff (>=0.4.4)"]
+dev = ["mypy (>=1.10.0)", "pytest (>=8.2.2)", "pytest-asyncio (>=0.23.7)", "ruff (>=0.4.8)"]
lxml = ["lxml (>=5.2.2)"]
[[package]]
@@ -2450,13 +2470,9 @@ files = [
{file = "lxml-5.2.2-cp36-cp36m-win_amd64.whl", hash = "sha256:edcfa83e03370032a489430215c1e7783128808fd3e2e0a3225deee278585196"},
{file = "lxml-5.2.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:28bf95177400066596cdbcfc933312493799382879da504633d16cf60bba735b"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3a745cc98d504d5bd2c19b10c79c61c7c3df9222629f1b6210c0368177589fb8"},
- {file = "lxml-5.2.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1b590b39ef90c6b22ec0be925b211298e810b4856909c8ca60d27ffbca6c12e6"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b336b0416828022bfd5a2e3083e7f5ba54b96242159f83c7e3eebaec752f1716"},
- {file = "lxml-5.2.2-cp37-cp37m-manylinux_2_28_aarch64.whl", hash = "sha256:c2faf60c583af0d135e853c86ac2735ce178f0e338a3c7f9ae8f622fd2eb788c"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_28_x86_64.whl", hash = "sha256:4bc6cb140a7a0ad1f7bc37e018d0ed690b7b6520ade518285dc3171f7a117905"},
- {file = "lxml-5.2.2-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:7ff762670cada8e05b32bf1e4dc50b140790909caa8303cfddc4d702b71ea184"},
{file = "lxml-5.2.2-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:57f0a0bbc9868e10ebe874e9f129d2917750adf008fe7b9c1598c0fbbfdde6a6"},
- {file = "lxml-5.2.2-cp37-cp37m-musllinux_1_2_aarch64.whl", hash = "sha256:a6d2092797b388342c1bc932077ad232f914351932353e2e8706851c870bca1f"},
{file = "lxml-5.2.2-cp37-cp37m-musllinux_1_2_x86_64.whl", hash = "sha256:60499fe961b21264e17a471ec296dcbf4365fbea611bf9e303ab69db7159ce61"},
{file = "lxml-5.2.2-cp37-cp37m-win32.whl", hash = "sha256:d9b342c76003c6b9336a80efcc766748a333573abf9350f4094ee46b006ec18f"},
{file = "lxml-5.2.2-cp37-cp37m-win_amd64.whl", hash = "sha256:b16db2770517b8799c79aa80f4053cd6f8b716f21f8aca962725a9565ce3ee40"},
@@ -3954,13 +3970,13 @@ files = [
[[package]]
name = "pydantic"
-version = "2.7.3"
+version = "2.7.4"
description = "Data validation using Python type hints"
optional = false
python-versions = ">=3.8"
files = [
- {file = "pydantic-2.7.3-py3-none-any.whl", hash = "sha256:ea91b002777bf643bb20dd717c028ec43216b24a6001a280f83877fd2655d0b4"},
- {file = "pydantic-2.7.3.tar.gz", hash = "sha256:c46c76a40bb1296728d7a8b99aa73dd70a48c3510111ff290034f860c99c419e"},
+ {file = "pydantic-2.7.4-py3-none-any.whl", hash = "sha256:ee8538d41ccb9c0a9ad3e0e5f07bf15ed8015b481ced539a1759d8cc89ae90d0"},
+ {file = "pydantic-2.7.4.tar.gz", hash = "sha256:0c84efd9548d545f63ac0060c1e4d39bb9b14db8b3c0652338aecc07b5adec52"},
]
[package.dependencies]
@@ -7335,16 +7351,16 @@ doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linke
test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", "jaraco.test", "more-itertools", "pytest (>=6,!=8.1.*)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-ignore-flaky", "pytest-mypy", "pytest-ruff (>=0.2.1)"]
[extras]
-all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "docx2txt", "duckduckgo-search", "googlemaps", "neo4j", "newspaper3k", "openapi-spec-validator", "opencv-python", "pillow", "prance", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "slack-sdk", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]
+all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "discord.py", "docx2txt", "duckduckgo-search", "googlemaps", "neo4j", "newspaper3k", "openapi-spec-validator", "opencv-python", "pillow", "prance", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "slack-sdk", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]
encoders = ["sentence-transformers"]
graph-storages = ["neo4j"]
huggingface-agent = ["accelerate", "datasets", "diffusers", "opencv-python", "sentencepiece", "soundfile", "torch", "transformers"]
retrievers = ["cohere", "rank-bm25"]
test = ["mock", "pytest", "pytest-asyncio"]
-tools = ["PyMuPDF", "beautifulsoup4", "docx2txt", "duckduckgo-search", "googlemaps", "newspaper3k", "openapi-spec-validator", "pillow", "prance", "pydub", "pygithub", "pyowm", "requests_oauthlib", "slack-sdk", "unstructured", "wikipedia", "wolframalpha"]
+tools = ["PyMuPDF", "beautifulsoup4", "discord.py", "docx2txt", "duckduckgo-search", "googlemaps", "newspaper3k", "openapi-spec-validator", "pillow", "prance", "pydub", "pygithub", "pyowm", "requests_oauthlib", "slack-sdk", "unstructured", "wikipedia", "wolframalpha"]
vector-databases = ["pymilvus", "qdrant-client"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.8.1,<3.12"
-content-hash = "30e5ed3148a38de70436af019c87f99516067ffdd2c8a3e172d723efed6a1402"
+content-hash = "99d3e2383e7f5667a52a5c015ffbc9384be61fa73e0ea39fb2a788cc6ee2cf9e"
diff --git a/pyproject.toml b/pyproject.toml
index 1e30bf253..636e43d36 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -68,6 +68,7 @@ pillow = { version = "^10.2.0", optional = true }
slack-sdk = { version = "^3.27.2", optional = true }
pydub = { version = "^0.25.1", optional = true }
pygithub = { version = "^2.3.0", optional = true }
+"discord.py" = { version = "^2.3.2", optional = true }
# encoders
sentence-transformers = { version = "^2.2.2", optional = true }
@@ -125,6 +126,7 @@ tools = [
"slack-sdk",
"pydub",
"pygithub",
+ "discord.py",
]
vector-databases = [
@@ -155,6 +157,7 @@ all = [
"beautifulsoup4",
"docx2txt",
"pygithub",
+ "discord.py",
"PyMuPDF",
"wikipedia",
"duckduckgo-search",
@@ -299,7 +302,7 @@ module = [
"pillow",
"slack-sdk",
"pydub",
- "pygithub"
-
+ "pygithub",
+ "discord.py"
]
ignore_missing_imports = true
\ No newline at end of file
diff --git a/test/bots/test_discord_bot.py b/test/bots/test_discord_bot.py
new file mode 100644
index 000000000..e20172e90
--- /dev/null
+++ b/test/bots/test_discord_bot.py
@@ -0,0 +1,170 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+import asyncio
+import unittest
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from camel.agents import ChatAgent
+from camel.bots.discord_bot import DiscordBot
+
+
+class TestDiscordBot(unittest.TestCase):
+ def setUp(self):
+ self.chat_agent_mock = MagicMock(spec=ChatAgent)
+ self.token = "fake_token"
+ self.channel_ids = [123, 456]
+
+ def test_init_token_provided_uses_provided_token(self):
+ bot = DiscordBot(self.chat_agent_mock, discord_token=self.token)
+ self.assertEqual(bot.token, self.token)
+
+ @patch('discord.Client')
+ def test_on_ready(self, mock_client_class):
+ # Setup a mock for the Client instance
+ mock_client = MagicMock()
+ user_mock = MagicMock()
+ user_mock.__str__.return_value = 'BotUser'
+ mock_client.user = user_mock
+
+ # Ensure the mock client class returns the mock client instance
+ mock_client_class.return_value = mock_client
+
+ # Initialize the bot with the mocked client
+ bot = DiscordBot(self.chat_agent_mock, discord_token=self.token)
+
+ async def test():
+ with patch('builtins.print') as mocked_print:
+ await bot.on_ready()
+ mocked_print.assert_called_with('We have logged in as BotUser')
+
+ asyncio.run(test())
+
+ @patch('discord.Client')
+ def test_on_message_ignores_own_messages(self, mock_client_class):
+ mock_client = MagicMock()
+ mock_user = MagicMock()
+ mock_client.user = mock_user
+
+ mock_client_class.return_value = mock_client
+
+ bot = DiscordBot(self.chat_agent_mock, discord_token=self.token)
+
+ message_mock = MagicMock()
+ message_mock.author = mock_user
+
+ # Make the send method an async function
+ message_mock.channel.send = AsyncMock()
+
+ async def test():
+ await bot.on_message(message_mock)
+ message_mock.channel.send.assert_not_called()
+
+ asyncio.run(test())
+
+ @patch('discord.Client')
+ def test_on_message_handles_channel_check(self, mock_client_class):
+ mock_client = MagicMock()
+ channel_ids = [123, 456]
+ mock_client.user = MagicMock()
+
+ mock_client_class.return_value = mock_client
+
+ bot = DiscordBot(
+ self.chat_agent_mock,
+ channel_ids=channel_ids,
+ discord_token=self.token,
+ )
+
+ message_mock = MagicMock()
+ message_mock.channel.id = 789 # Not in channel_ids
+
+ # Make the send method an async function
+ message_mock.channel.send = AsyncMock()
+
+ async def test():
+ await bot.on_message(message_mock)
+ message_mock.channel.send.assert_not_called()
+
+ asyncio.run(test())
+
+ @patch('discord.Client')
+ def test_on_message_sends_response_when_mentioned(self, mock_client_class):
+ mock_client = MagicMock()
+ channel_ids = [123, 456]
+ mock_client.user = MagicMock()
+
+ mock_client_class.return_value = mock_client
+
+ bot = DiscordBot(
+ self.chat_agent_mock,
+ channel_ids=channel_ids,
+ discord_token=self.token,
+ )
+
+ message_mock = MagicMock()
+ message_mock.channel.id = 123 # In channel_ids
+ message_mock.content = "Hello, @bot!"
+ message_mock.mentions = [] # Bot is not mentioned
+ mock_client.user.mentioned_in = MagicMock(return_value=True)
+
+ response_message = "Hello, human!"
+ self.chat_agent_mock.step.return_value = MagicMock(
+ msg=MagicMock(content=response_message)
+ )
+ # Make the send method an async function
+ message_mock.channel.send = AsyncMock()
+
+ async def test():
+ await bot.on_message(message_mock)
+ message_mock.channel.send.assert_called_once_with(response_message)
+
+ asyncio.run(test())
+
+ @patch('discord.Client')
+ def test_on_message_ignores_when_not_mentioned(self, mock_client_class):
+ mock_client = MagicMock()
+ channel_ids = [123, 456]
+ mock_client.user = MagicMock()
+
+ mock_client_class.return_value = mock_client
+
+ bot = DiscordBot(
+ self.chat_agent_mock,
+ channel_ids=channel_ids,
+ discord_token=self.token,
+ )
+
+ message_mock = MagicMock()
+ message_mock.channel.id = 123 # In channel_ids
+ message_mock.content = "Hello, bot!"
+ message_mock.mentions = [] # Bot is not mentioned
+ mock_client.user.mentioned_in = MagicMock(return_value=False)
+
+ response_message = "Hello, human!"
+ self.chat_agent_mock.step.return_value = MagicMock(
+ msg=MagicMock(content=response_message)
+ )
+ # Make the send method an async function
+ message_mock.channel.send = AsyncMock()
+
+ async def test():
+ await bot.on_message(message_mock)
+ message_mock.channel.send.assert_not_called()
+
+ asyncio.run(test())
+
+
+if __name__ == '__main__':
+ unittest.main()