-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_bot.py
152 lines (125 loc) · 5.57 KB
/
run_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
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
import discord
from discord.ext import commands
import requests
import json
from collections import defaultdict
from datetime import datetime, timedelta
import asyncio
import random
import pytz
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
conversations = defaultdict(lambda: {"history": [], "last_update": datetime.now()})
# Lunch and Friday messages
lunch_messages = ["Vi på jobbet behöver veta vart vi ska äta lunch på Södermalm, gärna nära Medborgarplatsen, ge oss ett tips på resturang, inte fler, bara ett tips till oss. Nämn resturangen. Inled meningen med: Här är ett tips på lunch"]
friday_messages = ["Önska alla en trevlig helg och säg att dom jobbat bra, var kortfattad."]
def get_gpt_sw3_response(conversation):
url = 'https://gpt.ai.se/v1/engines/gpt-sw3/chat/completions'
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
payload = {
"model": "gpt-sw3-20b-instruct",
"messages": conversation,
"max_tokens": 768,
"temperature": 0.8,
"top_p": 1,
"n": 1,
"stream": False,
"stop": ["<s>"],
"logit_bias": {},
"presence_penalty": 0,
"frequency_penalty": 0,
"user": "ai_sweden",
"token": "6p3T5QW4gu2FDsJfVmmPvS26HNT478uj"
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()
async def clear_old_conversations():
while True:
current_time = datetime.now()
for user_id in list(conversations.keys()):
if current_time - conversations[user_id]["last_update"] > timedelta(minutes=60*4):
del conversations[user_id]
await asyncio.sleep(60)
channel_ids = [911287092586356787, 1070643520727171154]
#channel_ids = [1070643520727171154]
@bot.command()
async def send_dm(ctx, user_id: int, *, prompt: str):
print("send_dm command invoked")
if not isinstance(ctx.channel, discord.channel.DMChannel):
await ctx.send("This command can only be used in direct messages.")
return
conversation = [{"role": "user", "content": prompt}]
print("!send_dm_input", conversation, user_id)
api_response = get_gpt_sw3_response(conversation)
if api_response and api_response.get('choices'):
gpt_message = api_response['choices'][0]['message']['content']
print("!send_dm_response", gpt_message, user_id)
else:
await ctx.send("Sorry, I couldn't generate a response.")
return
user = await bot.fetch_user(user_id)
if user:
try:
await user.send(gpt_message)
await ctx.send(f"Message sent to {user.display_name}.")
except discord.errors.Forbidden:
await ctx.send("I can't send a message to this user.")
else:
await ctx.send("User not found.")
async def send_scheduled_message():
now = datetime.now(pytz.timezone('Europe/Stockholm'))
next_run_time = now + timedelta(days=1)
next_run_time = next_run_time.replace(hour=11, minute=random.randint(15, 30), second=0, microsecond=0)
if now.weekday() < 5:
lunch_time_today = now.replace(hour=11, minute=random.randint(15, 30), second=0, microsecond=0)
if now < lunch_time_today:
next_run_time = lunch_time_today
message_list = lunch_messages
elif now.weekday() == 4:
friday_message_time = now.replace(hour=15, minute=random.randint(30, 45), second=0, microsecond=0)
if now < friday_message_time:
next_run_time = friday_message_time
message_list = friday_messages
wait_seconds = (next_run_time - now).total_seconds()
await asyncio.sleep(wait_seconds)
for channel_id in channel_ids:
channel = bot.get_channel(channel_id)
if channel:
initial_message = random.choice(message_list)
conversation = [{"role": "user", "content": initial_message}]
api_response = get_gpt_sw3_response(conversation)
if api_response and api_response.get('choices'):
assistant_message = api_response['choices'][0]['message']['content']
await channel.send(assistant_message)
bot.loop.create_task(send_scheduled_message())
@bot.event
async def on_message(message):
if message.author == bot.user or message.author.bot:
return
if bot.user.mentioned_in(message):
user_id = message.author.id
conversation = conversations[user_id]["history"]
if not conversation:
conversation.append({"role": "assistant", "content": "Hi, I am your helpful assistant."})
conversation.append({"role": "user", "content": message.content})
api_response = get_gpt_sw3_response(conversation)
if api_response and api_response.get('choices'):
assistant_message = api_response['choices'][0]['message']['content']
conversation.append({"role": "assistant", "content": assistant_message})
await message.channel.send(assistant_message)
else:
await message.channel.send("Sorry, I couldn't process that.")
conversations[user_id]["last_update"] = datetime.now()
print("conversation:", conversation)
await bot.process_commands(message)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
for guild in bot.guilds:
print(f'Guild: {guild.name}')
for channel in guild.channels:
print(f' - {channel.name} ({channel.id})')
bot.loop.create_task(send_scheduled_message())
bot.run('MTE4Mzg1MDA5OTg4MTgyMDIwMA.GjgFkO.B21SV9U5IhdV4XxMGEBFw_Cz97FKk3cArYzZlg')