LogoLogo
  • Welcome to Sandbloc Documentation
  • INTRODUCTION
    • What is Sandbloc?
  • INTEGRATIONS
    • AI Providers
    • Vector Stores
  • TEMPLATES
    • Sandbloc Templates
    • Discord
    • Telegram
    • Twitter
    • Future Templates
  • API REFERENCES
    • API Guide
    • Endpoints
  • SANDBLOC LANGUAGE
    • The Sandbloc Language (SOON)
Powered by GitBook
On this page
Export as PDF
  1. TEMPLATES

Discord

Deploy AI agents on Discord using Sandbloc’s modular templates. Automate messages, integrate AI models, and build smarter bots with minimal setup.

Sandbloc simplifies building and deploying AI agents for Discord. These templates provide a full-stack, modular solution to automate responses, moderate chats, and enhance community interaction with AI.

Whether you want to deploy a basic chatbot, automate repetitive tasks, or integrate advanced AI models, this guide will show you how to set up a fully functional AI agent using Sandbloc.


Step 1: Installation and Setup

Start by installing Sandbloc and its dependencies:

bashCopy code# Install Sandbloc
pip install sandbloc

# Install Discord API wrapper (discord.py)
pip install discord.py

# Install any AI provider SDK (e.g., OpenAI)
pip install openai

Step 2: Set Up Your Discord Bot

  1. Go to the Discord Developer Portal.

  2. Create a new application and navigate to the Bot tab.

  3. Add a bot, copy the token, and save it securely.

  4. Invite your bot to a server using the OAuth2 URL Generator (scope: bot, permissions: Send Messages).


Step 3: Complete Discord AI Agent Code

Here’s a fully modular example that listens to messages, processes them using OpenAI's GPT-4, and sends intelligent responses back to the same Discord channel.


Full Discord Agent Code

pythonCopy codeimport discord
from sandbloc import InputBlock, ProcessingBlock, OutputBlock
import asyncio

# Discord Bot Token
DISCORD_TOKEN = "YOUR_DISCORD_BOT_TOKEN"
CHANNEL_ID = 1234567890  # Replace with your Discord channel ID

# Initialize Discord Client
intents = discord.Intents.default()
intents.messages = True
client = discord.Client(intents=intents)

# Sandbloc Workflow Blocks
class DiscordAgent:
    def __init__(self):
        self.input_block = InputBlock("discord", token=DISCORD_TOKEN, channel_id=CHANNEL_ID)
        self.processing_block = ProcessingBlock("openai", model="gpt-4", api_key="YOUR_OPENAI_API_KEY")
        self.output_block = OutputBlock("discord_response", token=DISCORD_TOKEN)

    async def process_message(self, message_content):
        # Connect the Sandbloc blocks for input, processing, and output
        print(f"Processing message: {message_content}")

        # Input Block: Capture the message
        self.input_block.data = {"prompt": message_content}

        # Processing Block: Send the message to OpenAI's GPT-4
        response = self.processing_block.process()

        # Output Block: Send response back to Discord
        await self.output_block.send(response)

# Event: On Message
@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # Initialize Sandbloc agent and process message
    agent = DiscordAgent()
    await agent.process_message(message.content)
    await message.channel.send("Thinking... 🤖")

# Run the Discord Bot
client.run(DISCORD_TOKEN)

Step 4: Explanation of Code

  1. InputBlock: Captures messages from the Discord channel.

  2. ProcessingBlock: Uses OpenAI’s GPT-4 to process the message and generate a response.

  3. OutputBlock: Sends the AI-generated response back to the Discord channel.

  4. Discord Events: Listens to incoming messages (on_message) and triggers Sandbloc workflows.


Step 5: Run the Bot

Save the script as discord_ai_agent.py and run it:

bashCopy codepython discord_ai_agent.py

You should see a message like: We have logged in as YOUR_BOT_NAME

Now type something in your Discord channel, and the bot will respond intelligently using GPT-4 or the selected AI provider.


Extending the Template

Here are a few ideas for extending this template:

  1. Moderation Tool Filter out harmful messages and send alerts to moderators.

pythonCopy code# Replace ProcessingBlock with moderation logic
self.processing_block = ProcessingBlock("openai", model="gpt-4", prompt="Filter harmful content: {message}")
  1. Contextual Chatbot Store chat history in a vector store like Pinecone for better context-aware replies.

pythonCopy codefrom sandbloc import ProcessingBlock

vector_block = ProcessingBlock("pinecone", api_key="YOUR_PINECONE_API_KEY", index_name="chat_history")
self.processing_block >> vector_block
  1. Scheduled Announcements Automate reminders and updates in Discord at scheduled times.

pythonCopy codeawait self.output_block.send("Daily update: Don't forget to check Sandbloc's new templates!", schedule="daily 9:00 AM")

Use Cases for Discord Templates

  • Community Bots: AI-powered bots to answer FAQs, moderate content, or provide announcements.

  • Task Automation: Automate repetitive tasks, like scheduling reminders or pulling data.

  • Interactive Chatbots: Intelligent agents that provide natural, AI-driven responses to user queries.

  • Gaming and Utility Bots: Create tools for gaming stats, updates, and integrations.


Conclusion

With Sandbloc’s Discord templates, building AI-powered bots is fast, efficient, and fully customizable. Whether you’re automating tasks, improving user interactions, or deploying advanced AI tools, Sandbloc’s modular workflows give you everything you need to get started.

PreviousSandbloc TemplatesNextTelegram

Last updated 5 months ago

Page cover image