Skip to content

Commit 0ec583e

Browse files
authored
added ask cmd! and rename random to search..
1 parent d4eb408 commit 0ec583e

12 files changed

+410
-201
lines changed

LICENSE

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2024 AmitxD
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2024 AmitxD
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+111-111
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,111 @@
1-
2-
# BhaluBot Discord Bot (Under Development)
3-
4-
BhaluBot is a versatile Discord bot designed to elevate your server experience with interactive and entertaining features. From generating random images to providing insightful responses, BhaluBot is here to make your Discord server lively and engaging.
5-
6-
**Please note: This bot is currently under development. More commands will be added soon.**
7-
8-
## Installation:
9-
10-
1. **Clone the repository:**
11-
```
12-
git clone https://github.com/Amitminer/BhaluBot-PY.git
13-
cd BhaluBot
14-
```
15-
16-
2. **Install Dependencies:**
17-
```
18-
pip install -r requirements.txt
19-
```
20-
21-
4. **Setup:**
22-
23-
- **Configuration:**
24-
BhaluBot uses a `config.yml` file for configuration. Ensure the following settings are correctly configured in the `config.yml`
25-
26-
- **Environment Variables:**
27-
Create a `.env` file in the root directory to store your sensitive information. Here are the environment variables you can set in the `.env` file [Click me to see .env setup](https://github.com/Amitminer/BhaluBot-PY#environment-variables-env)
28-
29-
5. **Start the bot using:**
30-
31-
```bash
32-
./start.sh
33-
```
34-
or
35-
```bash
36-
./start.bat
37-
```
38-
39-
## Commands:
40-
41-
- **`+random (your query)`**
42-
43-
Generates a random image based on your query, for example:
44-
```
45-
+random supercar
46-
```
47-
BhaluBot will send you a random supercar image. Powered by Unsplash API.
48-
49-
- **`+spam (count) (message to spam)`**
50-
51-
Sends the specified message multiple times, for example:
52-
```
53-
+spam 5 hello
54-
```
55-
BhaluBot will send "hello" 5 times.
56-
57-
- **`+say (message)`**
58-
59-
Bot will send the specified message, for example:
60-
```
61-
+say Hello, everyone!
62-
```
63-
BhaluBot will send "Hello, everyone!"
64-
65-
- **`+shutdown`**
66-
67-
Shuts down the bot for maintenance. This command is owner-only.
68-
69-
## Configuration:
70-
71-
- **`config.yml`**
72-
73-
```yaml
74-
# Your bot prefix
75-
prefix: +
76-
```
77-
78-
## Environment Variables (`.env`):
79-
80-
Create a `.env` file in the root directory to store your sensitive information. Here are the environment variables you can set in the `.env` file:
81-
82-
- `DISCORD_BOT_TOKEN`: Your Discord bot token.
83-
- `OPENAI_API_KEY`: Your OpenAI API key. Get it from [OpenAI](https://platform.openai.com/).
84-
- `UNSPLASH_ACCESS_KEY`: Your Unsplash API key. Obtain it from [Unsplash](https://unsplash.com/developers).
85-
- `CHATGPT_ACCESS_TOKEN`: Your ChatGPT API access token.
86-
87-
Example `.env` file:
88-
89-
```plaintext
90-
DISCORD_BOT_TOKEN=your_discord_bot_token
91-
OPENAI_API_KEY=your_openai_api_key
92-
UNSPLASH_ACCESS_KEY=your_unsplash_access_key
93-
CHATGPT_ACCESS_TOKEN=your_chatgpt_access_token
94-
```
95-
96-
Make sure to replace `your_discord_bot_token`, `your_openai_api_key`, `your_unsplash_access_key`, and `your_chatgpt_access_token` with your actual tokens and keys.
97-
98-
These environment variables are crucial for the proper functioning of BhaluBot. Ensure that you keep this file secure and do not share these sensitive details publicly.
99-
100-
101-
## Contributing:
102-
103-
If you find a bug or want to suggest an improvement, please feel free to open an issue or submit a pull request. Your contributions are greatly appreciated!
104-
105-
## License:
106-
107-
Bhalu Bot is open-source software licensed under the [MIT License](LICENSE).
108-
109-
## Credits:
110-
111-
The Bhalu Discord Bot is created and maintained by [AmitxD](https://github.com/Amitminer), based on BhaluBot (php).
1+
2+
# BhaluBot Discord Bot (Under Development)
3+
4+
BhaluBot is a versatile Discord bot designed to elevate your server experience with interactive and entertaining features. From generating random images to providing insightful responses, BhaluBot is here to make your Discord server lively and engaging.
5+
6+
**Please note: This bot is currently under development. More commands will be added soon.**
7+
8+
## Installation:
9+
10+
1. **Clone the repository:**
11+
```
12+
git clone https://github.com/Amitminer/BhaluBot-PY.git
13+
cd BhaluBot
14+
```
15+
16+
2. **Install Dependencies:**
17+
```
18+
pip install -r requirements.txt
19+
```
20+
21+
4. **Setup:**
22+
23+
- **Configuration:**
24+
BhaluBot uses a `config.yml` file for configuration. Ensure the following settings are correctly configured in the `config.yml`
25+
26+
- **Environment Variables:**
27+
Create a `.env` file in the root directory to store your sensitive information. Here are the environment variables you can set in the `.env` file [Click me to see .env setup](https://github.com/Amitminer/BhaluBot-PY#environment-variables-env)
28+
29+
5. **Start the bot using:**
30+
31+
```bash
32+
./start.sh
33+
```
34+
or
35+
```bash
36+
./start.bat
37+
```
38+
39+
## Commands:
40+
41+
- **`+random (your query)`**
42+
43+
Generates a random image based on your query, for example:
44+
```
45+
+random supercar
46+
```
47+
BhaluBot will send you a random supercar image. Powered by Unsplash API.
48+
49+
- **`+spam (count) (message to spam)`**
50+
51+
Sends the specified message multiple times, for example:
52+
```
53+
+spam 5 hello
54+
```
55+
BhaluBot will send "hello" 5 times.
56+
57+
- **`+say (message)`**
58+
59+
Bot will send the specified message, for example:
60+
```
61+
+say Hello, everyone!
62+
```
63+
BhaluBot will send "Hello, everyone!"
64+
65+
- **`+shutdown`**
66+
67+
Shuts down the bot for maintenance. This command is owner-only.
68+
69+
## Configuration:
70+
71+
- **`config.yml`**
72+
73+
```yaml
74+
# Your bot prefix
75+
prefix: +
76+
```
77+
78+
## Environment Variables (`.env`):
79+
80+
Create a `.env` file in the root directory to store your sensitive information. Here are the environment variables you can set in the `.env` file:
81+
82+
- `DISCORD_BOT_TOKEN`: Your Discord bot token.
83+
- `OPENAI_API_KEY`: Your OpenAI API key. Get it from [OpenAI](https://platform.openai.com/).
84+
- `UNSPLASH_ACCESS_KEY`: Your Unsplash API key. Obtain it from [Unsplash](https://unsplash.com/developers).
85+
- `CHATGPT_ACCESS_TOKEN`: Your ChatGPT API access token.
86+
87+
Example `.env` file:
88+
89+
```plaintext
90+
DISCORD_BOT_TOKEN=your_discord_bot_token
91+
OPENAI_API_KEY=your_openai_api_key
92+
UNSPLASH_ACCESS_KEY=your_unsplash_access_key
93+
CHATGPT_ACCESS_TOKEN=your_chatgpt_access_token
94+
```
95+
96+
Make sure to replace `your_discord_bot_token`, `your_openai_api_key`, `your_unsplash_access_key`, and `your_chatgpt_access_token` with your actual tokens and keys.
97+
98+
These environment variables are crucial for the proper functioning of BhaluBot. Ensure that you keep this file secure and do not share these sensitive details publicly.
99+
100+
101+
## Contributing:
102+
103+
If you find a bug or want to suggest an improvement, please feel free to open an issue or submit a pull request. Your contributions are greatly appreciated!
104+
105+
## License:
106+
107+
Bhalu Bot is open-source software licensed under the [MIT License](LICENSE).
108+
109+
## Credits:
110+
111+
The Bhalu Discord Bot is created and maintained by [AmitxD](https://github.com/Amitminer), based on BhaluBot (php).

config.yml

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
1-
Prefix: "++"
2-
owner_id: "000000000"
1+
# config.yml
2+
3+
# Prefix for bot commands
4+
Prefix: "++"
5+
6+
# Owner ID of the bot (e.g., Discord user ID)
7+
owner_id: "000000000"
8+
9+
# Generation configuration for the AI model
10+
generation_config:
11+
temperature: 0.9 # Temperature parameter for text generation
12+
top_p: 1 # Top p parameter for text generation
13+
top_k: 0 # Top k parameter for text generation
14+
max_output_tokens: 2048 # Maximum number of output tokens
15+
response_mime_type: "text/plain" # Mime type of the response
16+
17+
# Safety settings for filtering harmful content
18+
safety_settings:
19+
- category: "HARM_CATEGORY_HARASSMENT" # Category of harmful content (e.g., harassment)
20+
threshold: "BLOCK_NONE" # Threshold for blocking (e.g., none)
21+
- category: "HARM_CATEGORY_HATE_SPEECH" # Category of harmful content (e.g., hate speech)
22+
threshold: "BLOCK_NONE" # Threshold for blocking (e.g., none)
23+
- category: "HARM_CATEGORY_SEXUALLY_EXPLICIT" # Category of harmful content (e.g., sexually explicit)
24+
threshold: "BLOCK_NONE" # Threshold for blocking (e.g., none)
25+
- category: "HARM_CATEGORY_DANGEROUS_CONTENT" # Category of harmful content (e.g., dangerous content)
26+
threshold: "BLOCK_NONE" # Threshold for blocking (e.g., none)
27+
28+
# IF your primary unsplash api key limit is reached.. use backup api instead..
29+
Use_Unsplash_key2: false

src/Commands/Ask.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from discord.ext import commands
2+
from Manager.ChatManager import ChatManager
3+
import aiohttp
4+
5+
class Ask(commands.Cog):
6+
def __init__(self, bot):
7+
self.bot = bot
8+
9+
@commands.command()
10+
async def ask(self, ctx, *, question=''):
11+
# Check if the message contains text or attachments
12+
if not question.strip() and not ctx.message.attachments:
13+
await ctx.send("You need to ask something or attach an image")
14+
return
15+
16+
try:
17+
await ctx.channel.typing() # Start typing indication
18+
19+
if ctx.message.attachments: # If message contains attachments
20+
await self.process_image_attachments(ctx, question)
21+
else: # If no attachments, generate response based on text
22+
response = await ChatManager().ask_gemini_ai(question)
23+
await self.send_split_message(ctx, response)
24+
25+
except Exception as e:
26+
await ctx.send(f"An error occurred: {e}")
27+
28+
async def process_image_attachments(self, ctx, question):
29+
"""
30+
Process image attachments and generate a response.
31+
"""
32+
for attachment in ctx.message.attachments:
33+
if any(attachment.filename.lower().endswith(ext) for ext in ['.png', '.jpg', '.jpeg', '.gif', '.webp']):
34+
async with aiohttp.ClientSession() as session:
35+
async with session.get(attachment.url) as resp:
36+
if resp.status != 200:
37+
await ctx.send('Unable to download the image.')
38+
return
39+
image_data = await resp.read()
40+
response_text = await ChatManager().generate_response_with_image_and_text(image_data, question)
41+
await self.send_split_message(ctx, response_text)
42+
43+
async def send_split_message(self, ctx, response):
44+
"""
45+
Split the response message into multiple messages if it exceeds Discord's message length limit.
46+
"""
47+
max_message_length = 1900
48+
if len(response) <= max_message_length:
49+
await ctx.message.reply(response)
50+
else:
51+
chunks = [response[i:i+max_message_length] for i in range(0, len(response), max_message_length)]
52+
for chunk in chunks:
53+
await ctx.message.reply(chunk)
54+
55+
@commands.command()
56+
async def deletechat(self, ctx):
57+
"""
58+
Delete chat history and reset data.
59+
"""
60+
try:
61+
ChatManager().delete_data()
62+
await ctx.send("Chat history deleted successfully.")
63+
except Exception as e:
64+
await ctx.send(f"An error occurred: {e}")
65+
66+
async def setup(bot):
67+
await bot.add_cog(Ask(bot))

src/Commands/SearchImage.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from discord.ext import commands
2+
from Manager.ImageManager import ImageManager
3+
4+
class SearchImage(commands.Cog):
5+
6+
def __init__(self, client):
7+
self.client = client
8+
9+
10+
@commands.command()
11+
async def search(self, ctx, count: int = 1, *, query: str):
12+
try:
13+
await ctx.channel.typing()
14+
15+
if (count<5):
16+
for _ in range(count):
17+
image_url = await ImageManager.get_image(query)
18+
await ctx.message.reply(image_url)
19+
else :
20+
await ctx.message.reply("Bhai mat kar :/")
21+
22+
23+
except Exception as e:
24+
await ctx.channel.typing()
25+
await ctx.message.reply(f"An error occurred: {e}. Please try again later.")
26+
27+
async def setup(client):
28+
await client.add_cog(SearchImage(client))

0 commit comments

Comments
 (0)