Add a command to clear all conversation threads immediately across all servers the bot uses

Kaveen Kumarasinghe 2 years ago
parent 636fce327c
commit 165c62c978

@ -1,3 +1,4 @@
import datetime
import json
import os
import re
@ -7,6 +8,7 @@ import traceback
import discord
from discord.ext import commands
from models.deletion_service import Deletion
from models.message_model import Message
from models.user_model import User
from collections import defaultdict
@ -23,7 +25,7 @@ redo_users = {}
class GPT3ComCon(commands.Cog, name='GPT3ComCon'):
def __init__(self, bot, usage_service, model, message_queue, DEBUG_GUILD, DEBUG_CHANNEL):
def __init__(self, bot, usage_service, model, message_queue, deletion_queue, DEBUG_GUILD, DEBUG_CHANNEL):
self.debug_channel = None
self.bot = bot
self._last_member_ = None
@ -36,6 +38,7 @@ class GPT3ComCon(commands.Cog, name='GPT3ComCon'):
self.GLOBAL_COOLDOWN_TIME = 1
self.usage_service = usage_service
self.model = model
self.deletion_queue = deletion_queue
try:
# Attempt to read a conversation starter text string from the environment variables
@ -64,6 +67,17 @@ class GPT3ComCon(commands.Cog, name='GPT3ComCon'):
self.debug_channel = self.bot.get_guild(self.DEBUG_GUILD).get_channel(self.DEBUG_CHANNEL)
print(f"The debug channel was acquired")
@commands.command()
async def delete_all_conversation_threads(self, ctx):
# If the user has ADMIN_ROLES
if not any(role.name in self.ADMIN_ROLES for role in ctx.author.roles):
return
for guild in self.bot.guilds:
for thread in guild.threads:
if "with gpt" in thread.name.lower():
print(f"Deleting thread {thread.name}")
await thread.delete()
def check_conversing(self, message):
cond1 = message.author.id in self.conversating_users and message.channel.name in ["gpt3", "offtopic",
"general-bot",
@ -376,7 +390,11 @@ class RedoButtonView(discord.ui.View): # Create a class called MyView that subc
emoji="🔄") # Create a button with the label "😎 Click me!" with color Blurple
async def button_callback(self, button, interaction):
await interaction.response.send_message("Redoing your original request...", ephemeral=True)
msg = await interaction.response.send_message("Redoing your original request...", ephemeral=True)
# Put the message into the deletion queue with a timestamp of 10 seconds from now to be deleted
deletion = Deletion(msg, (datetime.datetime.now() + datetime.timedelta(seconds=10)).timestamp())
await self.bot.deletion_queue.put(deletion) #TODO this isn't supported by discord yet!
# Get the user
user_id = interaction.user.id

@ -9,7 +9,7 @@ class ImgPromptOptimizer(commands.Cog, name='ImgPromptOptimizer'):
_OPTIMIZER_PRETEXT = "Optimize the following text for DALL-E image generation to have the most detailed and realistic image possible. Prompt:"
def __init__(self, bot, usage_service, model, message_queue):
def __init__(self, bot, usage_service, model, message_queue, deletion_queue):
self.bot = bot
self.usage_service = usage_service
self.model = model

@ -6,6 +6,7 @@ from dotenv import load_dotenv
from cogs.gpt_3_commands_and_converser import GPT3ComCon
from cogs.image_prompt_optimizer import ImgPromptOptimizer
from models.deletion_service import Deletion
from models.message_model import Message
from models.openai_model import Model
from models.usage_service_model import UsageService
@ -17,7 +18,9 @@ import os
Message queueing for the debug service, defer debug messages to be sent later so we don't hit rate limits.
"""
message_queue = asyncio.Queue()
deletion_queue = asyncio.Queue()
asyncio.ensure_future(Message.process_message_queue(message_queue, 1.5, 5))
asyncio.ensure_future(Deletion.process_deletion_queue(deletion_queue, 1, 1))
"""
@ -37,13 +40,14 @@ An encapsulating wrapper for the discord.py client. This uses the old re-write w
async def on_ready(): # I can make self optional by
print('We have logged in as {0.user}'.format(bot))
async def main():
debug_guild = int(os.getenv('DEBUG_GUILD'))
debug_channel = int(os.getenv('DEBUG_CHANNEL'))
# Load the main GPT3 Bot service
bot.add_cog(GPT3ComCon(bot, usage_service, model, message_queue, debug_guild, debug_channel))
bot.add_cog(ImgPromptOptimizer(bot, usage_service, model, message_queue))
bot.add_cog(GPT3ComCon(bot, usage_service, model, message_queue, deletion_queue, debug_guild, debug_channel))
bot.add_cog(ImgPromptOptimizer(bot, usage_service, model, message_queue, deletion_queue))
await bot.start(os.getenv('DISCORD_TOKEN'))

@ -0,0 +1,37 @@
import asyncio
import traceback
from datetime import datetime
class Deletion:
def __init__(self, message, timestamp):
self.message = message
self.timestamp = timestamp
# This function will be called by the bot to process the message queue
@staticmethod
async def process_deletion_queue(deletion_queue, PROCESS_WAIT_TIME, EMPTY_WAIT_TIME):
while True:
try:
# If the queue is empty, sleep for a short time before checking again
if deletion_queue.empty():
await asyncio.sleep(EMPTY_WAIT_TIME)
continue
# Get the next message from the queue
deletion = await deletion_queue.get()
# Check if the current timestamp is greater than the deletion timestamp
if datetime.now().timestamp() > deletion.timestamp:
# If the deletion timestamp has passed, delete the message
await deletion.message.delete_original_response()
else:
await deletion_queue.put(deletion)
# Sleep for a short time before processing the next message
# This will prevent the bot from spamming messages too quickly
await asyncio.sleep(PROCESS_WAIT_TIME)
except:
traceback.print_exc()
pass
Loading…
Cancel
Save