Skip to content

Instantly share code, notes, and snippets.

@doyousketch2
Last active July 22, 2019 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doyousketch2/d7a00b54f8e5a398898eac2bcb058993 to your computer and use it in GitHub Desktop.
Save doyousketch2/d7a00b54f8e5a398898eac2bcb058993 to your computer and use it in GitHub Desktop.
Simple IRC Chat Bot for HexChat
https://git.io/fjDe4
-----------------------------------------------------------
-- @Doyousketch2 AGPL-3 Jun 29, 2019
-- https://www.gnu.org/licenses/agpl-3.0.en.html
-----------------------------------------------------------
-- header
local name = 'auto_bot.lua'
local version = '1.7'
local description = 'Simple IRC Chat Bot'
local author = '@Doyousketch2'
hexchat .register( name, version, description )
hexchat .print( name ..' ver. ' ..version ..' loaded' )
hexchat .print( ' ' ..description )
hexchat .print( ' by: ' ..author )
-----------------------------------------------------------
--[[ notes:
https://hexchat.readthedocs.io/en/latest/script_lua.html
For example on aChannel Messageevent:
( same as Python, but begin count at 1 )
[23:29:26] <@Nick> hello everyone
word[1] is Nick
word[2] is hello everyone
word[3] is @
]]---------------------------------------------------------
-- variable declaration
local print_debug_msgs = false -- set to true if you want to see excess jibberish.
--local time = os.date( '*t' ) -- produce a date table
-- { year, yday, month, wday, day, hour, min, sec, isdst }
-- last entry is a boolean, true if daylight saving
local chmsg = 'Channel Message' -- from others
local yrmsg = 'Your Message' -- from self
local prmsg = 'Private Message to Dialog' -- /msg's from others
local hilight = 'Channel Msg Hilight' -- from others, specifically directed to your nick
local banned_users = { 'ventfan' }
local timestamp = os.clock()
-----------------------------------------------------------
-- define functions
local function automate( word )
local NICK = word[1]
local MSG = word[2]
local CMD = ''
local ARGS = ''
-- check if first char is a dot
if MSG :sub( 1, 2 ) == '..' then
local action_string = ''
safe_user = true
for key, value in pairs( banned_users ) do
if NICK :lower() == value then safe_user = false end
end
-- 5 second delay. os.clock() counts in centiseconds aka 100ths
if os.clock() - timestamp < 0.05 then safe_user = false end
if safe_user then
if MSG :find( ' ' ) and not recursion then -- find CMD, and ARGS if there's a space
CMD = MSG :sub( 3, MSG :find(' ') -1 )
ARGS = MSG :sub( MSG :find(' ') +1, -1 )
else
CMD = MSG :sub( 3 )
end -- CMD
---------------------------------------------
if print_debug_msgs then
hexchat .print( 'NICK||' ..NICK ..'||' )
hexchat .print( 'MSG||' ..MSG ..'||' )
if CMD ~= '' then
hexchat .print( 'CMD||' ..CMD ..'||' )
if ARGS ~= '' then
hexchat .print( 'ARGS||' ..ARGS ..'||' )
end -- ARGS
end -- CMD
end -- print_debug_msgs
---------------------------------------------
if NICK == hexchat .get_info( 'nick' ) then
hexchat .command( 'say ' ..MSG ) -- add a space in front of MSG to combat recursion.
-- this way it says the message before hexchat .EAT_HEXCHAT
-- but doesn't go into a recursive loop because '.' isn't the first char.
end
---------------------------------------------
-- ping ...pong
if CMD == 'ping' then
action_string = 'ACTION pong'
end -- CMD
---------------------------------------------
-- print time
if CMD == 'time' then
--time_string = ('%02d:%02d :%02d') :format( time.hour, time.min, time.sec )
time_string = os.date( 'The current time is %a %d %b, %X' )
action_string = 'ACTION ' ..time_string
end -- time
---------------------------------------------
-- print a random number from 1 - 100, or use optional max_value
if CMD == 'random' then
math.randomseed( os.time() ) -- number of seconds since epoch
math.random() math.random() math.random() -- shake the dice
local max_value = 100
if ARGS ~= '' then
max_value = ARGS
end -- ARGS
local random_number = math.random( max_value )
action_string = 'ACTION ' ..random_number ..' = Random number from 1 to '..max_value
end -- CMD
---------------------------------------------
-- print a random number from 1 - 100, or use optional max_value
if CMD == 'roll' then
math.randomseed( os.time() ) -- number of seconds since epoch
math.random() math.random() math.random() -- shake the dice
local random_number = math.random( 6 )
action_string = 'ACTION ' ..NICK ..' rolled a '..random_number ..' !!'
end -- CMD
---------------------------------------------
-- heads or tails
if CMD == 'coin' or CMD == 'flip' then
math.randomseed( os.time() ) -- number of seconds since epoch
math.random() math.random() math.random() -- shake the dice
local side = 'Heads!'
if math.random( 2 ) == 2 then side = 'Tails!' end
action_string = 'ACTION Coin Flip: ' ..side
end -- CMD
---------------------------------------------
if CMD == 'help' then
help_string = ' ..ping ..time ..random ..random # ..coin ..flip ..roll'
action_string = 'ACTION ' ..help_string
end -- CMD
---------------------------------------------
-- do action
if print_debug_msgs then
hexchat .print( '/' ..action_string )
end -- print_debug_msgs
hexchat .command( action_string )
timestamp = os.clock()
if NICK == hexchat .get_info( 'nick' ) then
return hexchat .EAT_HEXCHAT -- don't print .CMD to self
end
end -- safe_user
end -- find ..
end -- funct
-----------------------------------------------------------
-- main loop
hexchat .hook_print( chmsg, automate )
hexchat .hook_print( prmsg, automate )
hexchat .hook_print( yrmsg, automate )
-----------------------------------------------------------
-- eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment