Discord
This notebook provides a quick overview for getting started with Discord tooling in langchain_discord. For more details on each tool and configuration, see the docstrings in your repository or relevant doc pages.
Overviewโ
Integration detailsโ
Class | Package | Serializable | JS support | Package latest |
---|---|---|---|---|
DiscordReadMessages , DiscordSendMessage | langchain-discord-shikenso | N/A | TBD |
Tool featuresโ
DiscordReadMessages
: Reads messages from a specified channel.DiscordSendMessage
: Sends messages to a specified channel.
Setupโ
The integration is provided by the langchain-discord-shikenso
package. Install it as follows:
%pip install --quiet -U langchain-discord-shikenso
Credentialsโ
This integration requires you to set DISCORD_BOT_TOKEN
as an environment variable to authenticate with the Discord API.
export DISCORD_BOT_TOKEN="your-bot-token"
import getpass
import os
# Example prompt to set your token if not already set:
# if not os.environ.get("DISCORD_BOT_TOKEN"):
# os.environ["DISCORD_BOT_TOKEN"] = getpass.getpass("DISCORD Bot Token:\n")
You can optionally set up LangSmith for tracing or observability:
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
Instantiationโ
Below is an example showing how to instantiate the Discord tools in langchain_discord
. Adjust as needed for your specific usage.
from langchain_discord.tools.discord_read_messages import DiscordReadMessages
from langchain_discord.tools.discord_send_messages import DiscordSendMessage
read_tool = DiscordReadMessages()
send_tool = DiscordSendMessage()
# Example usage:
# response = read_tool({"channel_id": "1234567890", "limit": 5})
# print(response)
#
# send_result = send_tool({"message": "Hello from notebook!", "channel_id": "1234567890"})
# print(send_result)
Invocationโ
Direct invocation with argsโ
Below is a simple example of calling the tool with keyword arguments in a dictionary.
invocation_args = {"channel_id": "1234567890", "limit": 3}
response = read_tool(invocation_args)
response
Invocation with ToolCallโ
If you have a model-generated ToolCall
, pass it to tool.invoke()
in the format shown below.
tool_call = {
"args": {"channel_id": "1234567890", "limit": 2},
"id": "1",
"name": read_tool.name,
"type": "tool_call",
}
tool.invoke(tool_call)
Chainingโ
Below is a more complete example showing how you might integrate the DiscordReadMessages
and DiscordSendMessage
tools in a chain or agent with an LLM. This example assumes you have a function (like create_react_agent
) that sets up a LangChain-style agent capable of calling tools when appropriate.
# Example: Using Discord Tools in an Agent
from langgraph.prebuilt import create_react_agent
from langchain_discord.tools.discord_read_messages import DiscordReadMessages
from langchain_discord.tools.discord_send_messages import DiscordSendMessage
# 1. Instantiate or configure your language model
# (Replace with your actual LLM, e.g., ChatOpenAI(temperature=0))
llm = ...
# 2. Create instances of the Discord tools
read_tool = DiscordReadMessages()
send_tool = DiscordSendMessage()
# 3. Build an agent that has access to these tools
agent_executor = create_react_agent(llm, [read_tool, send_tool])
# 4. Formulate a user query that may invoke one or both tools
example_query = "Please read the last 5 messages in channel 1234567890"
# 5. Execute the agent in streaming mode (or however your code is structured)
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
# 6. Print out the model's responses (and any tool outputs) as they arrive
for event in events:
event["messages"][-1].pretty_print()
API referenceโ
See the docstrings in:
for usage details, parameters, and advanced configurations.
Relatedโ
- Tool conceptual guide
- Tool how-to guides