stdinbot/example.py

59 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/python3
#
# Example miniirc-based bot
#
# © 2018 by luk3yx
#
2019-06-19 23:21:25 -07:00
import miniirc, sys
assert miniirc.ver >= (1,4,0), 'This bot requires miniirc >= v1.4.0.'
# Variables
nick = 'miniirc-test' + str(hash('.'))[1:4] # Make a unique(-ish) nickname
ident = nick
realname = 'Example miniirc bot - https://gitlab.com/luk3yx/stdinbot'
identity = None
# identity = '<username> <password>'
debug = False
channels = ['#lurk']
prefix = '`'
ip = 'xeroxirc.net'
port = 6697
# Welcome!
print('Welcome to {}!'.format(nick), file=sys.stderr)
2019-06-19 23:21:25 -07:00
irc = IRC(ip, port, nick, channels, ident=ident, realname=realname,
ns_identity=identity, debug=debug, auto_connect=False)
# Handle normal messages
# This could probably be better than a large if/else statement.
2019-06-19 23:21:25 -07:00
@irc.Handler('PRIVMSG', colon=False)
def handle_privmsg(irc, hostmask, args):
channel = args[0]
2019-06-19 23:21:25 -07:00
text = args[-1].split(' ')
2018-10-28 11:14:11 -07:00
cmd = text[0].lower()
# Unprefixed commands here
2019-06-19 23:21:25 -07:00
if cmd.startswith('meep'):
irc.msg(channel, '\u200bMeep!')
elif cmd.startswith(prefix):
# Prefixed commands
2019-06-19 23:21:25 -07:00
cmd = cmd[len(prefix):]
if cmd == 'yay':
2018-10-28 11:14:11 -07:00
irc.msg(channel, '\u200bYay!')
elif cmd == 'rev':
2018-10-28 11:14:11 -07:00
if len(text) > 1:
irc.msg(channel, "{}: {}".format(hostmask[0],
' '.join(text[1:])[::-1]))
else:
2018-10-28 11:14:11 -07:00
irc.msg(channel, 'Invalid syntax! Syntax: ' + prefix +
'rev <string>')
elif cmd == 'about':
2018-10-28 11:14:11 -07:00
irc.msg(channel,
'I am {}, an example miniirc bot.'.format(irc.nick))
# Connect
2019-06-19 23:21:25 -07:00
if __name__ == '__main__':
irc.connect()