Add Bot
This commit is contained in:
parent
82a292b5d8
commit
27b3f4b29e
3 changed files with 134 additions and 10 deletions
39
test.py
39
test.py
|
|
@ -6,10 +6,10 @@ import logging
|
|||
|
||||
import yaboli
|
||||
|
||||
#FORMAT = "{asctime} [{levelname:<7}] <{name}> {funcName}(): {message}"
|
||||
#LEVEL = logging.DEBUG
|
||||
FORMAT = "{asctime} [{levelname:<7}] <{name}>: {message}"
|
||||
LEVEL = logging.INFO
|
||||
FORMAT = "{asctime} [{levelname:<7}] <{name}> {funcName}(): {message}"
|
||||
LEVEL = logging.DEBUG
|
||||
#FORMAT = "{asctime} [{levelname:<7}] <{name}>: {message}"
|
||||
#LEVEL = logging.INFO
|
||||
|
||||
DATE_FORMAT = "%F %T"
|
||||
handler = logging.StreamHandler()
|
||||
|
|
@ -23,20 +23,39 @@ logger = logging.getLogger('yaboli')
|
|||
logger.setLevel(LEVEL)
|
||||
logger.addHandler(handler)
|
||||
|
||||
class TestClient(yaboli.Client):
|
||||
class TestBot(yaboli.Bot):
|
||||
DEFAULT_NICK = "testbot"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.register_botrulez()
|
||||
self.register_general("test", self.cmd_test, args=False)
|
||||
self.register_general("who", self.cmd_who, args=False)
|
||||
self.register_general("err", self.cmd_err, args=False)
|
||||
|
||||
async def started(self):
|
||||
await self.join("test")
|
||||
|
||||
async def on_send(self, room, message):
|
||||
if message.content == "!test":
|
||||
await message.reply(f"You said {message.content!r}.")
|
||||
msg1 = await room.send(f"{message.sender.atmention} said something.")
|
||||
await msg1.reply("Yes, they really did.")
|
||||
await self.process_commands(room, message,
|
||||
aliases=["testalias", "aliastest"])
|
||||
|
||||
async def cmd_test(self, room, message, args):
|
||||
await message.reply(f"You said {message.content!r}.")
|
||||
msg1 = await room.send(f"{message.sender.atmention} said something.")
|
||||
await msg1.reply("Yes, they really did.")
|
||||
|
||||
async def cmd_who(self, room, message, args):
|
||||
lines = []
|
||||
for user in await room.who():
|
||||
lines.append(repr(user.nick))
|
||||
await message.reply("\n".join(lines))
|
||||
|
||||
async def cmd_err(self, room, message, args):
|
||||
await message.reply(str(1/0))
|
||||
|
||||
async def main():
|
||||
tc = TestClient()
|
||||
tc = TestBot()
|
||||
await tc.run()
|
||||
|
||||
asyncio.run(main())
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from typing import List
|
||||
|
||||
from .bot import *
|
||||
from .client import *
|
||||
from .command import *
|
||||
from .connection import *
|
||||
|
|
@ -11,6 +12,7 @@ from .session import *
|
|||
from .util import *
|
||||
|
||||
__all__: List[str] = []
|
||||
__all__ += bot.__all__
|
||||
__all__ += client.__all__
|
||||
__all__ += command.__all__
|
||||
__all__ += connection.__all__
|
||||
|
|
|
|||
103
yaboli/bot.py
Normal file
103
yaboli/bot.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
from typing import List, Optional
|
||||
import logging
|
||||
|
||||
from .client import Client
|
||||
from .command import *
|
||||
from .message import LiveMessage
|
||||
from .room import Room
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
__all__ = ["Bot"]
|
||||
|
||||
class Bot(Client):
|
||||
PING_REPLY: str = "Pong!"
|
||||
HELP_GENERAL: Optional[str] = None
|
||||
HELP_SPECIFIC: Optional[str] = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
self._commands: List[Command] = []
|
||||
|
||||
# Registering commands
|
||||
|
||||
def register(self, command: Command) -> None:
|
||||
self._commands.append(command)
|
||||
|
||||
def register_general(self,
|
||||
name: str,
|
||||
cmdfunc: GeneralCommandFunction,
|
||||
args: bool = True
|
||||
) -> None:
|
||||
command = GeneralCommand(name, cmdfunc, args)
|
||||
self.register(command)
|
||||
|
||||
def register_specific(self,
|
||||
name: str,
|
||||
cmdfunc: SpecificCommandFunction,
|
||||
args: bool = True
|
||||
) -> None:
|
||||
command = SpecificCommand(name, cmdfunc, args)
|
||||
self.register(command)
|
||||
|
||||
# Processing commands
|
||||
|
||||
async def process_commands(self,
|
||||
room: Room,
|
||||
message: LiveMessage,
|
||||
aliases: List[str] = []
|
||||
) -> None:
|
||||
nicks = [room.session.nick] + aliases
|
||||
print()
|
||||
print(nicks)
|
||||
print()
|
||||
data = CommandData.from_string(message.content)
|
||||
|
||||
if data is not None:
|
||||
logger.debug(f"Processing command from {message.content!r}")
|
||||
for command in self._commands:
|
||||
await command.run(room, message, nicks, data)
|
||||
|
||||
async def on_send(self, room: Room, message: LiveMessage) -> None:
|
||||
await self.process_commands(room, message)
|
||||
|
||||
# Botrulez
|
||||
|
||||
def register_botrulez(self,
|
||||
ping: bool = True,
|
||||
help_: bool = True
|
||||
) -> None:
|
||||
if ping:
|
||||
self.register_general("ping", self.cmd_ping, args=False)
|
||||
self.register_specific("ping", self.cmd_ping, args=False)
|
||||
|
||||
if help_:
|
||||
if self.HELP_GENERAL is None and self.HELP_SPECIFIC is None:
|
||||
logger.warn(("HELP_GENERAL and HELP_SPECIFIC are None, but the"
|
||||
" help command is enabled"))
|
||||
self.register_general("help", self.cmd_help_general, args=False)
|
||||
self.register_specific("help", self.cmd_help_specific, args=False)
|
||||
|
||||
async def cmd_ping(self,
|
||||
room: Room,
|
||||
message: LiveMessage,
|
||||
args: ArgumentData
|
||||
) -> None:
|
||||
await message.reply(self.PING_REPLY)
|
||||
|
||||
async def cmd_help_general(self,
|
||||
room: Room,
|
||||
message: LiveMessage,
|
||||
args: ArgumentData
|
||||
) -> None:
|
||||
if self.HELP_GENERAL is not None:
|
||||
await message.reply(self.HELP_GENERAL)
|
||||
|
||||
async def cmd_help_specific(self,
|
||||
room: Room,
|
||||
message: LiveMessage,
|
||||
args: SpecificArgumentData
|
||||
) -> None:
|
||||
if self.HELP_SPECIFIC is not None:
|
||||
await message.reply(self.HELP_SPECIFIC)
|
||||
Loading…
Add table
Add a link
Reference in a new issue