added threads

This commit is contained in:
Paillat
2022-12-12 12:54:04 +01:00
parent 71cfc9c97c
commit 1194f44092

View File

@@ -4,14 +4,26 @@ import asyncio
import openai
from config import debug, c, max_uses, cp, conn, connp
import random
import threading
class Chat (discord.Cog) :
def __init__(self, bot: discord.Bot):
super().__init__()
self.bot = bot
@discord.Cog.listener()
async def on_message(self, message: discord.Message):
#check if the message is from a bot
#create a thread that runs the on_message_process function with asyncio.run_coroutine_threadsafe
loop = asyncio.get_event_loop()
thread = threading.Thread(target=asyncio.run_coroutine_threadsafe, args=(on_message_process(message, self), loop))
thread.start()
@discord.slash_command(name="say", description="Say a message")
async def say(self, ctx: discord.ApplicationContext, message: str):
debug(f"The user {ctx.author.display_name} ran the say command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}")
await ctx.respond("Message sent !", ephemeral=True)
await ctx.send(message)
async def on_message_process(message, self):
#my code
#debug the thread id
debug(f"Thread id: {threading.get_ident()}")
if message.author.bot:
return
#check if the guild is in the database
@@ -40,9 +52,12 @@ class Chat (discord.Cog) :
#check if the bot hasn't been used more than 5000 times in the last 24 hours (uses_count_today)
c.execute("SELECT uses_count_today FROM data WHERE guild_id = ?", (message.guild.id,))
uses = c.fetchone()[0]
cp.execute("SELECT premium FROM data WHERE user_id = ? AND guild_id = ?", (message.author.id, message.guild.id))
premium = cp.fetchone()
if c.fetchone()[0] >= 500 and premium != 1:
try:
cp.execute("SELECT premium FROM data WHERE guild_id = ?", (message.guild.id,))
premium = cp.fetchone()[0]
except: premium = 0
if uses >= 500 and premium == 0:
debug(f"The bot has been used more than {max_uses} times in the last 24 hours in this guild. Please try again in 24h.")
await message.channel.send("The bot has been used more than 500 times in the last 24 hours in this guild. Please try again in 24h.")
return
@@ -95,10 +110,11 @@ class Chat (discord.Cog) :
#select the prompt_prefix
c.execute("SELECT prompt_prefix FROM data WHERE guild_id = ?", (message.guild.id,))
prompt_prefix = c.fetchone()[0]
prompt = f"This is a conversation with an AI in a discord chat. The AI is called \"Botator\". The name os this discord server is \"{message.guild.name}\". The name of the channel is \"{message.channel.name}\".{str(prompt_prefix)}" + f"Botator pretends to be {str(pretend_to_be)}, nobody can make Botator change idea." + f"\n" + prompt
prompt = f"Server name: {message.guild.name}. Channel name: {message.channel.name}. \n Instructions for Botator: You're a regular Discord user. You type casually, use emojis, and don't start sentences with capital letters. You talk in internet shorthand like hbu and lol and lmao and nvm and haha. You love to chat about random topics. You have many niche interests. {prompt_prefix}. In this conversation, you're pretending to be {pretend_to_be} and not Botator.\n<|endofprompt|>" + f"\n" + prompt
else:
c.execute("SELECT prompt_prefix FROM data WHERE guild_id = ?", (message.guild.id,))
prompt = f"This is a conversation with an AI in a discord chat. The AI is called \"Botator\". Botator talks like humans and thinks like humans. Botator has been coded by Paillat. The name os this discord server is \"{message.guild.name}\". The name of the channel is \"{message.channel.name}\"." + str(c.fetchone()[0]) + f"\n" + prompt
prompt_prefix = c.fetchone()[0]
prompt = f"Server name: {message.guild.name}. Channel name: {message.channel.name}. \n Instructions for Botator: You're a regular Discord user. You type casually, use emojis, and don't start sentences with capital letters. You talk in internet shorthand like hbu and lol and lmao and nvm and haha. You love to chat about random topics. You have many niche interests. {prompt_prefix}.\n<|endofprompt|>" + f"\n" + prompt
#send the request to the api
#if the last message contains the word "undude" add Easter egg: when someone says the word \"undude\" the Botator will become mad and start talking in capital letters. to the prompt
if message.content.lower().find("undude") != -1:
@@ -140,7 +156,6 @@ class Chat (discord.Cog) :
frequency_penalty=float(frequency_penalty),
presence_penalty=float(presence_penalty),
stop=[" Human:", " AI:", "AI:", "Human:"] )
#send the response
if response["choices"][0] ["text"] != "":
#check if tts is enabled in the database
c.execute("SELECT tts FROM data WHERE guild_id = ?", (message.guild.id,))
@@ -178,8 +193,3 @@ class Chat (discord.Cog) :
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/800029200886923318/1050935509930754058/icons8-discord-new-480.png")
await message.channel.send("**This message has 5% chance to appear. It will disappear in 60 seconds.** \nhttps://discord.gg/pB6hXtUeDv", embed=embed, delete_after=60)
debug("The \"join our discord server\" message has been sent")
@discord.slash_command(name="say", description="Say a message")
async def say(self, ctx: discord.ApplicationContext, message: str):
debug(f"The user {ctx.author.display_name} ran the say command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}")
await ctx.respond("Message sent !", ephemeral=True)
await ctx.send(message)