File size: 3,094 Bytes
297e47e
 
 
 
 
 
 
2769f4c
297e47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92c1a96
297e47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c264bf
297e47e
 
 
 
da3b687
297e47e
 
 
 
 
 
 
 
 
 
2769f4c
 
 
755bd26
e73fef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4a5034
e73fef4
297e47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
    # Ignore messages from the bot itself
    if message.author == bot.user:
        return

    await handle_message(message)  # Call your function

    # Optional: if using commands, don't forget this
    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"))