-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordHook.txt
75 lines (65 loc) · 2.71 KB
/
DiscordHook.txt
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
Script("Name") = "DiscordHook"
Script("Author") = "Pyro"
Script("Major") = 0
Script("Minor") = 2
Script("Revision") = 20180106
Script("Description") = "Sends chat events to a Discord webhook."
Public HookUrl ' The URL of the webhook
Public ShowJoins ' "True" if join/leave messages should be sent.
Public ChannelFilter ' If set, only shows events from the specified channel.
Public UseDiscordNameField ' "True" to use the discord username field for messages (unreliable)
Sub Event_Load()
HookUrl = GetSettingsEntry("Hook")
ShowJoins = CBool(LCase(GetSettingsEntry("ShowJoins")) = "true")
ChannelFilter = GetSettingsEntry("Channel")
UseDiscordNameField = CBool(LCase(GetSettingsEntry("UseDiscordNameField")) = "true")
End Sub
Sub Event_UserTalk(Username, Flags, Message, Ping)
If Len(ChannelFilter) = 0 Or StrComp(Channel.Name, ChannelFilter, vbTextCompare) = 0 Then
Send Username, Message
End If
End Sub
Sub Event_MessageSent(MessageID, Message, Tag)
If Len(ChannelFilter) = 0 Or StrComp(Channel.Name, ChannelFilter, vbTextCompare) = 0 Then
If Left(Message, 1) <> "/" Then
Send Channel.Self.Name, Message
End If
End If
End Sub
Sub Event_UserJoins(Username, Flags, Message, Ping, Product, Level, StatString, Banned)
If ShowJoins And (Len(ChannelFilter) = 0 Or StrComp(Channel.Name, ChannelFilter, vbTextCompare) = 0) Then
Send "", StringFormat("{0} [{1}ms] has joined the channel using {2}.", Username, Ping, Product)
End If
End Sub
Sub Event_UserLeaves(Username, Flags)
If ShowJoins And (Len(ChannelFilter) = 0 Or StrComp(Channel.Name, ChannelFilter, vbTextCompare) = 0) Then
Send "", StringFormat("{0} has left the channel.", Username)
End If
End Sub
Sub Send(user, msg)
user = DoReplace(user)
msg = DoReplace(msg)
data = "{"
If Len(user) > 0 Then
If UseDiscordNameField Then
data = data & Chr(34) & "username" & Chr(34) & ":" & Chr(34) & user & Chr(34) & ","
Else
msg = "<" & user & "> " & msg
End If
End If
data = data & Chr(34) & "content" & Chr(34) & ":" & Chr(34) & msg & Chr(34) & "}"
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", HookUrl
.SetRequestHeader "Content-Type", "application/json"
.Send data
End With
End Sub
Function DoReplace(msg)
DoReplace = Replace(msg, "\", "\\")
DoReplace = Replace(DoReplace, Chr(8), "\b")
DoReplace = Replace(DoReplace, Chr(9), "\t")
DoReplace = Replace(DoReplace, Chr(10), "\n")
DoReplace = Replace(DoReplace, Chr(12), "\f")
DoReplace = Replace(DoReplace, Chr(13), "\r")
DoReplace = Replace(DoReplace, Chr(34), "\" & Chr(34))
End Function