Skip to content

Commit be6074c

Browse files
committed
Add autojoin plugin
1 parent 5d74e48 commit be6074c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

plugins/autojoin.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from sqlalchemy import PrimaryKeyConstraint, Column, String, Table, and_
2+
from sqlalchemy.exc import IntegrityError
3+
4+
from cloudbot import hook
5+
from cloudbot.util import database
6+
7+
table = Table(
8+
'autojoin',
9+
database.metadata,
10+
Column('conn', String),
11+
Column('chan', String),
12+
PrimaryKeyConstraint('conn', 'chan')
13+
)
14+
15+
16+
@hook.irc_raw('004')
17+
def do_joins(db, conn):
18+
chans = db.execute(table.select().where(table.c.conn == conn.name.casefold())).fetchall()
19+
for chan in chans:
20+
conn.join(chan[1])
21+
22+
23+
@hook.irc_raw('JOIN', singlethread=True)
24+
def add_chan(db, conn, chan, nick):
25+
if nick.casefold() == conn.nick.casefold():
26+
try:
27+
db.execute(table.insert().values(conn=conn.name.casefold(), chan=chan.casefold()))
28+
db.commit()
29+
except IntegrityError:
30+
pass
31+
32+
33+
@hook.irc_raw('PART', singlethread=True)
34+
def on_part(db, conn, chan, nick):
35+
if nick.casefold() == conn.nick.casefold():
36+
db.execute(table.delete().where(and_(table.c.conn == conn.name.casefold(), table.c.chan == chan.casefold())))
37+
db.commit()
38+
39+
40+
@hook.irc_raw('KICK', singlethread=True)
41+
def on_kick(db, conn, chan, target):
42+
if target.casefold() == conn.nick.casefold():
43+
db.execute(table.delete().where(and_(table.c.conn == conn.name.casefold(), table.c.chan == chan.casefold())))
44+
db.commit()

0 commit comments

Comments
 (0)