-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12a3ba0
commit 3204866
Showing
7 changed files
with
182 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Filename: leonardo_test.py | ||
Author: Iliya Vereshchagin | ||
Copyright (c) 2023. All rights reserved. | ||
Created: 15.10.2023 | ||
Last Modified: 20.11.2023 | ||
Description: | ||
This file contains testing procedures for Midjourney experiments | ||
""" | ||
|
||
import asyncio | ||
|
||
from utils.discord_watcher import DiscordWatcher | ||
from utils.discord_interactions import DiscordInteractions | ||
|
||
from examples.creds import discord_watcher_token, discord_midjourney_payload | ||
|
||
|
||
# Usage | ||
async def main(): | ||
"""Main function""" | ||
prompt = "a beautiful necromancer witch resurrects skeletons against the backdrop of a burning ruined castle" | ||
discord = DiscordInteractions(token=discord_midjourney_payload['auth_token'], | ||
application_id=discord_midjourney_payload['application_id'], | ||
guild_id=discord_midjourney_payload['guild_id'], | ||
channel_id=discord_midjourney_payload['channel_id'], | ||
session_id=discord_midjourney_payload['session_id'], | ||
version=discord_midjourney_payload['version'], | ||
interaction_id=discord_midjourney_payload['interaction_id']) | ||
response = await discord.post_interaction(my_text_prompt=prompt) | ||
print(response) | ||
bot = DiscordWatcher(watch_user_id=int(discord_midjourney_payload['application_id'])) | ||
bot.run(discord_watcher_token) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ llamaapi==0.1.36 | |
leonardo-api==0.0.7 | ||
openai-python-api==0.0.5 | ||
ablt-python-api==0.0.2 | ||
# Discord | ||
py-cord==2.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Filename: discord_interaction.py | ||
Author: Iliya Vereshchagin | ||
Copyright (c) 2023. All rights reserved. | ||
Created: 20.11.2023 | ||
Last Modified: 20.11.2023 | ||
Description: | ||
This file contains discord interactions to python API. | ||
""" | ||
|
||
import aiohttp | ||
|
||
|
||
class DiscordInteractions: | ||
"""""" | ||
def __init__(self, token, **kwargs): | ||
""" | ||
Initialize DiscordInteractions class. | ||
:param token: The token to use for authorization. | ||
:param kwargs: The default parameters for the interaction. | ||
""" | ||
self.token = token | ||
self.headers = {"authorization": self.token} | ||
self.url = "https://discord.com/api/v9/interactions" | ||
self.default_params = kwargs | ||
|
||
async def post_interaction(self, my_text_prompt, **kwargs): | ||
""" | ||
Post any discord interaction. | ||
:param my_text_prompt: The text prompt to post. | ||
:type my_text_prompt: str | ||
:param kwargs: The parameters for the interaction. | ||
:return: The response from the interaction. | ||
:rtype: dict | ||
""" | ||
params = {**self.default_params, **kwargs} | ||
|
||
payload_data = { | ||
"type": 2, | ||
"application_id": params.get('application_id'), | ||
"guild_id": params.get('guild_id'), | ||
"channel_id": params.get('channel_id'), | ||
"session_id": params.get('session_id'), | ||
"data": { | ||
"version": params.get('version'), | ||
"id": params.get('interaction_id'), | ||
"name": "imagine", | ||
"type": 1, | ||
"options": [ | ||
{ | ||
"type": 3, | ||
"name": "prompt", | ||
"value": my_text_prompt | ||
} | ||
] | ||
} | ||
} | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.post(self.url, json=payload_data, headers=self.headers) as resp: | ||
if resp.status != 200: | ||
raise ValueError(f"Request failed with status code {resp.status}") | ||
return await resp.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Filename: discord_watcher.py | ||
Author: Iliya Vereshchagin | ||
Copyright (c) 2023. All rights reserved. | ||
Created: 20.11.2023 | ||
Last Modified: 20.11.2023 | ||
Description: | ||
This file contains discord interactions to python API. | ||
""" | ||
from abc import ABC | ||
|
||
from discord import Intents | ||
from discord.ext import commands | ||
|
||
from utils.logger_config import setup_logger | ||
|
||
|
||
class DiscordWatcher(commands.Bot, ABC): | ||
def __init__(self, watch_user_id=None, **options): | ||
""" | ||
Initialize DiscordWatcher class. | ||
:param command_prefix: The prefix for the bot. | ||
:param watch_user_id: The user ID to watch. | ||
:param options: The options for the bot. | ||
""" | ||
super().__init__(command_prefix='/', intents=Intents.all(), **options) | ||
self.target_user_id = watch_user_id | ||
self.___logger = setup_logger("discord_watcher", "discord_watcher.log") | ||
self.___logger.info('DiscordWatcher initialized') | ||
|
||
async def on_ready(self): | ||
"""This function is called when the bot is ready.""" | ||
self.___logger.debug('We have logged in as %s', self.user) | ||
|
||
async def on_message(self, message): | ||
""" | ||
This function is called when a message is created and sent. | ||
:param message: The message that was sent. | ||
:type message: discord.Message | ||
:return: The message content. | ||
:rtype: str | ||
""" | ||
self.___logger.debug('Got a message from %s : %s : %s', message.author, message.author.id, message.content) | ||
if message.author.id == self.target_user_id: | ||
if 'Waiting to start' not in message.content: | ||
self.___logger.debug('Found a message from the target user: %s', message.content) | ||
if message.attachments: | ||
for attachment in message.attachments: | ||
self.___logger.debug('Found an attachment: %s', attachment.url) | ||
return attachment.url | ||
if message.embeds: | ||
for embed in message.embeds: | ||
self.___logger.debug('Found an embed: %s', embed.to_dict()) | ||
return embed.to_dict() | ||
else: | ||
self.___logger.debug('Found a message from the target user, but content is not ready yet...') |