Visual Basic Irc Bot
Visual Basic Irc Bot
Hello Dudes And Dudets Lol , This Is My First Post And Tutorial . Lets Get Straight To It
Alright, Start up A New Standard Eex Project. Change Around Your Project Properties If Needed, Like The Project Name. Name Our Initial Form: FrmMain. Though The Bot Will Eventually Be Invisible, For Debugging Purposes We'll Want To Add A Textbox That Tells Us What's Going On For Now. So Add A New Textbox And Drag It To Fill The Entire Form [You Can Resize The Form, Too, Of Course). Change The Properties Of The Textbox To:
Code:
Name: txtLog
MultiLine: True
ScrollBars: Verticle
Locked: True.
Text: (empty)
Open Up The Code View And Add The Followin Function:
Code:
Public Function addLog(Message As String)
txtLog.Text = txtLog & "[" & Time & "]: " & Message & vbCrLf
End Function
So Now When We Want To Output Some Info To The Log Window, We Use: AddLog "Message".
So Let's Add A Winsock Control (Ctrl+T And Check Microsoft WinSock Control SP5) Drag It Onto Our Form And Name It WskIRC.
In Our Form_Load Let's Add The Code To Select And Connect To Our Irc Server.
Code:
Private Sub Form_Load()
wskIRC.RemoteHost = "irc.efnet.net"
wskIRC.RemotePort = 6667
addLog "Connecting to " & wskIRC.RemoteHost & _
" port " & wskIRC.RemotePort & "..."
wskIRC.Connect
End Sub
Note: Replace "Irc.efnet.net" With Your Irc Server Of Your Choice. I Would Recommend Against Using A Public IRC Server, But For Testing It Shouldn't Matter. Also, If You Use A Server Like efnet.net, Chances Are, Your Bots Will End Up On Different Servers Because Efnet Is A Network Of Servers.
On The wskIRC_Connect Event, We'll Generate A Name With The Following Code
Code:
Private Sub wskIRC_Connect()
addLog "Connected!"
Dim Name As String
Dim Random As Integer
Dim Channel as String ' we'll be using this in the next step
Randomize
Random = Rnd * 1000
Name = "[BOT]-" & Random ' Example: [BOT]-723
addLog "Username: " & Name
End Sub
Now We'll Tell The IRC Server What Our Username Is And Yry And Join A Channel. [This Is Inside The Same Sub]
Code:
...
wskIRC.SendData "NICK " & Name & vbCrLf
wskIRC.SendData "USER bot bot bot bot" & vbCrLf 'Lazily Coded But It Works
Channel = "#bot"
wskIRC.senddata "JOIN " & Channel & vBCrLf
End Sub
Now, Let's Make A wskIRC_DataArrival Sub And Add The Folowing Codes That'll Add The Recieved Data To The Log:
Code:
Private Sub wskIRC_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
wskIRC.GetData Data
addLog Data
End Sub
So If You Run It, You Should Get The Irc Server's MOTD (Message Of The Day) And Anything Else It Would Send A Client.
Now, Open Up A IRC Client, Like Mirc Or X-chat And Join The Same Channel As The Bot, And You Should See It In The Users List biggrin.gif.
If You Leave The Bot Long Enough, You'll Notice That It Gest Disconnected This Is Because The IRC Servor Tries To "Ping" The Bot, But The Bot Won't "Pong" Back.
So Add This Inside The DataArrival:
Code:
If InStr(Data, "PING") Then
wskIRC.SendData Replace(Data, "PING", "PONG") & vbCrLf
addLog Replace(Data, "PING", "PONG")
End If
So Now Let's Send It A Message And See What Gest Sent To The Bot:
Code:
In The Chatroom:
!Pretend.This.Is.A.Command
----
The bot saw:
[1:08:47 PM]: :Propaine!DMZ@** PRIVMSG #bot :!Pretend.This.Is.A.Command
If It Looks Something Like That, Then We Can Search For The Text ":!" And Whatever After That Is The Command. If You Want To Use Another Symbol, Like "." Then You'd Search For ":.".
So The Code Would Be: [Below AddLog Data]
Code:
If InStr(Data, ":!") Then
ircCommand = Split(Data, ":!")(1)
addLog "Command: " & ircCommand
End If
So Now, To Add Commands, You'd Just Put This Somewhere Inside Of Your DataArrival Near The Bottom:
Code:
If InStr(ircCommand, "test") Then
MsgBox "it works"
End If
So Here Is The Final Code, Incase You Missed Something:
Code:
Public Function addLog(Message As String)
txtLog.Text = txtLog & "[" & Time & "]: " & Message & vbCrLf
End Function
Private Sub Form_Load()
wskIRC.RemoteHost = "irc.efnet.net"
wskIRC.RemotePort = 6667
addLog "Connecting to " & wskIRC.RemoteHost & _
" port " & wskIRC.RemotePort & "..."
wskIRC.Connect
End Sub
Private Sub wskIRC_Connect()
addLog "Connected!"
Dim Name As String
Dim Random As Integer
Dim Channel As String
Randomize
Random = Rnd * 1000
Name = "[BOT]-" & Random
addLog "Username: " & Name
wskIRC.SendData "NICK " & Name & vbCrLf
wskIRC.SendData "USER bot bot bot bot" & vbCrLf
Channel = "#bot"
wskIRC.SendData "JOIN " & Channel & vbCrLf
End Sub
Private Sub wskIRC_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
wskIRC.GetData Data
addLog Data
Dim ircCommand As String
If InStr(Data, ":!") Then
ircCommand = Split(Data, ":!")(1)
addLog "Command: " & ircCommand
End If
If InStr(Data, "PING") Then
wskIRC.SendData Replace(Data, "PING", "PONG") & vbCrLf
addLog Replace(Data, "PING", "PONG")
End If
If InStr(ircCommand, "test") Then
MsgBox "it works"
End If
End Sub
----
by Propaine xx edited by nad


