Skip to content

Instantly share code, notes, and snippets.

@TRSGuy
Created May 22, 2016 13:58
Show Gist options
  • Save TRSGuy/14e777a86b7f420821100be08426685f to your computer and use it in GitHub Desktop.
Save TRSGuy/14e777a86b7f420821100be08426685f to your computer and use it in GitHub Desktop.
#Imports
import socket
import sys
import sched, time
import re
#Globals
server = "chat.freenode.net" #The irc server that you want the bot to connect to.
channel = "##kanal56" #The channel you want the bot to join on start
botnick = "vorabot" #The bot's IRC Nick
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Defines the socket
silenceTime = 0
#Arrays
#Command Arrays
silenceCommandArray = [':!silence',':!scilence',':!silense',':!silens']
helloCommandArray = [':!hi',':!hello']
helpCommandArray = [':!help',':!list',':!commands',':!command']
awayCommandArray = [':!go',':!away','!left']
backCommandArray = [':!back']
toggleAwayCommandArray
#Information Arrays
awayArray = ['','','','','','','','','','','','','','','']
#Server connect. This part connects the bot to the IRC server, Joins a channel and set's the nick.
print "Connecting to:"+server
irc.connect((server, 6667)) #Connects to the server
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a chat bot!\n") #authenticates the bot with the server
irc.send("NICK "+ botnick +"\n") #Sets the bots nick
irc.send("JOIN "+ channel +"\n") #Joins the channel
#functions
def getMsg(line):
message = line.split()[-1]
return message;
def sendMsg(message):
irc.send('PRIVMSG '+channel+' :'+message+'\r\n')
return;
def getNick(message): #This function gets the nick.
pos = message.find("!") #Finds the position of the end of the nick
print pos
nick = message[1:pos] #Selects everything between the first letter of the nick all the way to the position of the last nick charachter
print nick
return nick;
def getArgument(msg): #This function gets the word said after a command
m = message.split()[1]
return m;
def isAnInt(time): #This checks if time is an int
try:
int(time)
return True
except ValueError:
return False
while 1: #Getting the text. This is where the bot gets all the text outputted to the IRC channel and network
text = irc.recv(2040) #Recieves the IRC text
print text #Prints the IRC text
message = getMsg(text)
if text.find('PING') !=-1: #This checks for a PING from the server. IRC Servers does this to assure that the client still is connected
irc.send('PONG ' + text.split() [1] + '\r\n') #This responds PONG to the server to tell it that we are indeed still connected
time.sleep(0.1)
#Custom commands
#Greeting command. This command greets a newly joined
if message in helloCommandArray or ' JOIN ' in text: #Greets the sender
sendMsg('Hello '+str(getNick(text))+'! Welcome to the channel!') #Sends hello back
#This command shows a list of all the available commands. Note: Make so that it sends the commandArray instead.
if message in helpCommandArray:
sendMsg('Command Listing. !help, !list - Show this screen. !hi, !hello - Greetings from the bot! Command executed by '+str(getNick(text))+'!')
#This command silences the bot for set amount of minutes
if message in silenceCommandArray:
sendMsg('Shutting up for'+str(silenceTime)+'minutes')
time.sleep(silenceTime*60)
#This command toggles your away state. It adds the nick who executed it to the awayArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment