-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendFileToDiscord.ps1
223 lines (191 loc) · 7.58 KB
/
SendFileToDiscord.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
[cmdletbinding()]
param(
[Parameter(Mandatory)]
[string]
$WebhookUrl,
[Parameter(Mandatory, ParameterSetName = 'file')]
[string]$FilePath
)
class DiscordFile {
[string]$FilePath = [string]::Empty
[string]$FileName = [string]::Empty
[string]$FileTitle = [string]::Empty
[System.Net.Http.MultipartFormDataContent]$Content = [System.Net.Http.MultipartFormDataContent]::new()
[System.IO.FileStream]$Stream = $null
DiscordFile([string]$FilePath) {
$this.FilePath = $FilePath
$this.FileName = Split-Path $filePath -Leaf
$this.fileTitle = $this.FileName.Substring(0,$this.FileName.LastIndexOf('.'))
$fileContent = $this.GetFileContent($FilePath)
$this.Content.Add($fileContent)
}
[System.Net.Http.StreamContent]GetFileContent($filePath) {
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = $this.fileTitle
$fileHeader.FileName = $this.FileName
$fileContent = [System.Net.Http.StreamContent]::new($fileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("text/plain")
$this.stream = $fileStream
return $fileContent
}
}
function Invoke-PayloadBuilder {
[cmdletbinding()]
param(
[Parameter(Mandatory)]
$PayloadObject
)
process {
$type = $PayloadObject | Get-Member | Select-Object -ExpandProperty TypeName -Unique
switch ($type) {
'DiscordEmbed' {
[boolean]$createArray = $true
#check if array
$PayloadObject.PSObject.TypeNames | ForEach-Object {
switch -Regex ($_) {
'^System\.Collections\.Generic\.List.+' {
$createArray = $false
}
'System.Array' {
$createArray = $false
}
'System.Collections.ArrayList' {
$createArray = $false
}
}
}
if (-not ($createArray)) {
$payload = [PSCustomObject]@{
embeds = $PayloadObject
}
}
else {
$embedArray = New-Object 'System.Collections.Generic.List[DiscordEmbed]'
$null = $embedArray.Add($PayloadObject)
$payload = [PSCustomObject]@{
embeds = $embedArray
}
}
}
'System.String' {
if (Test-Path $PayloadObject -ErrorAction SilentlyContinue) {
$payload = [DiscordFile]::New($payloadObject)
}
else {
$payload = [PSCustomObject]@{
content = ($PayloadObject | Out-String)
}
}
}
}
}
end {
return $payload
}
}
function Invoke-PSDsHook {
[cmdletbinding()]
param(
[Parameter(ParameterSetName = 'createDsConfig')]
[string]$CreateConfig,
[Parameter()]
[string]$WebhookUrl,
[Parameter(Mandatory,ParameterSetName = 'file')]
[string]$FilePath,
[Parameter()]
[string]$ConfigName = 'config',
[Parameter(ParameterSetName = 'configList')]
[switch]$ListConfigs,
[Parameter(ParameterSetName = 'embed', Position = 0)]
$EmbedObject,
[Parameter(ParameterSetName = 'simple')]
[string]$HookText
)
begin {
#Create full path to the configuration file
$configPath = "$($configDir)$($separator)$($ConfigName).json"
#Ensure we can access the path, and error out if we cannot
if (!(Test-Path -Path $configPath -ErrorAction SilentlyContinue) -and !$CreateConfig -and !$WebhookUrl) {
throw "Unable to access [$configPath]. Please provide a valid configuration name. Use -ListConfigs to list configurations, or -CreateConfig to create one."
}
elseif (!$CreateConfig -and $WebhookUrl) {
$hookUrl = $WebhookUrl
Write-Verbose "Manual mode enabled..."
}
elseif ((!$CreateConfig -and !$WebhookUrl) -and $configPath) {
#Get configuration information from the file specified
$config = [DiscordConfig]::New($configPath)
$hookUrl = $config.HookUrl
}
}
process {
switch ($PSCmdlet.ParameterSetName) {
'embed' {
$payload = Invoke-PayloadBuilder -PayloadObject $EmbedObject
Write-Verbose "Sending:"
Write-Verbose ""
Write-Verbose ($payload | ConvertTo-Json -Depth 4)
try {
Invoke-RestMethod -Uri $hookUrl -Body ($payload | ConvertTo-Json -Depth 4) -ContentType 'Application/Json' -Method Post
}
catch {
$errorMessage = $_.Exception.Message
throw "Error executing Discord Webhook -> [$errorMessage]!"
}
}
'file' {
if ($PSVersionTable.PSVersion.Major -lt 6) {
throw "Support for sending files is not yet available in PowerShell 5.x"
}
else {
$fileInfo = Invoke-PayloadBuilder -PayloadObject $FilePath
$payload = $fileInfo.Content
Write-Verbose "Sending:"
Write-Verbose ""
Write-Verbose ($payload | Out-String)
#If it is a file, we don't want to include the ContentType parameter as it is included in the body
try {
Invoke-RestMethod -Uri $hookUrl -Body $payload -Method Post
}
catch {
$errorMessage = $_.Exception.Message
throw "Error executing Discord Webhook -> [$errorMessage]!"
}
finally {
$fileInfo.Stream.Dispose()
}
}
}
'simple' {
$payload = Invoke-PayloadBuilder -PayloadObject $HookText
Write-Verbose "Sending:"
Write-Verbose ""
Write-Verbose ($payload | ConvertTo-Json -Depth 4)
try {
Invoke-RestMethod -Uri $hookUrl -Body ($payload | ConvertTo-Json -Depth 4) -ContentType 'Application/Json' -Method Post
}
catch {
$errorMessage = $_.Exception.Message
throw "Error executing Discord Webhook -> [$errorMessage]!"
}
}
'createDsConfig' {
[DiscordConfig]::New($CreateConfig, $configPath)
}
'configList' {
$configs = (Get-ChildItem -Path (Split-Path $configPath) | Where-Object {$PSitem.Extension -eq '.json'} | Select-Object -ExpandProperty Name)
if ($configs) {
Write-Host "Configuration files in [$configDir]:"
return $configs
}
else {
Write-Host "No configuration files found in [$configDir]"
}
}
}
}
}
# Call the function that will send the embed array to the webhook URL via the default configuration file
Invoke-PSDsHook -FilePath $FilePath -WebhookUrl $WebhookUrl