poshbotio / PoshBot

Powershell-based bot framework
MIT License
536 stars 108 forks source link

Discord User-Agent required #239

Closed phdavis closed 2 years ago

phdavis commented 2 years ago

Expected Behavior

When starting PoshBot using the DiscordBackend following the documentation expect it to connect without issue.

Current Behavior

When using PoshBot 0.13.0 and attempting to connect to Discord using Start-PoshBot various errors are thrown.

Write-Error: {"DataTime":"2022-01-08 03:26:57Z","Class":"DiscordConnection","Method":"ConnectGateway","Severity":"Error","LogLevel":"Info","Message":"Unable to determine Discord gateway URL","Data":{"CommandName":"Invoke-RestMethod","Message":"Response status code does not indicate success: 403 (Forbidden).","TargetObject":{"Version":{"Major":1,"Minor":1,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"VersionPolicy":0,"Content":{"Headers":[{"Key":"Content-Length","Value":["0"]}]},"Method":{"Method":"GET"},"RequestUri":"https://discord.com/api/gateway/bot","Headers":[{"Key":"Authorization","Value":["Bot X1Y2Z3"]},{"Key":"User-Agent","Value":["Mozilla/5.0","(Windows NT 10.0; Microsoft Windows 10.0.19043; en-US)","PowerShell/7.2.1"]}],"Properties":[],"Options":[]},"Position":"At C:\\Program Files\\WindowsPowerShell\\Modules\\PoshBot\\0.13.0\\PoshBot.psm1:7439 char:33\r\n+ … tewayInfo = Invoke-RestMethod -Uri 'https://discordapp.com/api/gateway/b …\r\n+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~","CategoryInfo":"InvalidOperation: (Method: GET, Reques…ontent-Length: 0\r\n}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException","FullyQualifiedErrorId":"WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"}}

Possible Solution

After digging into the code base a bit and looking at Discord's API reference it looks like they require a valid User-Agent to be passed. Although I can see one in the error response it doesn't seem like Discord likes it.

{"Key":"User-Agent","Value":["Mozilla/5.0","(Windows NT 10.0; Microsoft Windows 10.0.19043; en-US)","PowerShell/7.2.1"]}]

As a test I grabbed one of the lines invoking the API and added the UserAgent parameter (in PoshBot\0.13.0\PoshBot.psm1) $this.GatewayInfo = Invoke-RestMethod -Uri 'https://discordapp.com/api/gateway/bot' -Headers $headers -UserAgent 'PoshBot'

This responded without issue providing the URL that would be expected for the remainder to continue wss://gateway.discord.gg 1 @{total=1000; remaining=1000; reset_after=0; max_concurrency=1}

To further test I ended up editing the module itself and reloading it. After this I was able to re-import and run without any further issues. I also noticed https://discordapp.com/api being used in the code, but their documentation suggests https://discord.com/api. This doesn't appear to be an issue right now but thought I'd mention it.

Steps to Reproduce (for bugs)

  1. Launch pswh (7.2.1)
  2. Import PostBot module (0.13.0)
  3. Create Discord configuration per documentation
  4. Attempt to Start-PoshBot using supplied configuration

Context

Unable to use Discord backend without some tweaking.

Your Environment

PSVersion 7.2.1 PSEdition Core OS Microsoft Windows 10.0.19043 Script 0.13.0 PoshBot