Make an example bot

Move the IRC commands into a separate bot.
master
luk3yx 2018-09-14 15:47:16 +12:00
parent 4b591f0a68
commit 45b28fb98f
2 changed files with 58 additions and 27 deletions

56
example.py Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/python3
#
# Example miniirc-based bot
#
# © 2018 by luk3yx
#
import os, socket, sys, atexit
from miniirc import IRC
# 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)
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.
@irc.Handler('PRIVMSG')
def handle_privmsg(irc, hostmask, args):
channel = args[0]
args = args[-1][1:].split(' ')
cmd = args[0].lower()
# Unprefixed commands here
if cmd.startswith("meep"):
irc.msg(channel, "Meep™!")
elif cmd.startswith(prefix):
# Prefixed commands
cmd = cmd[1:]
if cmd == 'yay':
irc.msg(channel, "\u200bYay!")
elif cmd == 'rev':
if len(args) > 1:
irc.msg(channel, "{}: {}".format(hostmask[0], ' '.join(args[1:])[::-1]))
else:
irc.msg(channel, "Invalid syntax! Syntax: <rev <string>")
elif cmd == 'about':
irc.msg(channel, 'I am {}, an example miniirc bot.'.format(irc.nick))
elif print_cmds and cmd != '':
print(' '.join(args)[1:])
# Connect
irc.connect()

View File

@ -18,8 +18,7 @@ identity = False
print_cmds = False
channels = ['#lurk']
connected = False
debug = False
ip = 'xeroxirc.net'
port = 6697
@ -27,31 +26,7 @@ port = 6697
# Welcome!
print("Welcome to stdinbot!", file=sys.stderr)
irc = IRC(ip, port, nick, channels, ident = ident, realname = realname,
ns_identity = identity, debug = True, auto_connect = False)
# Handle normal messages
@irc.Handler('PRIVMSG')
def handle_privmsg(irc, hostmask, args):
channel = args[0]
args = args[-1][1:].split(' ')
cmd = args[0].lower()
# Unprefixed commands here
if cmd.startswith("meep"):
irc.msg(channel, "Meep™!")
elif cmd.startswith('<'):
# Prefixed commands
cmd = cmd[1:]
if cmd == 'yay':
irc.msg(channel, "\u200bYay!")
elif cmd == 'rev':
if len(args) > 1:
irc.msg(channel, "{}: {}".format(hostmask[0], ' '.join(args[1:])[::-1]))
else:
irc.msg(channel, "Invalid syntax! Syntax: <rev <string>")
elif cmd == 'about':
irc.msg(channel, 'I am stdinbot, a simple IRC bot that reads text from stdin and prints it to a channel.')
elif print_cmds and cmd != '':
print(' '.join(args)[1:])
ns_identity = identity, debug = debug, auto_connect = False)
# Read stdin
@irc.Handler('001')