move join/part/quit/autojoin to separate functions. part 1 of making things generic
This commit is contained in:
parent
bad1a82dc4
commit
317d0d575d
121
dr.botzo.py
121
dr.botzo.py
@ -6,6 +6,86 @@ import sys
|
|||||||
|
|
||||||
import irclib
|
import irclib
|
||||||
|
|
||||||
|
#####
|
||||||
|
# sub_join_channel
|
||||||
|
# join a channel when told to by an admin
|
||||||
|
# interface assumptions: privmsg
|
||||||
|
#####
|
||||||
|
|
||||||
|
def sub_join_channel(connection, event, admin_unlocked):
|
||||||
|
nick = event.source().split('!')[0]
|
||||||
|
userhost = event.source().split('!')[1]
|
||||||
|
what = event.arguments()[0]
|
||||||
|
|
||||||
|
if what.split(' ')[0] == 'join' and admin_unlocked:
|
||||||
|
channel = what.split(' ')[1]
|
||||||
|
connection.join(channel)
|
||||||
|
connection.privmsg(nick, 'joined ' + channel + '.')
|
||||||
|
|
||||||
|
#####
|
||||||
|
# sub_part_channel
|
||||||
|
# leave a channel when told to by an admin. optionally provide a message
|
||||||
|
# interface assumptions: privmsg
|
||||||
|
#####
|
||||||
|
|
||||||
|
def sub_part_channel(connection, event, admin_unlocked):
|
||||||
|
nick = event.source().split('!')[0]
|
||||||
|
userhost = event.source().split('!')[1]
|
||||||
|
what = event.arguments()[0]
|
||||||
|
|
||||||
|
if what.split(' ')[0] == 'part' and admin_unlocked:
|
||||||
|
channel = what.split(' ')[1]
|
||||||
|
connection.part(channel, ' '.join(what.split(' ')[2:]))
|
||||||
|
connection.privmsg(nick, 'parted ' + channel + '.')
|
||||||
|
|
||||||
|
#####
|
||||||
|
# sub_quit_channel
|
||||||
|
# quit irc server. optionally provide a message
|
||||||
|
# interface assumptions: privmsg
|
||||||
|
#####
|
||||||
|
|
||||||
|
def sub_quit_channel(connection, event, admin_unlocked):
|
||||||
|
nick = event.source().split('!')[0]
|
||||||
|
userhost = event.source().split('!')[1]
|
||||||
|
what = event.arguments()[0]
|
||||||
|
|
||||||
|
if what.split(' ')[0] == 'quit' and admin_unlocked:
|
||||||
|
connection.privmsg(nick, 'quitting')
|
||||||
|
connection.quit(' '.join(what.split(' ')[1:]))
|
||||||
|
with open('dr.botzo.cfg', 'w') as cfg:
|
||||||
|
config.write(cfg)
|
||||||
|
|
||||||
|
#####
|
||||||
|
# sub_handle_autojoin
|
||||||
|
# add/remove on channel autojoin list.
|
||||||
|
# interface assumptions: privmsg
|
||||||
|
#####
|
||||||
|
|
||||||
|
def sub_autojoin_manipulate(connection, event, admin_unlocked):
|
||||||
|
nick = event.source().split('!')[0]
|
||||||
|
userhost = event.source().split('!')[1]
|
||||||
|
what = event.arguments()[0]
|
||||||
|
|
||||||
|
if what.split(' ')[0] == 'autojoin' and admin_unlocked:
|
||||||
|
if what.split(' ')[1] == 'add':
|
||||||
|
try:
|
||||||
|
# get existing list
|
||||||
|
channel = what.split(' ')[2]
|
||||||
|
channelset = set(config.get('channels', 'autojoin').split(','))
|
||||||
|
channelset.add(channel)
|
||||||
|
config.set('channels', 'autojoin', ','.join(channelset))
|
||||||
|
connection.privmsg(nick, 'added ' + channel + ' to autojoin')
|
||||||
|
except NoOptionError: pass
|
||||||
|
elif what.split(' ')[1] == 'remove':
|
||||||
|
try:
|
||||||
|
# get existing list
|
||||||
|
channel = what.split(' ')[2]
|
||||||
|
channelset = set(config.get('channels', 'autojoin').split(','))
|
||||||
|
channelset.discard(channel)
|
||||||
|
config.set('channels', 'autojoin', ','.join(channelset))
|
||||||
|
connection.privmsg(nick, 'removed ' + channel + ' from autojoin')
|
||||||
|
except NoOptionError: pass
|
||||||
|
|
||||||
#####
|
#####
|
||||||
# on_connect
|
# on_connect
|
||||||
# handler for when the bot has connected to IRC
|
# handler for when the bot has connected to IRC
|
||||||
@ -43,43 +123,10 @@ def on_privmsg(connection, event):
|
|||||||
except NoOptionError: pass
|
except NoOptionError: pass
|
||||||
|
|
||||||
# admin commands
|
# admin commands
|
||||||
|
sub_join_channel(connection, event, admin_unlocked)
|
||||||
# join channel
|
sub_part_channel(connection, event, admin_unlocked)
|
||||||
if what.split(' ')[0] == 'join' and admin_unlocked:
|
sub_quit_channel(connection, event, admin_unlocked)
|
||||||
channel = what.split(' ')[1]
|
sub_autojoin_manipulate(connection, event, admin_unlocked)
|
||||||
connection.join(channel)
|
|
||||||
connection.privmsg(nick, 'joined ' + channel + '.')
|
|
||||||
# part channel, with message
|
|
||||||
elif what.split(' ')[0] == 'part' and admin_unlocked:
|
|
||||||
channel = what.split(' ')[1]
|
|
||||||
connection.part(channel, ' '.join(what.split(' ')[2:]))
|
|
||||||
connection.privmsg(nick, 'parted ' + channel + '.')
|
|
||||||
# quit server, with message
|
|
||||||
elif what.split(' ')[0] == 'quit' and admin_unlocked:
|
|
||||||
connection.privmsg(nick, 'quitting')
|
|
||||||
connection.quit(' '.join(what.split(' ')[1:]))
|
|
||||||
with open('dr.botzo.cfg', 'w') as cfg:
|
|
||||||
config.write(cfg)
|
|
||||||
# add/remove channel to/from autojoin list
|
|
||||||
elif what.split(' ')[0] == 'autojoin' and admin_unlocked:
|
|
||||||
if what.split(' ')[1] == 'add':
|
|
||||||
try:
|
|
||||||
# get existing list
|
|
||||||
channel = what.split(' ')[2]
|
|
||||||
channelset = set(config.get('channels', 'autojoin').split(','))
|
|
||||||
channelset.add(channel)
|
|
||||||
config.set('channels', 'autojoin', ','.join(channelset))
|
|
||||||
connection.privmsg(nick, 'added ' + channel + ' to autojoin')
|
|
||||||
except NoOptionError: pass
|
|
||||||
elif what.split(' ')[1] == 'remove':
|
|
||||||
try:
|
|
||||||
# get existing list
|
|
||||||
channel = what.split(' ')[2]
|
|
||||||
channelset = set(config.get('channels', 'autojoin').split(','))
|
|
||||||
channelset.discard(channel)
|
|
||||||
config.set('channels', 'autojoin', ','.join(channelset))
|
|
||||||
connection.privmsg(nick, 'removed ' + channel + ' from autojoin')
|
|
||||||
except NoOptionError: pass
|
|
||||||
|
|
||||||
#####
|
#####
|
||||||
# init
|
# init
|
||||||
|
Loading…
Reference in New Issue
Block a user