Compare commits
	
		
			2 Commits
		
	
	
		
			fd08420f26
			...
			ede777ee42
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ede777ee42 | |||
| 4ba6e64403 | 
							
								
								
									
										25
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								bot.py
									
									
									
									
									
								
							| @@ -64,6 +64,15 @@ async def webhook(channel): | |||||||
|         return await channel.create_webhook(name=f'aoi-{channel.id}') |         return await channel.create_webhook(name=f'aoi-{channel.id}') | ||||||
|     return channel_hooks[0] |     return channel_hooks[0] | ||||||
|  |  | ||||||
|  | async def clear_reactions(channel, message_ids): | ||||||
|  |     for message_id in message_ids: | ||||||
|  |         try: | ||||||
|  |             message = await channel.fetch_message(message_id) | ||||||
|  |             await message.clear_reaction("🔁") | ||||||
|  |             await message.clear_reaction("❌") | ||||||
|  |         except (discord.NotFound, discord.Forbidden): | ||||||
|  |             pass # Ignore if message is not found or we don't have perms | ||||||
|  |  | ||||||
|  |  | ||||||
| # --- Bot Events --- | # --- Bot Events --- | ||||||
| @bot.event | @bot.event | ||||||
| @@ -96,13 +105,7 @@ async def on_message(message): | |||||||
|     try: |     try: | ||||||
|         async with channel.typing(): |         async with channel.typing(): | ||||||
|             response = await conversation.generate(user_message, media) |             response = await conversation.generate(user_message, media) | ||||||
|         for old_message_id in conversation.last_messages: |         await clear_reactions(channel, conversation.last_messages) | ||||||
|             try: |  | ||||||
|                 old_message = await channel.fetch_message(old_message_id) |  | ||||||
|                 await old_message.clear_reaction("🔁") |  | ||||||
|                 await old_message.clear_reaction("❌") |  | ||||||
|             except (discord.NotFound, discord.Forbidden): |  | ||||||
|                 pass # Ignore if message is not found or we don't have perms |  | ||||||
|         conversation.last_messages = await discord_send( |         conversation.last_messages = await discord_send( | ||||||
|             channel, response, conversation.bot_name, |             channel, response, conversation.bot_name, | ||||||
|         ) |         ) | ||||||
| @@ -158,11 +161,17 @@ async def on_reaction_add(reaction, user): | |||||||
| async def newchat(interaction: discord.Interaction, prompt: str = None): | async def newchat(interaction: discord.Interaction, prompt: str = None): | ||||||
|     await interaction.response.defer() |     await interaction.response.defer() | ||||||
|     channel_id = interaction.channel_id |     channel_id = interaction.channel_id | ||||||
|  |     old_convo = await Conversation.get( | ||||||
|  |         channel_id, args.base_url, bot.db, create_if_not_exist=False, | ||||||
|  |     ) | ||||||
|  |     if old_convo: | ||||||
|  |         await clear_reactions(interaction.channel, old_convo.last_messages) | ||||||
|     conversation = await Conversation.create( |     conversation = await Conversation.create( | ||||||
|         channel_id, args.base_url, bot.db, prompt |         channel_id, args.base_url, bot.db, prompt | ||||||
|     ) |     ) | ||||||
|     await interaction.followup.send( |     await interaction.followup.send( | ||||||
|         f'Starting a new chat with {conversation.bot_name}: "{prompt}"' |         f'Starting a new chat with {conversation.bot_name}: ' | ||||||
|  |         f'"{conversation.history[0]["content"]}"' | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
| @bot.tree.command( | @bot.tree.command( | ||||||
|   | |||||||
| @@ -33,7 +33,7 @@ class Conversation: | |||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     async def get(cls, key, base_url, db): |     async def get(cls, key, base_url, db, create_if_not_exist=True): | ||||||
|         convo_data = db.get_conversation(key) |         convo_data = db.get_conversation(key) | ||||||
|         if convo_data: |         if convo_data: | ||||||
|             history, bot_name, last_messages = convo_data |             history, bot_name, last_messages = convo_data | ||||||
| @@ -42,7 +42,9 @@ class Conversation: | |||||||
|             convo.history = history |             convo.history = history | ||||||
|             convo.last_messages = last_messages |             convo.last_messages = last_messages | ||||||
|             return convo |             return convo | ||||||
|         return await Conversation.create(key, base_url, db) |         if create_if_not_exists: | ||||||
|  |             return await Conversation.create(key, base_url, db) | ||||||
|  |         return None | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     async def create(cls, key, base_url, db, prompt=None): |     async def create(cls, key, base_url, db, prompt=None): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user