' [Thresher IRC] Bot example
' (By Elektro)
'
' Instructions
' 1. Add a reference to 'Sharkbite.Thresher.dll'.
'
' Usage Examples:
' Public BOT As New IRCBot("irc.freenode.net", "#ircehn", "ElektroBot")
#Region " Imports "
Imports Sharkbite.Irc
#End Region
Public Class IRCBot
#Region " Members "
#Region " Properties "
''' <summary>
''' Indicates the IRC server to connect.
''' </summary>
Private Property Server As String = String.Empty
''' <summary>
''' Indicates the IRC channel to join.
''' </summary>
Private Property Channel As String = String.Empty
''' <summary>
''' Indicates the nickname to use.
''' </summary>
Private Property Nick As String = String.Empty
#End Region
#Region " Others "
''' <summary>
''' Performs the avaliable Bot commands.
''' </summary>
Public WithEvents BotConnection As Connection
''' <summary>
''' Handles the Bot events.
''' </summary>
Public WithEvents BotListener As Listener
''' <summary>
''' Stores a list of the current users on a channel room.
''' </summary>
Private RoomUserNames As New List(Of String)
''' <summary>
''' Indicates the invoked command arguments.
''' </summary>
Private CommandParts As String() = {String.Empty}
#End Region
#End Region
#Region " Constructor "
''' <summary>
''' Initializes a new instance of the <see cref="IRCBot"/> class.
''' </summary>
''' <param name="Server">Indicates the IRC server to connect.</param>
''' <param name="Channel">Indicates the IRC channel to join.</param>
''' <param name="Nick">Indicates the nickname to use.</param>
Public Sub New(ByVal Server As String,
ByVal Channel As String,
ByVal Nick As String)
Me.Server = Server
Me.Channel = Channel
Me.Nick = Nick
CreateConnection()
End Sub
#End Region
#Region " Private Methods "
''' <summary>
''' Establishes the first connection to the server.
''' </summary>
Public Sub CreateConnection()
Console.WriteLine(String.Format("[+] Bot started........: '{0}'", DateTime.Now.ToString))
Identd.Start(Me.Nick)
BotConnection = New Connection(New ConnectionArgs(Me.Nick, Me.Server), False, False)
BotListener = BotConnection.Listener
Try
BotConnection.Connect()
Console.WriteLine(String.Format("[+] Connected to server: '{0}'", Me.Server))
Catch e As Exception
Console.WriteLine(String.Format("[X] Error during connection process: {0}", e.ToString))
Identd.Stop()
End Try
End Sub
''' <summary>
''' Kicks everybody from the channel room unless the user who invoked the command.
''' </summary>
''' <param name="UserInvoked">Indicates the user who invoked the command.</param>
''' <param name="CommandMessage">Indicates the command message to retrieve the command arguments.</param>
Private Sub KickEverybody(ByVal UserInvoked As String,
ByVal CommandMessage As String)
' Renew the current nicknames on the channel room.
BotConnection.Sender.AllNames()
' Get the Kick Reason from the CommandMessage.
CommandParts = CommandMessage.Split
Select Case CommandParts.Length
Case Is > 1
CommandParts = CommandParts.Skip(1).ToArray
Case Else
BotConnection.Sender.PublicMessage(Me.Channel, String.Format(
"[X] Can't process the invoked command, 'KickReason' parameter expected."))
BotConnection.Sender.PublicMessage(Me.Channel, String.Format(
"[i] Command Syntax: !KickAll ""Kick Reason"""))
Exit Sub
End Select
' Kick each users one by one.
For Each User As String In (From Nick As String
In RoomUserNames
Where Not Nick = UserInvoked _
AndAlso Not Nick = Me.Nick)
BotConnection.Sender.Kick(Me.Channel, String.Join(" ", CommandParts), User)
Next User
End Sub
''' <summary>
''' Kicks the specified user from the channel.
''' </summary>
''' <param name="CommandMessage">Indicates the command message to retrieve the command arguments.</param>
Private Sub Kick(ByVal CommandMessage As String)
' Renew the current nicknames on the channel room.
BotConnection.Sender.AllNames()
' Get the user to Kick and the Kick Reason.
CommandParts = CommandMessage.Split
Select Case CommandParts.Length
Case Is > 2
CommandParts = CommandParts.Skip(1).ToArray
Case Is < 2
BotConnection.Sender.PublicMessage(Me.Channel, String.Format(
"[X] Can't process the invoked command, 'NickName' parameter expected."))
BotConnection.Sender.PublicMessage(Me.Channel, String.Format(
"[X] Command Syntax: !Kick ""NickName"" ""Kick Reason"""))
Exit Sub
End Select
BotConnection.Sender.Kick(Me.Channel, String.Join(" ", CommandParts.Skip(1)), CommandParts(0))
End Sub
#End Region
#Region " Event Handlers "
''' <summary>
''' Occurs when the Bot joins to a channel.
''' </summary>
Private Sub OnRegistered() Handles BotListener.OnRegistered
Try
Identd.Stop()
BotConnection.Sender.Join(Me.Channel)
Console.WriteLine(String.Format("[+] Channel joined.....: '{0}'", Me.Channel))
Catch e As Exception
Console.WriteLine(String.Format("[X] Error in 'OnRegistered' Event: {0}", e.Message))
End Try
End Sub
''' <summary>
''' Occurs when an unexpected Bot error happens.
''' </summary>
''' <param name="code">Indicates the ReplyCode.</param>
''' <param name="message">Contains the error message information.</param>
Private Sub OnError(ByVal code As ReplyCode,
ByVal message As String) Handles BotListener.OnError
BotConnection.Sender.PublicMessage(Me.Channel, String.Format("[X] Unexpected Error: {0}", message))
Console.WriteLine(String.Format("[X] Unexpected Error: {0}", message))
Debug.
WriteLine(String.
Format("[X] Unexpected Error: {0}", message
))
End Sub
''' <summary>
''' Occurs when a user sends a public message in a channel room.
''' </summary>
''' <param name="user">Indicates the user who sent the public message.</param>
''' <param name="channel">Indicates the channel where the public message was sent.</param>
''' <param name="message">Indicates the content of the public message.</param>
Public Sub OnPublic(ByVal User As UserInfo,
ByVal Channel As String,
ByVal Message As String) Handles BotListener.OnPublic
Select Case True
Case Message.Trim.StartsWith("!KickAll ", StringComparison.OrdinalIgnoreCase)
KickEverybody(User.Nick, Message)
Case message.Trim.StartsWith("!Kick ", StringComparison.OrdinalIgnoreCase)
Kick(Message)
End Select
End Sub
''' <summary>
''' Occurs when the Bot invokes one of the methods to retrieve the nicks of a channel.
''' For example, the 'Sender.AllNames' method.
''' </summary>
''' <param name="Channel">Indicates the channel to list the nicks.</param>
''' <param name="Nicks">Indicates the nicks of the channel.</param>
''' <param name="LastError">Indicates the last command error.</param>
Private Sub OnNames(ByVal Channel As String,
ByVal Nicks() As String,
ByVal LastError As Boolean) Handles BotListener.OnNames
If Channel = Me.Channel AndAlso Not RoomUserNames.Count <> 0 Then
RoomUserNames.Clear()
RoomUserNames.AddRange((From Name As String In Nicks
Select If(Name.StartsWith("@"), Name.Substring(1), Name)).
ToArray)
End If
End Sub
''' <summary>
''' Occurs when the bot invokes the Kick command.
''' </summary>
''' <param name="user">Indicates the user who invoked the Kick command.</param>
''' <param name="channel">Indicates the channel where the user was kicked.</param>
''' <param name="kickee">Indicates the kickee.</param>
''' <param name="reason">Indicates the kick reason.</param>
Private Sub OnKick(ByVal user As UserInfo,
ByVal channel As String,
ByVal kickee As String,
ByVal reason As String) Handles BotListener.OnKick
Console.WriteLine(String.Format("[+]: User kicked: '{0}' From channel: '{1}' With reason: '{2}'.",
user.Nick,
channel,
reason))
End Sub
#End Region
End Class