-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSend-DiscordFile.ps1
54 lines (43 loc) · 1.57 KB
/
Send-DiscordFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function Send-DiscordMessageWithFile {
[CmdletBinding()]
param (
[Parameter()]
$ChannelId,
$BotToken,
$FolderPath,
$FileName
)
# Don't leak your bot token
if ($null -eq $BotToken) {
$BotToken = $env:BotToken
}
$Headers = @{
"Authorization" = "Bot $BotToken"
"User-Agent" = "PSDCBot (blabla, v0.2)"
}
# Thanks to https://stackoverflow.com/questions/68677742/multipart-form-data-file-upload-with-powershell
$FileName = Split-Path $FilePath -Leaf
$boundary = [System.Guid]::NewGuid().ToString()
$TheFile = [System.IO.File]::ReadAllBytes($FilePath)
$TheFileContent = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($TheFile)
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"Description`"`r`n",
"File uploaded by a bot",
"--$boundary",
"Content-Disposition: form-data; name=`"TheFile`"; filename=`"$FileName`"",
"Content-Type: application/json`r`n",
$TheFileContent,
"--$boundary--`r`n"
) -join "`r`n"
Invoke-RestMethod -Uri "https://discord.com/api/v9/channels/$ChannelId/messages" -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines -Headers $Headers
}
if ($FolderPath) {
$items = Get-ChildItem -Path $FolderPath -Recurse
foreach ($item in $items) {
Send-DiscordMessageWithFile -ChannelId $ChannelId -FilePath $item.FullName
}
}
if ($FileName) {
Send-DiscordMessageWithFile -ChannelId $ChannelId -FilePath $FileName
}