Added the /redo command (now working)

This commit is contained in:
Paillat
2022-12-16 15:43:32 +01:00
parent 9a6a9a8a77
commit ec5c4e8a5d

View File

@@ -18,80 +18,99 @@ class Chat (discord.Cog) :
@discord.slash_command(name="say", description="Say a message") @discord.slash_command(name="say", description="Say a message")
async def say(self, ctx: discord.ApplicationContext, message: str): 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}") print(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.respond("Message sent !", ephemeral=True)
await ctx.send(message) await ctx.send(message)
@discord.slash_command(name="redo", description="Redo a message") @discord.slash_command(name="redo", description="Redo a message")
async def redo(self, ctx: discord.ApplicationContext): async def redo(self, ctx: discord.ApplicationContext):
#first delete the last message but only if it was sent by the bot #first delete the last message but only if it was sent by the bot
# get the last message # get the last message
last_message = await ctx.channel.fetch_message(ctx.channel.last_message_id) history = await ctx.channel.history(limit=2).flatten()
# check if the last message was sent by the bot message_to_delete = history[0]
# if it was, delete it message_to_redo = history[1]
if last_message.author.id == self.bot.user.id: if message_to_delete.author.id == self.bot.user.id:
await last_message.delete() await message_to_delete.delete()
else: else:
await ctx.respond("The last message wasn't sent by the bot", ephemeral=True) await ctx.respond("The last message wasn't sent by the bot", ephemeral=True)
return return
await ctx.defer()
#get the message to redo aka the last message, because the old last message has been deleted #get the message to redo aka the last message, because the old last message has been deleted
#sleep for 1 second to make sure the message has been deleted #get the message before the last message, because the new last message is the bot thinking message, so the message before the last message is the message to redo
await asyncio.sleep(4) if message_to_redo.author.id == self.bot.user.id:
last_message = await ctx.channel.fetch_message(ctx.channel.last_message_id)
#check if the message to redo was sent by the bot
if last_message.author.id == self.bot.user.id:
await ctx.respond("The message to redo was sent by the bot", ephemeral=True) await ctx.respond("The message to redo was sent by the bot", ephemeral=True)
return return
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
thread = threading.Thread(target=asyncio.run_coroutine_threadsafe, args=(on_message_process(last_message, self), loop)) thread = threading.Thread(target=asyncio.run_coroutine_threadsafe, args=(on_message_process(message_to_redo, self), loop))
thread.start() thread.start()
await ctx.respond("Message redone !", ephemeral=True) await ctx.respond("Message redone !", delete_after=5)
async def on_message_process(message: discord.Message, self: Chat): async def on_message_process(message: discord.Message, self: Chat):
#my code #my code
#debug the thread id #print the thread id
debug(f"Thread id: {threading.get_ident()}") print(f"Thread id: {threading.get_ident()}")
print("hello")
if message.author.bot: if message.author.bot:
print("The message was sent by a bot")
return return
print("The message was sent by a human")
#check if the guild is in the database #check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,)) c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
if c.fetchone() is None: if c.fetchone() is None:
print("The guild is not in the database")
return return
print("The guild is in the database")
#check if the bot is enabled #check if the bot is enabled
c.execute("SELECT is_active FROM data WHERE guild_id = ?", (message.guild.id,)) c.execute("SELECT is_active FROM data WHERE guild_id = ?", (message.guild.id,))
if c.fetchone()[0] == False: if c.fetchone()[0] == False:
print("The bot is disabled")
return return
print("The bot is enabled")
#check if the message has been sent in the channel set in the database #check if the message has been sent in the channel set in the database
c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (message.guild.id,)) c.execute("SELECT channel_id FROM data WHERE guild_id = ?", (message.guild.id,))
#select channels from the database #check if the message begins with --, if it does, ignore it, it's a comment
try : cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,)) if message.content.startswith("-"):
except : channels = [] print("The message is a comment")
else : channels = cp.fetchone()[1:] return
#select channels from the premium table
try :
cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,))
channels = cp.fetchone()[1:]
except :
channels = []
print("No premium channels")
print("here2")
try : original_message = await message.channel.fetch_message(message.reference.message_id) try : original_message = await message.channel.fetch_message(message.reference.message_id)
except : original_message = None except : original_message = None
if original_message != None and original_message.author.id != self.bot.user.id: if original_message != None and original_message.author.id != self.bot.user.id:
original_message = None original_message = None
print("The message is a reply, but the reply is not to the bot")
print("here")
try :
cp.execute("SELECT premium FROM data WHERE guild_id = ?", (message.guild.id,))
premium = cp.fetchone()[0]
except :
premium = 0
print("No premium")
if str(message.channel.id) != str(c.fetchone()[0]) : if str(message.channel.id) != str(c.fetchone()[0]) :
#check if the message is a mention or if the message replies to the bot #check if the message is a mention or if the message replies to the bot
if original_message != None: if original_message != None:
debug("wrong channel, but reply") print("wrong channel, but reply")
elif message.content.find("<@"+str(self.bot.user.id)+">") != -1: elif message.content.find("<@"+str(self.bot.user.id)+">") != -1:
debug("wrong channel, but mention") print("wrong channel, but mention")
elif str(message.channel.id) in channels: elif str(message.channel.id) in channels and premium == 1:
debug("in a channel that is in the database") print("in a channel that is in the database and premium")
else : else :
debug("The message has been sent in the wrong channel") print("The message has been sent in the wrong channel")
return return
#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]
try: try:
cp.execute("SELECT premium FROM data WHERE guild_id = ?", (message.guild.id,)) cp.execute("SELECT premium FROM data WHERE guild_id = ?", (message.guild.id,))
premium = cp.fetchone()[0] premium = cp.fetchone()[0]
except: premium = 0 except: premium = 0
print("here1")
if uses >= 500 and 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.") print(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
#add 1 to the uses_count_today #add 1 to the uses_count_today
@@ -177,8 +196,8 @@ async def on_message_process(message: discord.Message, self: Chat):
prompt += "System: Fives3dprint detected. Botator will be very nice and cute with fives3dprint.\n" prompt += "System: Fives3dprint detected. Botator will be very nice and cute with fives3dprint.\n"
prompt += "Botator:" prompt += "Botator:"
prompt = prompt + f"\n" prompt = prompt + f"\n"
debug("Sending request to the api") print("Sending request to the api")
#debug(prompt) #print(prompt)
openai.api_key = api_key openai.api_key = api_key
response = openai.Completion.create( response = openai.Completion.create(
engine="text-davinci-003", engine="text-davinci-003",
@@ -196,11 +215,11 @@ async def on_message_process(message: discord.Message, self: Chat):
#if tts is enabled, send the message with tts enabled #if tts is enabled, send the message with tts enabled
if tts == 1: if tts == 1:
await message.channel.send(response["choices"][0]["text"], tts=True) await message.channel.send(response["choices"][0]["text"], tts=True)
debug("The response has been sent with tts enabled") print("The response has been sent with tts enabled")
#if tts is disabled, send the message with tts disabled #if tts is disabled, send the message with tts disabled
else: else:
await message.channel.send(response["choices"][0]["text"]) await message.channel.send(response["choices"][0]["text"])
debug("The response has been sent with tts disabled") print("The response has been sent with tts disabled")
else: else:
await message.channel.send("The AI is not sure what to say (the response was empty)") await message.channel.send("The AI is not sure what to say (the response was empty)")
debug("The response was empty") print("The response was empty")