forked from couleur-tweak-tips/CTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ps1
143 lines (104 loc) · 3.74 KB
/
lib.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
function Post-Webhook {
param(
[Parameter(ValueFromPipeline = $true)]
[System.IO.FileInfo]$Path
)
$Payload = Get-Content $Path | ConvertFrom-Yaml
$Keys = [Array]$Payload.Keys
if ('channel' -notin $Keys) {
Write-Warning "Webhook ``$($Path.Name)`` needs the following keys: secret, message_link and channel"
return
}
Write-Warning "Attempting to Post $($Path.Name) with secret $($Payload.channel)"
$Parameters = @{
Uri = ("https://discord.com/api/webhooks/" + (Get-Item "env:$($Payload.channel)").Value)
Headers = @{ "Content-Type" = "application/json" }
}
if ($Payload.message_link){
# Then we're modifying an existing message, we need to specify which via the message link
$Parameters.Uri += "/messages/$($Payload.message_link)"
$Parameters.Method = "Patch"
} else {
$Parameters.Method = "Post"
}
if ($Payload.thread_id){
# Then we need to provide context over which creation/modification this is in
$Parameters.Uri += "?thread_id=$($Payload.thread_id)"
}
$Parameters += @{
Body = Format-Embed $Payload.clone()
}
Write-Debug ($Parameters | ConvertTo-Yaml)
Invoke-WebRequest @Parameters
}
# Returns a JSON string to feed to the GitHub API
# Strips pings (parse_mentions)
function Format-Embed {
param(
[Hashtable]$Payload
)
foreach ($CTT_Keyword in ("channel", "message_link", "thread_id")) {
$Payload.remove($CTT_Keyword)
}
# Blocks out any potential mentions
# https://birdie0.github.io/discord-webhooks-guide/structure/allowed_mentions.html
$Payload."allowed_mentions" = @{
"parse" = @()
"roles" = @()
}
return $Payload | ConvertTo-Json -Depth 5
}
function ConvertFrom-WebHook {
param(
[Parameter(ValueFromPipeline = $true)]
[Hashtable]$Webhook
)
Write-Output $Webhook.content
$Webhook.embeds | Where-Object Title | ForEach-Object {
if (($author = $_.author) -and $_.author.name){
$author_str = "`n"
if ($author.icon_url){
$author_str += "<img class='author' src=`"$($author.icon_url)`" alt=`"author`" width=`"16`"/> "
}
if ($author.url){
$author_str += "[$($author.name)]($($author.url))"
}else{
$author_str += $author.name
}
Write-Output $author_str
}
Write-Output "### $($_.Title)"
if ($thumbnail = $_.thumbnail.url){
Write-Output "`n<img align=`"right`" src=`"$thumbnail`" />`n"
}
Write-Output "`n$($_.description)"
if ($_.fields){
if ($_.fields.inline -eq $true -and $_.fields.length -le 3){
Write-Output ($_.fields.name -join " | ")
Write-Output ((0..3 | ForEach-Object {"|"}) -join "-")
Write-Output ($_.fields.value -join " | ")
} else {
$_.fields | ForEach-Object {
Write-Output "#### $($_.name)"
Write-Output "`n$($_.value)`n"
}
}
}
if ($thumb = $_.thumbnail.url){
Write-Output "`n![]($thumb)`n"
}
if ($footer = $_.footer){
$footer_str = ""
if ($footer.icon_url){
$footer_str += "<img class='footer_icon' src=`"$($footer.icon_url)`" alt=`"author`" width=`"16`"/> "
}
if ($footer.text){
$footer_str += $footer.text
}else{
$footer_str += $footer.name
}
Write-Output $footer_str
# Write-Output "#### $($footer.text)"
}
}
}