Skip to content

Commit d08a097

Browse files
committed
discord: fix interactions answers
1 parent 42d35dd commit d08a097

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

examples/discord/agent.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from duckduckgo_search import DDGS
2020
from typing import Dict, List
2121
import os
22+
from langchain.text_splitter import RecursiveCharacterTextSplitter
2223
import discord
2324
import openai
2425
import urllib.request

examples/discord/main.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async def on_ready():
8181
print(f"We have logged in as {client.user}")
8282

8383
def run_localagi_thread_history(history, message, thread, loop):
84+
agent.channel = message.channel
8485
def call(thing):
8586
return asyncio.run_coroutine_threadsafe(thing,loop).result()
8687
sent_message = call(thread.send(f"⚙️ LocalAGI starts"))
@@ -115,6 +116,7 @@ def reasoning_callback(name, reasoning):
115116

116117

117118
def run_localagi_message(message, loop):
119+
agent.channel = message.channel
118120
def call(thing):
119121
return asyncio.run_coroutine_threadsafe(thing,loop).result()
120122
sent_message = call(message.channel.send(f"⚙️ LocalAGI starts"))
@@ -148,6 +150,7 @@ def reasoning_callback(name, reasoning):
148150
call(sent_message.edit(content=f"<@{user.id}> {conversation_history[-1]['content']}"))
149151

150152
def run_localagi(interaction, prompt, loop):
153+
agent.channel = interaction.channel
151154

152155
def call(thing):
153156
return asyncio.run_coroutine_threadsafe(thing,loop).result()
@@ -204,8 +207,6 @@ def reasoning_callback(name, reasoning):
204207
@app_commands.describe(prompt="Ask me anything!")
205208
async def localai(interaction: discord.Interaction, prompt: str):
206209
loop = asyncio.get_running_loop()
207-
agent.loop = loop
208-
agent.channel = interaction.channel
209210
threading.Thread(target=run_localagi, args=[interaction, prompt,loop]).start()
210211

211212
# https://github.com/openai/gpt-discord-bot/blob/1161634a59c6fb642e58edb4f4fa1a46d2883d3b/src/utils.py#L15
@@ -224,16 +225,19 @@ def discord_message_to_message(message):
224225
return { "role": "user", "content": message.content }
225226
return None
226227

228+
@client.event
229+
async def on_ready():
230+
loop = asyncio.get_running_loop()
231+
agent.loop = loop
232+
227233
@client.event
228234
async def on_message(message):
229235
# ignore messages from the bot
230236
if message.author == client.user:
231237
return
232238
loop = asyncio.get_running_loop()
233-
agent.loop = loop
234239
# ignore messages not in a thread
235240
channel = message.channel
236-
agent.channel = channel
237241
if not isinstance(channel, discord.Thread) and client.user.mentioned_in(message):
238242
threading.Thread(target=run_localagi_message, args=[message,loop]).start()
239243
return

0 commit comments

Comments
 (0)