discord: fix interactions answers

This commit is contained in:
mudler
2023-08-23 21:56:19 +02:00
parent 42d35dd7a3
commit d08a097175
2 changed files with 9 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ from ascii_magic import AsciiArt
from duckduckgo_search import DDGS from duckduckgo_search import DDGS
from typing import Dict, List from typing import Dict, List
import os import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
import discord import discord
import openai import openai
import urllib.request import urllib.request

View File

@@ -81,6 +81,7 @@ async def on_ready():
print(f"We have logged in as {client.user}") print(f"We have logged in as {client.user}")
def run_localagi_thread_history(history, message, thread, loop): def run_localagi_thread_history(history, message, thread, loop):
agent.channel = message.channel
def call(thing): def call(thing):
return asyncio.run_coroutine_threadsafe(thing,loop).result() return asyncio.run_coroutine_threadsafe(thing,loop).result()
sent_message = call(thread.send(f"⚙️ LocalAGI starts")) sent_message = call(thread.send(f"⚙️ LocalAGI starts"))
@@ -115,6 +116,7 @@ def run_localagi_thread_history(history, message, thread, loop):
def run_localagi_message(message, loop): def run_localagi_message(message, loop):
agent.channel = message.channel
def call(thing): def call(thing):
return asyncio.run_coroutine_threadsafe(thing,loop).result() return asyncio.run_coroutine_threadsafe(thing,loop).result()
sent_message = call(message.channel.send(f"⚙️ LocalAGI starts")) sent_message = call(message.channel.send(f"⚙️ LocalAGI starts"))
@@ -148,6 +150,7 @@ def run_localagi_message(message, loop):
call(sent_message.edit(content=f"<@{user.id}> {conversation_history[-1]['content']}")) call(sent_message.edit(content=f"<@{user.id}> {conversation_history[-1]['content']}"))
def run_localagi(interaction, prompt, loop): def run_localagi(interaction, prompt, loop):
agent.channel = interaction.channel
def call(thing): def call(thing):
return asyncio.run_coroutine_threadsafe(thing,loop).result() return asyncio.run_coroutine_threadsafe(thing,loop).result()
@@ -204,8 +207,6 @@ def run_localagi(interaction, prompt, loop):
@app_commands.describe(prompt="Ask me anything!") @app_commands.describe(prompt="Ask me anything!")
async def localai(interaction: discord.Interaction, prompt: str): async def localai(interaction: discord.Interaction, prompt: str):
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
agent.loop = loop
agent.channel = interaction.channel
threading.Thread(target=run_localagi, args=[interaction, prompt,loop]).start() threading.Thread(target=run_localagi, args=[interaction, prompt,loop]).start()
# https://github.com/openai/gpt-discord-bot/blob/1161634a59c6fb642e58edb4f4fa1a46d2883d3b/src/utils.py#L15 # https://github.com/openai/gpt-discord-bot/blob/1161634a59c6fb642e58edb4f4fa1a46d2883d3b/src/utils.py#L15
@@ -224,16 +225,19 @@ def discord_message_to_message(message):
return { "role": "user", "content": message.content } return { "role": "user", "content": message.content }
return None return None
@client.event
async def on_ready():
loop = asyncio.get_running_loop()
agent.loop = loop
@client.event @client.event
async def on_message(message): async def on_message(message):
# ignore messages from the bot # ignore messages from the bot
if message.author == client.user: if message.author == client.user:
return return
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
agent.loop = loop
# ignore messages not in a thread # ignore messages not in a thread
channel = message.channel channel = message.channel
agent.channel = channel
if not isinstance(channel, discord.Thread) and client.user.mentioned_in(message): if not isinstance(channel, discord.Thread) and client.user.mentioned_in(message):
threading.Thread(target=run_localagi_message, args=[message,loop]).start() threading.Thread(target=run_localagi_message, args=[message,loop]).start()
return return