Awesome IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.7 KiB

#!/usr/bin/env python
.. module:: GameBase
:synopsis: A codebase for making IRC games
.. moduleauthor:: Dave Pedu <>
from pyircbot.modulebase import ModuleBase, ModuleHook
import random
import os
import time
import math
from threading import Timer
from collections import namedtuple, defaultdict
class Election(ModuleBase):
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName)
self.hooks = [ModuleHook("PRIVMSG", self.got_msg), ModuleHook("JOIN", self.join_ch)]
self.loadConfig() = {}
def got_msg(self, event):
# Ignore messages from users not logged in
if event.args[0][0] == "#":
# Channel message[event.args[0]].gotMsg(event)
def join_ch(self, event):
if event.prefix.nick ==
joined_ch = event.trailing
if joined_ch not in
if self.config["channelWhitelistOn"] and joined_ch not in self.config["channelWhitelist"]:
return[joined_ch] = ElectionGame(self, joined_ch)
def ondisable(self):"GameBase: Unload requested, ending games...")
for game in[game].gameover()
_game_phases = namedtuple("Phase", "election cooldown halted")
game_phases = _game_phases(0, 1, 2)
class ElectionGame:
def __init__(self, master, channel):
self.master = master = = channel
self.state = game_phases.election
self.election_length_s = self.master.config.get('duration', 30)
self.cooldown_length_s = self.master.config.get('cooldown', 30)
self.ends_at = time.time() + self.election_length_s
self.end_timer = None
self.cooldown_timer = None
def start_election(self):
self.cooldown_timer = None
self.votes = defaultdict(list, dict(**{i: [] for i in self.master.config["choices"]}))
self.state = game_phases.election
self.end_timer = Timer(self.election_length_s, self.end_election)
self.end_timer.start(), "[Election started] - vote with '.vote <item>' choosing from: {}."
" Ends in {}".format(', '.join(self.votes.keys()),
def end_election(self):
self.end_timer = None
self.state = game_phases.cooldown
self.print_results(prefix="[Election finished] ", show_writeins=True)
# Only start a new game if someone voted
if sum([len(i) for i in self.votes.values()]) > 0:
self.cooldown_timer = Timer(self.cooldown_length_s, self.start_election)
self.cooldown_timer.start(), "Next election starts in: {}".format(self.format_seconds(self.cooldown_length_s)))
else:, "Nobody voted! To turn me on again, type .election")
self.state = game_phases.halted
def gotMsg(self, event):
if self.state == game_phases.halted:
cmd =".election", event.trailing)
if cmd:
if self.state == game_phases.election:
cmd =".vote", event.trailing, requireArgs=True)
if cmd:
if self.master.config.get('allow_writeins', True) or cmd.args[0] in self.votes.keys():
# Invalid candidate
cmd =".votes", event.trailing)
if cmd:
self.print_results(show_remaining_time=True, show_writeins=True)
# No election running, pass
def annul_voter(self, voter_nick):
for target in self.votes:
except ValueError:
def print_results(self, prefix='', postfix='', show_remaining_time=False, show_writeins=False):
results = ['{} - {}'.format(candidate, len(voters)) for candidate, voters in self.votes.items() if candidate in self.master.config["choices"]]
writeins = [(candidate, len(voters)) for candidate, voters in self.votes.items()
if candidate not in self.master.config["choices"]]
writeins = sorted(writeins, reverse=True, key=lambda item: item[1])
writein_results = ['{} - {}'.format(candidate, voters) for candidate, voters in writeins if voters > 0]
togo_info = ""
if show_remaining_time:
togo_info = " | ends in {}".format(self.format_seconds(self.ends_at - time.time())), "{}{}{}{}".format(prefix, ', '.join(results), postfix, togo_info))
if show_writeins and writein_results:, "Top write-ins: {}".format(', '.join(writein_results[0:5])))
def gameover(self):
for t in [self.end_timer, self.cooldown_timer]:
if t:
def format_seconds(self, secs):
Turns '90' into '1m30s'
output = ""
minutes = 0
if secs > 60:
minutes = math.floor(secs / 60)
output += "{}m".format(minutes)
secs -= minutes * 60
output += "{}s".format(round(secs))
return output