|
import os |
|
import threading |
|
from flask import Flask, jsonify |
|
from dotenv import load_dotenv |
|
import discord |
|
from discord.ext import commands |
|
from glai import GLAI |
|
import time |
|
|
|
load_dotenv() |
|
|
|
intents = discord.Intents().default() |
|
intents.message_content = True |
|
intents.members = True |
|
intents.message_content = True |
|
|
|
bot = commands.Bot(command_prefix="!", intents=intents) |
|
app = Flask(__name__) |
|
|
|
ai = GLAI(api_key=os.getenv("GOOGLE_API_KEY"), pinecone_api_key=os.getenv("PINECONE_API_KEY")) |
|
|
|
status = True |
|
|
|
allowed_channels = [1355959320222892281, 997339826565152844, 1370813873791303771, 1371683872974311575] |
|
|
|
|
|
def get_members(): |
|
guild = bot.get_guild(1355959320222892273) |
|
if not guild: |
|
raise Exception("Guild not found!") |
|
memes = [{"username": member.name, "id": member.id, "name": member.global_name} for member in guild.members if |
|
not member.bot] |
|
return memes |
|
|
|
|
|
@app.route('/members', methods=['GET']) |
|
def members(): |
|
members_list = get_members() |
|
return jsonify({"members": members_list}) |
|
|
|
|
|
def get_response(message: str, session_id: str, name: str) -> str: |
|
try: |
|
response = ai.query(message, session_id, "Rohit", name) |
|
return response["answer"].content |
|
except Exception as e: |
|
print("Error: \n") |
|
print(e) |
|
return f"Something went wrong!, Retrying connection... {str(e)}" |
|
|
|
|
|
async def handle_message(message: discord.Message): |
|
user = message.author |
|
content = message.content |
|
|
|
global status |
|
global allowed_channels |
|
|
|
print(f"channel: {message.channel.id}") |
|
|
|
if message.channel.id == 1371683872974311575: |
|
time.sleep(2) |
|
|
|
if (message.channel.id in allowed_channels) or ("1368647373110382702" in content): |
|
if "start" in content and str(user) == "adityasharmalive" and status is False: |
|
await message.channel.send("<@325866452089307146> I am on") |
|
status = True |
|
return |
|
|
|
if "stop" in content and str(user) == "adityasharmalive": |
|
await message.channel.send("<@325866452089307146> bye") |
|
status = False |
|
return |
|
|
|
if status is False: |
|
await message.channel.send("Please ask <@1186231758191071313> sr to start me.") |
|
return |
|
|
|
async with message.channel.typing(): |
|
response = get_response(content, str(message.channel.id), user) |
|
await message.channel.send(response) |
|
|
|
|
|
@bot.event |
|
async def on_message(message): |
|
|
|
if message.author == bot.user: |
|
return |
|
|
|
await handle_message(message) |
|
|
|
|
|
await bot.process_commands(message) |
|
|
|
|
|
@bot.command() |
|
async def server_id(ctx): |
|
await ctx.send(f"The server ID is: {ctx.guild.id}") |
|
|
|
|
|
def start_flask(): |
|
app.run(port=7860, host="0.0.0.0", debug=False) |
|
|
|
|
|
@bot.event |
|
async def on_ready(): |
|
print(f"Bot logged in as {bot.user}") |
|
flask_thread = threading.Thread(target=start_flask) |
|
flask_thread.start() |
|
|
|
|
|
bot.run(os.getenv("DISCORD_BOT_TOKEN")) |