-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
190 lines (146 loc) · 6.9 KB
/
bot.py
File metadata and controls
190 lines (146 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Library Import
import discord
import asyncio
import random
import os
import threading
# Discord Library Import
from discord.ext import commands
# Environment Variables Fetch
from flask import Flask
from dotenv import load_dotenv
# Functions File Import
from functions import get_ai_response
from functions import translate_text
load_dotenv(dotenv_path='minimind.env')
TOKEN = os.getenv('DISCORD_TOKEN')
app = Flask(__name__)
@app.route('/')
def home():
return "I'm alive! 🚀"
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents, help_command=None)
# Global Variables
language_map = { "albanian": "sq", "amharic": "am", "arabic": "ar", "armenian": "hy", "basque": "eu", "bengali": "bn", "bosnian": "bs", "bulgarian": "bg", "catalan": "ca", "chichewa": "ny", "chinese": "zh-CN", "croatian": "hr", "czech": "cs", "danish": "da", "dutch": "nl", "english": "en", "estonian": "et", "farsi": "fa", "filipino": "tl", "finnish": "fi", "french": "fr", "georgian": "ka", "german": "de", "greek": "el", "haitian creole": "ht", "hebrew": "he", "hindi": "hi", "hungarian": "hu", "icelandic": "is", "indonesian": "id", "irish": "ga", "italian": "it", "japanese": "ja", "javanese": "jw", "kannada": "kn", "korean": "ko", "kurdish": "ku", "latin": "la", "latvian": "lv", "lithuanian": "lt", "malagasy": "mg", "malayalam": "ml", "malaysian": "ms", "mandarin": "zh", "marathi": "mr", "mongolian": "mn", "nepali": "ne", "norwegian": "no", "pashto": "ps", "polish": "pl", "portuguese": "pt", "punjabi": "pa", "romanian": "ro", "russian": "ru", "serbian": "sr", "sesotho": "st", "sinhala": "si", "slovak": "sk", "swahili": "sw", "swedish": "sv", "tagalog": "tl", "tamil": "ta", "telugu": "te", "thai": "th", "tigrinya": "ti", "turkish": "tr", "ukrainian": "uk", "vietnamese": "vi", "welsh": "cy", "xhosa": "xh", "yiddish": "yi", "zulu": "zu" }
waiting_messages = {
"grammar": [
"Checking grammar... ✍️",
"Proofreading your text... 📖",
"Analyzing sentence structure... 🔍",
"Ensuring grammatical accuracy... ✅",
],
"translate": [
"Translating text... 🌍",
"Processing your translation... 🔄",
"Finding the right words... 📖",
"Let's break the language barrier! 💬",
],
"language": [
"Determining language... 🌍",
"Figuring out the language code... 🔤",
"Identifying the language... 📜",
"Just a moment while we detect the language... 🕒",
],
"summarize": [
"Summarizing your text... 📝",
"Breaking it down... 🔍",
"Giving you the highlights... 🌟",
"Compressing your text... 💼",
],
"synonym": [
"Finding the perfect match... 🔍",
"Fetching synonyms... 📚",
"Digging up synonyms... 🌱",
"Exploring word options... 🧠",
]
}
@bot.event
async def on_ready():
print(f"✅ Logged in as {bot.user}")
@bot.command()
async def ping(ctx):
await ctx.send("Pong! 🏓")
@bot.command()
async def grammar(ctx, *, text: str):
message = await ctx.send(random.choice(waiting_messages["grammar"]))
async def background_task():
response = await get_ai_response("grammar", text)
await message.edit(content=response)
asyncio.create_task(background_task())
@bot.command()
async def translate(ctx, language_pair: str, *, text: str):
message = await ctx.send(random.choice(waiting_messages["translate"]))
try:
source_lang, target_lang = language_pair.split("->")
except ValueError:
await message.edit(content="Invalid language pair format! Use the format: [source_lang->target_lang]. Example: `!translate en->fr Hello`")
return
translated_text = translate_text(text, source_lang, target_lang)
await message.edit(content=f"**Translated from {source_lang} to {target_lang}:** {translated_text}")
@bot.command()
async def language(ctx, language_name: str):
message = await ctx.send(random.choice(waiting_messages["language"]))
language_name = language_name.lower()
if language_name in language_map:
await message.edit(content=f"The language code for {language_name} is `{language_map[language_name]}`.")
else:
await message.edit(content=f"Sorry, I don't have a language code for '{language_name}'. Please check the language name and try again.")
@bot.command()
async def summarize(ctx, *, text: str):
message = await ctx.send(random.choice(waiting_messages["summarize"]))
async def background_task():
response = await get_ai_response("summarize", text)
await message.edit(content=f"**Paraphrased Text:** {response}")
asyncio.create_task(background_task())
@bot.command()
async def dictionary(ctx, word: str):
message = await ctx.send(random.choice(waiting_messages["dictionary"]))
response = await get_ai_response("dictionary", word)
await message.edit(content=f"**Definition of '{word}':**\n{response}")
@bot.command()
async def synonym(ctx, word: str):
message = await ctx.send(random.choice(waiting_messages["synonym"]))
response = await get_ai_response("synonym", word)
await message.edit(content=f"**Synonyms for '{word}':**\n{response}")
@bot.command()
async def help(ctx, command: str=None):
if command is None:
help_message = """
**MiniMind Commands**:robot: ```ini
!ping - Check if the bot is online 🏓
!help - Show this help message ❓
!grammar <text> - Check for grammar and spelling errors ✍️
!translate <text> - Translate text to English 🌍
!language <language> - Retrieves the language code for the specified language.
!summarize <text> - Condenses long text into a short summary.```"""
await ctx.send(help_message)
elif command == "translate":
translate_help = """
**!translate Command Help** 🌍
Use this command to translate text to English or other languages.
**Command Format:** `!translate <language_pair> <text>`
**Language Pair Format:** `<source_language_code>-><target_language_code>`
To check for language codes, use the command !language <language> to retrieve the corresponding language code.
"""
await ctx.send(translate_help)
elif command == "language":
language_help = """
**!language Command Help**
Use this command to retrieve the language code for a language, which can then be used in the !translate command.
**Command Format:** `!language <language>`
*Example: !language english -> en*
"""
await ctx.send(language_help)
else:
await ctx.send("Unknown command. Type `!help` for a list of commands.")
async def run_bot():
await bot.start(TOKEN)
def run_flask():
app.run(host='0.0.0.0', port=3000, use_reloader=False)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.create_task(run_bot())
loop.run_in_executor(None, run_flask)
loop.run_forever()
#bot.run(TOKEN)