mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 01:06:19 +00:00
added threads
This commit is contained in:
@@ -4,14 +4,26 @@ import asyncio
|
|||||||
import openai
|
import openai
|
||||||
from config import debug, c, max_uses, cp, conn, connp
|
from config import debug, c, max_uses, cp, conn, connp
|
||||||
import random
|
import random
|
||||||
|
import threading
|
||||||
class Chat (discord.Cog) :
|
class Chat (discord.Cog) :
|
||||||
def __init__(self, bot: discord.Bot):
|
def __init__(self, bot: discord.Bot):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
@discord.Cog.listener()
|
@discord.Cog.listener()
|
||||||
async def on_message(self, message: discord.Message):
|
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:
|
if message.author.bot:
|
||||||
return
|
return
|
||||||
#check if the guild is in the database
|
#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)
|
#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,))
|
c.execute("SELECT uses_count_today FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||||
uses = c.fetchone()[0]
|
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()
|
try:
|
||||||
if c.fetchone()[0] >= 500 and premium != 1:
|
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.")
|
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.")
|
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
|
return
|
||||||
@@ -95,10 +110,11 @@ class Chat (discord.Cog) :
|
|||||||
#select the prompt_prefix
|
#select the prompt_prefix
|
||||||
c.execute("SELECT prompt_prefix FROM data WHERE guild_id = ?", (message.guild.id,))
|
c.execute("SELECT prompt_prefix FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||||
prompt_prefix = c.fetchone()[0]
|
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:
|
else:
|
||||||
c.execute("SELECT prompt_prefix FROM data WHERE guild_id = ?", (message.guild.id,))
|
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
|
#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 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:
|
if message.content.lower().find("undude") != -1:
|
||||||
@@ -140,7 +156,6 @@ class Chat (discord.Cog) :
|
|||||||
frequency_penalty=float(frequency_penalty),
|
frequency_penalty=float(frequency_penalty),
|
||||||
presence_penalty=float(presence_penalty),
|
presence_penalty=float(presence_penalty),
|
||||||
stop=[" Human:", " AI:", "AI:", "Human:"] )
|
stop=[" Human:", " AI:", "AI:", "Human:"] )
|
||||||
#send the response
|
|
||||||
if response["choices"][0] ["text"] != "":
|
if response["choices"][0] ["text"] != "":
|
||||||
#check if tts is enabled in the database
|
#check if tts is enabled in the database
|
||||||
c.execute("SELECT tts FROM data WHERE guild_id = ?", (message.guild.id,))
|
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")
|
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)
|
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")
|
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user