-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.vb
69 lines (54 loc) · 2.46 KB
/
Form1.vb
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
Imports System.ComponentModel
Imports Discord ' NuGet Discord.Net
Imports Discord.WebSocket ' NuGet Discord.Net
Public Class Form1
Dim WithEvents discordEv As New DiscordSocketClient
Private Async Function Connect(tt As TokenType, tstr As String) As Task
Await discordEv.LoginAsync(tt, tstr)
Await discordEv.StartAsync()
Await Task.Delay(-1)
End Function
Private Async Function Disconnect() As Task
Await discordEv.LogoutAsync()
End Function
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AddHandler discordEv.Ready, AddressOf onReady
AddHandler discordEv.MessageReceived, AddressOf onMessage
' Connect Discord
Try
Await Connect(TokenType.Bot, "############### YOUR oAuthToken ###############")
Await discordEv.StartAsync()
Catch ex As Exception
Dim result As Integer = MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Sub
Private Async Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
' Disconnect Discord
If discordEv.LoginState = LoginState.LoggedIn Then
Await Disconnect()
End If
Await discordEv.StopAsync()
End Sub
Private Delegate Sub mUpdateCaption(ByVal sCaption As String)
Private Sub UpdateCaption(ByVal sCaption As String)
If Me.InvokeRequired() Then
Dim udcap As New mUpdateCaption(AddressOf UpdateCaption)
Me.Invoke(udcap, sCaption)
Else
Me.Text = sCaption
End If
End Sub
Private Function onReady() As Task
UpdateCaption("Logged on as: " + discordEv.CurrentUser.Username) 'LoggedIn event still dosent capture CurrentUser info, so stuck with this.
Return Task.CompletedTask
End Function
Private Async Function onMessage(message As SocketMessage) As Task
'MsgBox(message.Author.Username & vbCrLf & message.Content) ' MsgBox is just an example do what you want
If message.Author.Id = discordEv.CurrentUser.Id Then 'Check if self message
'The reason this if statement is here, is because you get sent back the messages that you send.
'Ignore
Else
Await message.Channel.SendMessageAsync("Test") 'Bot must have user permissions in the channel to send message, unless it was a private DM
End If
End Function
End Class