Automate Your Discord Server in Windows using curl and PowerShell

Looking to fill your Discord server with random facts, jokes, quotes, and other content? In this tutorial, we’ll show you how to use Windows PowerShell and the command-line tool curl to retrieve data from the ViewBits API and send it to your Discord server using a webhook.

With just a few lines of code, you can automate the process of sending data from the ViewBits API to your server, adding some fun and additional engagement to your community. Follow along with the step-by-step instructions below to get started.

1. Create Discord Webhook

Create a webhook in your Discord server by first navigating to the desired channel. Click on the edit channel gear icon, then navigate to Integrations. From the integration settings, click on View Webhooks.

You can create a new webhook or select from an existing entry if it exists, then click on the Copy Webhook URL button. The name and icon you choose for the Webhook will appear in the channel when posting messages.

If you get stuck, check out the official Discord documentation for more information on how to create a webhook: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks

2. Use PowerShell to send API data to Discord

Use the following PowerShell command to send a random fact to Discord. Replace YOUR_WEBHOOK_URL_HERE with the actual webhook URL for your Discord server.

Check out the Full API Documentation for more data you can use!

# Get a random useless fact from ViewBits
$fact = (Invoke-RestMethod -Uri "https://api.viewbits.com/v1/uselessfacts?mode=batch")[0]

# Define the webhook URL
$url = "YOUR_WEBHOOK_URL_HERE"

# Define the message payload
$payload = @{
content = "$($fact.text) - $($fact.source)"
} | ConvertTo-Json

# Define the headers
$headers = @{
'Content-Type' = 'application/json'
}

# Send the message to the Discord server using Invoke-WebRequest
Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $payload

You can save this PowerShell script as a .ps1 file and run it using the PowerShell console or the PowerShell ISE.

Run as Batch File

If you want to run the script from a batch file, you can use the powershell.exe command to execute the PowerShell script. Here’s an example:

@echo off

powershell.exe -ExecutionPolicy Bypass -File "C:\Path\To\Your\Script.ps1"

pause

Save this file as a .bat file and simply double-click the file to run the bot. The pause command will keep the window open so that you can view any errors that may be returned.

Run as a Scheduled Task

You can use Windows Task Scheduler to run your batch file automatically based on your required criteria. You may want to modify the .ps1 file to close the cmd window after 5 seconds:

  1. Remove “pause” from batch file.
  2. Add the following to your .ps1 file to add the delay:
# REM Pause the script execution for 5 seconds
Start-Sleep -Seconds 5

# REM Exit the PowerShell console window
exit

This part is completely optional if you want to monitor the bot for any errors, otherwise you can just run the batch file as a scheduled task based on your criteria. Check out this detailed guide for more information on Windows Scheduled Tasks.