-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (95 loc) · 3.02 KB
/
main.py
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
from discord.ext import commands
import discord
from dotenv import load_dotenv
import os
from openai import OpenAI
import sqlite3
import json
load_dotenv()
client = OpenAI(
api_key=os.getenv("OPENAI_KEY"),
)
DISCORD = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
intents.typing = True
bot = commands.Bot(command_prefix='.', intents=intents)
def init_db():
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS users (
userID TEXT PRIMARY KEY,
messages TEXT
)
""")
conn.commit()
def user_check(user_id, limit):
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute("SELECT * FROM users WHERE userID=(?)", (user_id,))
result = cur.fetchone()
conn.close()
if result is None or len(json.loads(result[1])) <= limit:
return True
else:
return False
def fetch_messages(user_id):
con = sqlite3.connect('database.db')
cur = con.cursor()
cur.execute("SELECT * FROM users WHERE userID=(?)", (user_id,))
response = cur.fetchone()
con.close()
if response is None:
return []
return json.loads(response[1])
def save_messages(user_id, messages):
con = sqlite3.connect('database.db')
cur = con.cursor()
cur.execute("INSERT OR REPLACE INTO users (userID, messages) VALUES (?, ?)", (user_id, json.dumps(messages)))
con.commit()
con.close()
@bot.command('text')
async def openai_input_text(ctx, *, message):
if not user_check(ctx.author.id, 10):
await ctx.send('You are currently over the allowed quota for text responses. Thank you for trying us out!')
return
messages = fetch_messages(ctx.author.id)
messages.append(
{"role": "user", "content": message},
)
async with ctx.typing():
try:
chat_completion = client.chat.completions.create(
model="gpt-4o-mini", messages=messages
)
reply = chat_completion.choices[0].message.content
await ctx.send(reply)
messages.append(
{"role": "assistant", "content": reply},
)
save_messages(ctx.author.id, messages)
except Exception as e:
print(e)
await ctx.send('Error')
@bot.command('image')
async def openai_input_image(ctx, *, message):
if not user_check(ctx.author.id, 3):
await ctx.send('You are currently over the allowed quota for image responses. Thank you for trying us out!')
return
async with ctx.typing():
try:
response = client.images.generate(
model="dall-e-2",
prompt=message,
size="512x512",
quality="standard",
n=1,
)
reply = response.data[0].url
await ctx.send(reply)
except Exception as e:
print(e)
await ctx.send('Error')
init_db()
bot.run(DISCORD)