Adding usermode and channelmode capabilities with extra !mode, !op and !deop commands.

This commit is contained in:
Elijah Lazkani 2018-03-17 01:10:06 -04:00
parent bf76c3327c
commit a3cd1fceb0
3 changed files with 100 additions and 7 deletions

View file

@ -50,7 +50,7 @@ class AdminCmd:
split_msg = message.split(" ")
message = " ".join(split_msg[1:])
self.client.loop.create_task(
func(target, message, **kwargs))
func(nick, target, message, **kwargs))
def on_command(self,
command: str,

View file

@ -1,3 +1,4 @@
import re
import logging
import asyncio
import robot
@ -43,35 +44,101 @@ async def plugins(bot: robot.Bot):
admin_cmd = admin_commands.AdminCmd(administrator)
@admin_cmd.on_command("join")
async def join(target, message, **kwargs):
async def join(nick, target, message, **kwargs):
await bot.join(channel=message)
@admin_cmd.on_command("part")
async def part(target, message, **kwargs):
async def part(nick, target, message, **kwargs):
_target = message.split(' ')[0]
_message = " ".join(message.split(' ')[1:])
await bot.part(channel=_target, message=_message)
@admin_cmd.on_command("msg")
async def msg(target, message, **kwargs):
async def msg(nick, target, message, **kwargs):
_target = message.split(' ')[0]
_message = " ".join(message.split(' ')[1:])
await bot.msg(target=_target, message=_message)
@admin_cmd.on_command("action")
async def action(target, message, **kwargs):
async def action(nick, target, message, **kwargs):
_target = message.split(' ')[0]
_message = " ".join(message.split(' ')[1:])
await bot.action(target=_target, message=_message)
@admin_cmd.on_command("notice")
async def msg(target, message, **kwargs):
async def msg(nick, arget, message, **kwargs):
_target = message.split(' ')[0]
_message = " ".join(message.split(' ')[1:])
await bot.notice(target=_target, message=_message)
@admin_cmd.on_command("mode")
async def chanmode(nick, target, message, **kwargs):
chanmode_pattern = r'^(?P<channel>(\#|\#\#|\&)[a-zA-Z0-9]+)?' \
r'((^|\s+)(?P<modes>[\+\-][a-zA-Z]+))' \
r'(\s+(?P<params>.+)?)?'
compiled = re.compile(chanmode_pattern)
m = compiled.match(message)
if m.group("channel"):
_channel = m.group("channel")
else:
_channel = target
if m.group("modes"):
_modes = m.group("modes")
else:
await bot.msg(target=target, message="{}, you did not provide a mode".format(nick))
return
if m.group("params"):
_params = m.group("params")
else:
_params = None
await bot.channelmode(channel=_channel, modes=_modes, params=_params)
@admin_cmd.on_command("op")
async def op(nick, target, message, **kwargs):
_split_msg = message.split(' ')
nick_list = None
_channel = target
if _split_msg[0]:
if _split_msg[0].startswith('#') or _split_msg[0].startswith('&'):
_channel = _split_msg[0]
if _split_msg.__len__() > 1:
nick_list = _split_msg[1:]
else:
nick_list = _split_msg
if nick_list:
_modes = "+" + "o" * nick_list.__len__()
await bot.channelmode(channel=_channel, modes=_modes, params=" ".join(nick_list))
else:
await bot.channelmode(channel=_channel, modes="+o", params=nick)
@admin_cmd.on_command("deop")
async def op(nick, target, message, **kwargs):
_split_msg = message.split(' ')
nick_list = None
_channel = target
if _split_msg[0]:
if _split_msg[0].startswith('#') or _split_msg[0].startswith('&'):
_channel = _split_msg[0]
if _split_msg.__len__() > 1:
nick_list = _split_msg[1:]
else:
nick_list = _split_msg
if nick_list:
_modes = "-" + "o" * nick_list.__len__()
await bot.channelmode(channel=_channel, modes=_modes, params=" ".join(nick_list))
else:
await bot.channelmode(channel=_channel, modes="-o", params=nick)
@admin_cmd.on_command("quit")
async def quit(target, message, **kwargs):
async def quit(nick, target, message, **kwargs):
await bot.quit(message=message)
await bot.disconnect()
# Exit the event loop cleanly

View file

@ -260,6 +260,32 @@ class Bot(bottom.Client):
"""
await self.action_buffer("JOIN", channel=channel, **kwargs)
async def usermode(self, nick: str, modes: str, **kwargs):
"""
This method will send a usermode command to the server
:param nick str: the nick to apply usermode action to
:param modes str: the mode to apply to the nick
:param kwargs: any other parameters that needs to be sent
:return None
"""
self.send("USERMODE", nick=nick, modes=modes)
async def channelmode(self, channel: str, modes: str, params: str = None ,**kwargs):
"""
This method will send a channelmode command to the server
:param channel str: the nick to apply usermode action to
:param modes str: the mode to apply to the channel
:param params str: the parameters to apply to the modes
:param kwargs: any other parameters that needs to be sent
:return None
"""
if params:
self.send("CHANNELMODE", channel=channel, modes=modes, params=params)
else:
self.send("CHANNELMODE", channel=channel, modes=modes)
async def part(self, channel: str, message: str = None, **kwargs) -> None:
"""
This method will send a part command to the action buffer