How to Automatically Post Images into a Discord Channel

Spread the love

Do you run a Discord server and would like to automatically post images to a specific channel at regular intervals without lifting a finger? This article will show you how to automatically upload images into a Discord channel using the Discord-AIU tool and custom scripts.

Content

Our Objective and Toolkit

Imagine you’re running a Discord server dedicated to anime, and you want to keep your members engaged with fresh fan art published every day. The idea seems simple to implement: you just need to remember to post an image every 24 hours, right? Wrong. Life gets busy, and before you know it, your members are screaming at you, wondering where their daily dose of anime goodness has gone. That’s where automation comes in to save the day – and your sanity.

To automatically post images to your Discord channel, we’ll be using a combination of tools and scripts. Our main objective is to set up a system that will regularly upload images to your specified Discord channel without any manual intervention. We’ll be using the Discord-AIU (Automatic Image Uploader) tool as our foundation, along with some custom scripts to handle the scheduling and image selection process.

Installing Discord-AIU

Now that we’ve outlined our objective, let’s create our image upload bot using Discord-AIU. First, you’ll need to download Discord-AIU from its GitHub repository. I recommend you grab the latest version and unpack it in a suitable location. The tool is developed in Python, so you can run it on any major operating system.

Once you’ve downloaded Discord-AIU, you need to install the necessary Python dependencies. If Python is installed on your system, then you can just open your terminal or command prompt, navigate to the Discord-AIU directory, and run the command pdm install (you may also need to install PDM to execute it).

The command will install all the required packages for the tool to function properly, namely pillow, python-dotenv, and requests.

Create a Webhook Endpoint for Your Channel

The next step is to create a webhook endpoint in your Discord server. This webhook is the connection point between Discord-AIU and your server. To do this, open your Server Settings and navigate to the Integrations tab.

Click on the Create Webhook button, and you’ll be presented with options to customize your webhook. You can edit the avatar, choose the channel where images will be posted, and give your webhook a name.

After setting these up, you can click the Copy Webhook URL button to obtain the URL of the newly created webhook.

Configure Discord-AUI Settings

With your webhook set up, it’s time to configure Discord-AIU. Open the Discord-AUI folder and navigate to the src subfolder.

There, look for a file called discord_aiu.py and open it in your favorite text editor. Paste your Webhook URL in the corresponding configuration option near the top of the file. It should look something like this:

WEBHOOK_URL = "https://discord.com/api/webhooks/<YOUR_WEBHOOK_ID>/YOUR_WEBHOOK_TOKEN"

I also recommend you modify the FOLDER_PATH configuration option based on your preferences. By default, it’s set to:

FOLDER_PATH = './images/'

This path is relative to the location of the discord_aiu.py file. In other words, the “images” folder should be created in the same directory as the discord_aiu.py script. Feel free to save the file and create the folder now.

Automatically Post Multiple Images to Discord

To test if Discord-AUI works as it should, place one or more sample images in your newly specified images folder. Then, open your terminal or command prompt, navigate to the Discord-AIU directory, and run the following command:

pdm run ./src/discord_aiu.py

If everything is set up correctly, you should see all test images appear in the designated Discord channel within moments. You can also look at the tool’s output, which should tell you exactly how many images have been successfully uploaded.

The only problem is that the command always uploads all images in the specified folder in bulk. While that can be incredibly useful and time-saving if you need to share a lot of images with your community, our goal is to upload one image at a time to Discord automatically at regular intervals. Fortunately, all we need is a custom script.

Schedule Discord-AUI to Execute at Regular Intervals

Now that we’ve confirmed Discord-AUI works, let’s automate the process to post images at regular intervals. We’ll create a script that performs the following tasks:

  1. Picks a random file from a queue folder.
  2. Moves it to the images folder.
  3. Executes the Discord-AUI tool.
  4. Moves the uploaded image to an “uploaded” folder.

If you’re using Windows, then you need to create a new plaintext file (you can use Notepad) and copy and paste the following code:

$QUEUE_FOLDER = "C:\path\to\Discord-AIU\src\queue"
$IMAGES_FOLDER = "C:\path\to\Discord-AIU\src\images"
$UPLOADED_FOLDER = "C:\path\to\Discord-AIU\src\uploaded"
$DISCORD_AIU_PATH = "C:\path\to\Discord-AIU\src\discord_aiu.py"
 
# Pick a random file from the queue
$RANDOM_FILE = Get-ChildItem -Path $QUEUE_FOLDER | Get-Random -Count 1
 
if ($RANDOM_FILE) {
    # Move the file to the images folder
    Move-Item -Path $RANDOM_FILE.FullName -Destination $IMAGES_FOLDER
 
    # Run Discord-AIU
    Set-Location -Path "C:\path\to\Discord-AIU"
    pdm run $DISCORD_AIU_PATH
 
    # Move the processed file to the uploaded folder
    $PROCESSED_FILE = Get-ChildItem -Path $IMAGES_FOLDER
    Move-Item -Path $PROCESSED_FILE.FullName -Destination $UPLOADED_FOLDER
}

Save the script as script.ps1 or something similar (.ps1 is the extension of PowerShell scripts).

Linux and macOS users can create a script.sh file with the following bash script:

#!/bin/bash
 
QUEUE_FOLDER="/path/to/Discord-AIU/src/queue"
IMAGES_FOLDER="/path/to/Discord-AIU/src/images"
UPLOADED_FOLDER="/path/to/Discord-AIU/src/uploaded"
DISCORD_AIU_PATH="/path/to/Discord-AIU/src/discord_aiu.py"
 
# Pick a random file from the queue
RANDOM_FILE=$(find "$QUEUE_FOLDER" -type f | shuf -n 1)
 
if [ -n "$RANDOM_FILE" ]; then
    # Move the file to the images folder
    mv "$RANDOM_FILE" "$IMAGES_FOLDER"
 
    # Run Discord-AIU
    cd /path/to/Discord-AIU && pdm run "$DISCORD_AIU_PATH"
 
    # Move the file to the uploaded folder
    mv "$IMAGES_FOLDER"/* "$UPLOADED_FOLDER"
fi

Don’t forget to create the necessary folders and modify the paths to reflect their locations, as well as the location of the Discord-AIU folder itself. You also need to make the script executable using the chmod +X /path/to/script command.

Finally, you need to schedule the script.ps1 (Windows) file or the script.sh (Linux and macOS) file to run automatically – let’s say once a day.

As a Windows user, you can open the Task Scheduler by searching for it in the Start menu. Click on Create Basic Task in the right panel and give your task a name, such as “Discord Image Uploader”.

Choose Daily for the trigger and set your preferred time. For the action, select Start a program. In the Program/script field, enter “powershell”, and in the Add arguments field, enter -ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1".

If you’re using Linux or macOS, you can open your terminal and type

crontab -e

to edit your cron jobs (you can also use sudo EDITOR=nano crontab -e to use the Nano editor instead of Vim).

Add a new line to run the script daily at 12:00 PM (noon), which looks like this:

0 12 * * * /path/to/your/script.sh

Save and exit the editor.

With these steps completed, your chosen script will run automatically every day, keeping your Discord channel fresh with new images. Just remember to keep your queue folder stocked with new images to ensure a steady stream of content. If you would like to make your server an even more entertaining place to visit, then you can follow up by learning how to turn your Raspberry Pi into a Discord radio station.

Image and screenshots by David Morelo.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


David Morelo
Staff Writer

David Morelo is a professional content writer in the technology niche, covering everything from consumer products to emerging technologies and their cross-industry application. His interest in technology started at an early age and has only grown stronger over the years.

Leave a comment