Skip to content

Commit bb4ead0

Browse files
authored
Merge pull request #118 from TotallyNotRobots/gonzobot+join-keys
Add support for channel keys
2 parents 117c561 + 2cd457e commit bb4ead0

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
- Add Python 3.8 to testing matrix
10+
- Add support for channel keys (#95)
1011
### Changed
1112
- Refactor tests to remove dependency on mock library
1213
- Change link_announcer.py to only warn on connection errors

cloudbot/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ def set_nick(self, nick):
162162
"""
163163
raise NotImplementedError
164164

165-
def join(self, channel):
165+
def join(self, channel, key=None):
166166
"""
167167
Joins a given channel
168168
:type channel: str
169+
:type key: str
169170
"""
170171
raise NotImplementedError
171172

cloudbot/clients/irc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,12 @@ def notice(self, target, text):
238238
def set_nick(self, nick):
239239
self.cmd("NICK", nick)
240240

241-
def join(self, channel):
242-
self.send("JOIN {}".format(channel))
241+
def join(self, channel, key=None):
242+
if key:
243+
self.cmd("JOIN", channel, key)
244+
else:
245+
self.cmd("JOIN", channel)
246+
243247
if channel not in self.channels:
244248
self.channels.append(channel)
245249

plugins/admin_bot.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,26 @@ async def restart(text, bot):
232232

233233
@hook.command(permissions=["botcontrol", "snoonetstaff"])
234234
async def join(text, conn, nick, notice, admin_log):
235-
"""<channel> - joins <channel>
235+
"""<channel> [key] - joins <channel> with the optional [key]
236236
237237
:type text: str
238238
:type conn: cloudbot.client.Client
239+
:type nick: str
239240
"""
240-
for target in text.split():
241-
if not target.startswith("#"):
242-
target = "#{}".format(target)
243-
admin_log("{} used JOIN to make me join {}.".format(nick, target))
244-
notice("Attempting to join {}...".format(target))
245-
conn.join(target)
241+
parts = text.split(None, 1)
242+
target = parts.pop(0)
243+
244+
if parts:
245+
key = parts.pop(0)
246+
else:
247+
key = None
248+
249+
if not target.startswith("#"):
250+
target = "#{}".format(target)
251+
252+
admin_log("{} used JOIN to make me join {}.".format(nick, target))
253+
notice("Attempting to join {}...".format(target))
254+
conn.join(target, key)
246255

247256

248257
def parse_targets(text, chan):

tests/plugin_tests/test_admin_bot.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import asyncio
2+
from unittest.mock import MagicMock
3+
4+
import pytest
5+
6+
from cloudbot.event import CommandEvent
7+
from cloudbot.util import async_util
8+
from plugins import admin_bot
9+
10+
11+
@pytest.mark.asyncio
12+
@pytest.mark.parametrize('input_text,chan,key', [
13+
('#channel key', '#channel', 'key'),
14+
('channel key', '#channel', 'key'),
15+
('#channel', '#channel', None),
16+
('channel', '#channel', None),
17+
])
18+
async def test_join(input_text, chan, key):
19+
conn = MagicMock()
20+
conn.config = {}
21+
conn.bot = None
22+
23+
event = CommandEvent(
24+
text=input_text,
25+
cmd_prefix='.',
26+
triggered_command='join',
27+
hook=MagicMock(),
28+
bot=conn.bot,
29+
conn=conn,
30+
channel='#foo',
31+
nick='foobaruser',
32+
)
33+
34+
await async_util.run_func_with_args(asyncio.get_event_loop(), admin_bot.join, event)
35+
36+
event.conn.join.assert_called_with(chan, key)

0 commit comments

Comments
 (0)