Skip to main content

Midjourney automation system using Discord user tokens

Project description

๐ŸŽจ Python Midjourney

Midjourney automation Python package using Discord user tokens

Python Version License PyPI Version

โœจ Key Features

  • ๐ŸŽจ Midjourney Automation: Execute /imagine commands using Discord user tokens
  • ๐Ÿ”ช Auto Image Splitting: Automatically split 4-grid images into individual images
  • ๐Ÿ“ฆ Batch Processing: Process multiple prompts simultaneously
  • ๐Ÿ’พ Auto Save: Automatically save original + split images
  • ๐Ÿš€ Full Automation: Command โ†’ Generate โ†’ Download โ†’ Split โ†’ Save
  • ๐Ÿ“š Simple API: Intuitive and easy-to-use interface

๐Ÿš€ Installation

Install with pip

pip install python-midjourney

Install from source

git clone https://github.com/yourusername/python_midjourney.git
cd python_midjourney
pip install -e .

โš™๏ธ Environment Setup

1. Required Environment Variables

# Set Discord user token
export DISCORD_USER_TOKEN="your_discord_user_token_here"

# Set Midjourney channel ID
export MIDJOURNEY_CHANNEL_ID="your_midjourney_channel_id_here"

2. Using .env file (Recommended)

Create a .env file in your project root:

# Discord settings
DISCORD_USER_TOKEN=your_discord_user_token_here
MIDJOURNEY_CHANNEL_ID=your_midjourney_channel_id_here

3. How to Obtain Environment Variables

Getting Discord User Token

  1. Access Discord in web browser (discord.com)
  2. Open Developer Tools (F12)
  3. Go to Network tab
  4. Send any message
  5. Find Authorization header in API requests
  6. Copy the token (usually starts with MTAxxxxx...)

โš ๏ธ Important: Never share your user token!

Getting Midjourney Channel ID

  1. Enable Developer Mode in Discord
    • Settings โ†’ Advanced โ†’ Developer Mode ON
  2. Right-click on Midjourney channel
  3. Select "Copy ID"

๐ŸŽฏ Usage

Basic Usage

from python_midjourney import MidjourneyClient

# Create client (automatically uses environment variables)
client = MidjourneyClient()

# Generate image
result = client.generate_image("a beautiful sunset over mountains")

if result['success']:
    print(f"Original file: {result['original_file']}")
    print(f"Split files: {len(result['split_files'])} files")
    for i, file in enumerate(result['split_files'], 1):
        print(f"  {i}. {file}")

Direct Token Specification

from python_midjourney import MidjourneyClient

# Specify token and channel ID directly
client = MidjourneyClient(
    user_token="your_discord_token",
    channel_id="your_channel_id",
    auto_split=True  # Enable auto-split
)

result = client.generate_image("cyberpunk city landscape")

Batch Processing

from python_midjourney import MidjourneyClient

client = MidjourneyClient()

prompts = [
    "cozy coffee shop with warm lighting",
    "modern minimalist workspace",
    "serene mountain lake at dawn"
]

# Batch generation (5 second intervals)
results = client.generate_batch(prompts, delay=5)

# Check results
for i, result in enumerate(results):
    if result and result['success']:
        print(f"โœ… Image {i+1}: {result['total_files']} files generated")
    else:
        print(f"โŒ Image {i+1}: Failed")

Quick Usage (Convenience Function)

from python_midjourney import quick_generate

# Generate image in one line
result = quick_generate(
    "fantasy castle in the clouds",
    user_token="your_token",  # Optional
    channel_id="your_channel", # Optional
    auto_split=True
)

Disable Auto-Split

from python_midjourney import MidjourneyClient

# Save original image only (no splitting)
client = MidjourneyClient(auto_split=False)
result = client.generate_image("abstract art")

print(f"Original only saved: {result['original_file']}")
# split_files will be an empty list

Check Channel Information

from python_midjourney import MidjourneyClient

client = MidjourneyClient()

# Get current channel information
info = client.get_channel_info()
print(f"Channel name: {info['name']}")
print(f"Server ID: {info['guild_id']}")

# Change to another channel
client.set_channel("another_channel_id")

๐Ÿ“‚ Result File Structure

your_project/
โ”œโ”€โ”€ images/                    # Original images
โ”‚   โ”œโ”€โ”€ 20231201_143022_sunset_mountains_midjourney.png
โ”‚   โ””โ”€โ”€ 20231201_143122_cyberpunk_city_midjourney.png
โ””โ”€โ”€ images/split/              # Split images
    โ”œโ”€โ”€ 20231201_143022_sunset_mountains_midjourney_part1.png
    โ”œโ”€โ”€ 20231201_143022_sunset_mountains_midjourney_part2.png
    โ”œโ”€โ”€ 20231201_143022_sunset_mountains_midjourney_part3.png
    โ””โ”€โ”€ 20231201_143022_sunset_mountains_midjourney_part4.png

