An example bot for Discord written in PowerShell for Azure Functions.
This is based on a presentation give at the PowerShell Summit 2023 (PPTX + Code here).
If you have suggestions to improve this, please submit a PR!
If you have questions or trouble getting this to work, feel free to open an issue and I will do my best to help.
- Create a Discord Application in the Discord Developer portal
- Add a bot to your Discord Application
- Create a URL with at least the following permissions:
- bot
- application.commands
- bot\Send Messages
- Navigate to that URL using your browser and add the bot to your server.
- Copy the Public Key
- Create a Function App (Linux w/PowerShell 7.2+)
- Add the public key of your Discord bot to the app configuration as
DISCORD_PUBLIC_KEY
. - Add one of the function's keys into the app configuration as
FUNCTIONKEY
- Deploy this repository to the application using VS Code (needs Azure and Azure Functions extensions)
- Copy the HTTPStart function URL
- Ensure the Function App is warm (wait for the monitor to run or send a few POST requests)
- Paste the HTTPStart function URL (including key) into the Interactions Endpoint URL field.
You will not be able to save unless you have the function app configured properly!
Using the Discordant module:
# Authenticate with bot token (from Discord dev portal):
Connect-Discord -RestClient -TokenType Bot -Token '<token>'
# Get your guild (server):
$guild = Get-DiscordGuild | Where-Object { $_.Name -eq 'Bot Testing' }
# No parameters
# this command is already supported out of the box by the bot
New-DiscordGuildCommand -Name 'hello' -Description "Say hello to the bot" -Guild $guild -CommandBuilder (
New-DiscordSlashCommand -Name 'hello' -Description 'Say Hello to the bot'
)
# Single string parameter
New-DiscordGuildCommand -Name 'start-server' -Description 'Start a server' -Guild $guild -CommandBuilder (
New-DiscordSlashCommand -Name 'start-server' -Description 'Start a server' -Options @(
(New-DiscordSlashCommandOption -Name 'server' -Description 'The selected server' -Type String)
)
)
Both of the above example commands already exist in the Orchestrator. To add more, simply extend the switch
statement.