nxy/bot/plugins/ctcp.py
2017-08-22 14:56:29 +02:00

75 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
import time
import irc3
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
@irc3.plugin
class CTCP(Plugin):
requires = ['irc3.plugins.async',
'irc3.plugins.command']
TIMEOUT = 5
# noinspection PyMethodMayBeStatic
def _ctcp(self, name: str, nick: str, reply: str):
return '\x02[{}]\x02 {}: {}'.format(name.upper(), nick, reply)
async def ctcp(self, name: str, mask: IrcString, args: DocOptDict):
nick = args.get('<nick>', mask.nick)
data = await self.bot.ctcp_async(nick, name.upper(), self.TIMEOUT)
if not data or data['timeout']:
reply = 'timeout'
elif not data['success']:
reply = 'Error: {}'.format(data['reply'])
else:
reply = data['reply']
return self._ctcp(name, nick, reply)
@command
async def ping(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Sends ping via CTCP to user and sends the time needed
%%ping [<nick>]
"""
nick = args.get('<nick>', mask.nick)
data = await self.bot.ctcp_async(nick, 'PING {}'.format(time.time()))
if not data or data['timeout']:
reply = 'timeout'
elif not data['success']:
reply = 'Error: {}'.format(data['reply'])
else:
delta = time.time() - float(data['reply'])
if delta < 1.0:
delta *= 1000
unit = 'ms'
else:
unit = 's'
reply = '{0:.3f} {1}'.format(delta, unit)
return self._ctcp('ping', nick, reply)
@command
async def time(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Gets the client time from nick via CTCP
%%time [<nick>]
"""
return await self.ctcp('time', mask, args)
@command
async def ver(self, mask: IrcString, target: IrcString, args: DocOptDict):
"""Gets the client version from nick via CTCP
%%ver [<nick>]
"""
return await self.ctcp('version', mask, args)