-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
49 lines (40 loc) · 1.48 KB
/
bot.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
import json
import aiohttp
import asyncio
import discord
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
API_URL = 'http://localhost:8000/v1/chat/completions'
headers = {"Content-Type":"application/json"}
payload = {
"max_tokens":512,
"messages": []
}
class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!bot'):
stripped_msg = str(message.content).replace('!bot','').strip()
msg = {
"content": f"{stripped_msg} ### response: ",
"role": "user"
}
payload["messages"].append(msg)
async with aiohttp.ClientSession() as session:
async with session.post(API_URL, data = json.dumps(payload), headers = headers) as resp:
reply = await resp.json()
reply_content = reply["choices"][0]["message"]["content"]
await message.reply(reply_content, mention_author=True)
msg_idx = payload["messages"].index(msg)
payload["messages"][msg_idx]["content"] += reply_content
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
client.run(TOKEN)