Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

add configurations in DiscordBot

Malik Talha8c138e05ed07
diff --git a/examples/discord_api.py b/examples/discord_api.py
index 57c247ab6..e5b9b4a71 100644
--- a/examples/discord_api.py
+++ b/examples/discord_api.py
@@ -2,6 +2,7 @@ import os
from langchain_core.language_models.chat_models import BaseChatModel
from dotenv import load_dotenv
+from dataclasses import field
import discord
from discord.ext import commands
@@ -15,8 +16,18 @@ from browser_use import BrowserConfig
load_dotenv()
class DiscordBot(commands.Bot):
- def __init__(self, llm: BaseChatModel):
+ def __init__(
+ self,
+ llm: BaseChatModel,
+ prefix: str = "$bu",
+ ack: bool = False,
+ browser_config: BrowserConfig = BrowserConfig(headless=True)
+ ):
+
self.llm = llm
+ self.prefix = prefix.strip()
+ self.ack = ack
+ self.browser_config = browser_config
# Define intents.
intents = discord.Intents.default()
@@ -42,17 +53,18 @@ class DiscordBot(commands.Bot):
try:
if message.author == self.user: # Ignore the bot's messages
return
- if message.content.strip().startswith("$bu "):
- # try:
- # await message.reply(
- # "Starting browser use task...",
- # mention_author=True # Don't ping the user
- # )
- # except Exception as e:
- # print(f"Error sending start message: {e}")
+ if message.content.strip().startswith(f"{self.prefix} "):
+ if self.ack:
+ try:
+ await message.reply(
+ "Starting browser use task...",
+ mention_author=True # Don't ping the user
+ )
+ except Exception as e:
+ print(f"Error sending start message: {e}")
try:
- agent_message = await self.run_agent(message.content.replace("$bu ", "").strip())
+ agent_message = await self.run_agent(message.content.replace(f"{self.prefix} ", "").strip())
await message.channel.send(
content=f"{agent_message}",
reference=message,
@@ -72,13 +84,13 @@ class DiscordBot(commands.Bot):
async def run_agent(self, task: str) -> str:
try:
- # Browser configuration
- config = BrowserConfig(
- headless=True,
- disable_security=True
- )
- browser = Browser(config)
+ # # Browser configuration
+ # config = BrowserConfig(
+ # headless=self.headless,
+ # disable_security=True
+ # )
+ browser = Browser(config=self.browser_config)
agent = Agent(
task=(
task
diff --git a/examples/discord_example.py b/examples/discord_example.py
index 26310a8e1..aa7178b42 100644
--- a/examples/discord_example.py
+++ b/examples/discord_example.py
@@ -10,6 +10,8 @@ from pydantic import SecretStr
from langchain_google_genai import ChatGoogleGenerativeAI
from browser_use.agent.service import Agent
+from browser_use import BrowserConfig
+
from discord_api import DiscordBot
@@ -27,5 +29,10 @@ if not api_key:
llm = ChatGoogleGenerativeAI(model='gemini-2.0-flash-exp', api_key=SecretStr(api_key))
-bot = DiscordBot(llm=llm)
-bot.run(bot_token)
+bot = DiscordBot(
+ llm=llm,
+ prefix="$ta",
+ ack=True,
+ browser_config=BrowserConfig(headless=False)
+)
+bot.run(bot_token)
\ No newline at end of file