๐Ÿ› ๏ธ API Reference

MidjourneyClient

Constructor

MidjourneyClient(
    user_token: str = None,     # Discord user token
    channel_id: str = None,     # Midjourney channel ID  
    auto_split: bool = True     # Whether to auto-split
)

Methods

generate_image()
generate_image(
    prompt: str,                # Image generation prompt
    channel_id: str = None,     # Channel ID (default: set during initialization)
    timeout: int = 60           # Timeout in seconds
) -> Optional[Dict[str, Any]]

Return value:

{
    'success': True,
    'prompt': 'your prompt',
    'original_file': 'images/xxx.png',
    'split_files': ['images/split/xxx_part1.png', ...],
    'message_id': 'discord_message_id',
    'timestamp': '2023-12-01T14:30:22.123456',
    'total_files': 5
}
generate_batch()
generate_batch(
    prompts: List[str],         # List of prompts
    channel_id: str = None,     # Channel ID
    delay: int = 5              # Delay between requests in seconds
) -> List[Dict[str, Any]]
set_channel()
set_channel(channel_id: str)    # Change default channel
get_channel_info()
get_channel_info(
    channel_id: str = None      # Channel ID (default: currently set channel)
) -> Dict[str, Any]

Convenience Functions

quick_generate()

quick_generate(
    prompt: str,                # Image generation prompt
    user_token: str = None,     # Discord user token
    channel_id: str = None,     # Channel ID
    auto_split: bool = True     # Whether to auto-split
) -> Optional[Dict[str, Any]]

โš ๏ธ Important Notes

1. Discord Terms Compliance

  • Using user tokens may violate Discord's Terms of Service
  • Use only for personal projects
  • Commercial use prohibited

2. Token Security

  • Never share your user token
  • Don't upload tokens to public repositories like GitHub
  • Use environment variables or .env files

3. Usage Limitations

  • Requires paid Midjourney subscription
  • Must comply with Discord API rate limits
  • Avoid excessive requests

4. Technical Limitations

  • Internet connection required
  • Dependent on Discord server status
  • Dependent on Midjourney bot status

๐Ÿ”ง Troubleshooting

Common Errors

1. "Discord User Token not set"

# Check environment variable
echo $DISCORD_USER_TOKEN

# Set environment variable
export DISCORD_USER_TOKEN="your_token_here"

2. "Midjourney Channel ID not set"

# Set channel ID
export MIDJOURNEY_CHANNEL_ID="your_channel_id_here"

3. "Failed to get channel info: 403"

  • Token is incorrect or expired
  • No access permission to the channel
  • Need to check Discord account status

4. "Timeout: No response from Midjourney"

  • Midjourney bot is offline or response delayed
  • Try increasing timeout value (default: 60 seconds)
  • Check network connection

Debugging Tips

from python_midjourney import MidjourneyClient

# Debug with detailed output
client = MidjourneyClient()

# Check channel connection
try:
    info = client.get_channel_info()
    print("โœ… Channel connection successful:", info['name'])
except Exception as e:
    print("โŒ Channel connection failed:", e)

# Test image generation
result = client.generate_image("test image", timeout=120)
if result:
    print("โœ… Image generation successful")
else:
    print("โŒ Image generation failed")

๐Ÿ“ License

This project is distributed under the MIT License. See the LICENSE file for details.

๐Ÿค Contributing

  1. Fork this repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ž Support

๐Ÿ™ Acknowledgments

  • Midjourney - Amazing AI art generation service
  • Discord - Powerful communication platform
  • Pillow - Python image processing library

โš ๏ธ Disclaimer: This package is created for educational and personal use purposes. Please comply with Discord's and Midjourney's Terms of Service when using.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_midjourney-1.0.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_midjourney-1.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file python_midjourney-1.0.0.tar.gz.

File metadata

  • Download URL: python_midjourney-1.0.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for python_midjourney-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2c4cf8cac1610893094eea6b2eb0122c54c1c0207d3650b37ff52204451023e4
MD5 e4a6400fdd58691e21cf34b0cd7aab3e
BLAKE2b-256 8df413f44eaa5cf44798fbd9b811644e629dafa221a060a92ed3c56ce82f3eef

See more details on using hashes here.

File details

Details for the file python_midjourney-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_midjourney-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c46e3a617474c13c8b57632e296300c76ed4ab5f3daab74b8b5c903583a2d1b2
MD5 2e48390a99073c868c21aeb0b37c654b
BLAKE2b-256 376a465a8559fdc1682966e6202d6ee1de17a8ea03ff157296ed9e51a1e6f511

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page