예제 #1
0
async def process_commands(ws: web.WebSocketResponse, content: str) -> bool:
    """Returns if the process should carry on and send the message.
    If this returns `True`, the message was not a command and should
    be sent. If this returns `None` or `False`, the message should
    not be sent"""
    # FIXME: This needs to be re-styled because it currently looks like shit
    if not content.startswith(app.args["commandprefix"]):
        return True
    cmd = content.split()[0][1:].lower()
    args = content.split()[1:]

    if cmd in ["active", "online"]:
        # p = ("Other users in chat: "+ ", ".join([w.name for w in app.websockets if w != ws])) if len(app.websockets) > 1 else "No one else is currently in chat"
        p = ((
                "Other users in chat: "
                + ", ".join([
                    (f'{w.name} as "{w.nickname}"' if w.nickname else w.name)
                    for w in app.websockets
                    if w != ws
                ])
            )
            if len(app.websockets) > 1
            else "No one else is currently in chat"
        )
        await send_to_ws(ws, type="active_users", content=p)
    elif cmd in ["nick", "nickname"]:
        # TODO: do `clearall` before time check so you can always clear usernames if you are admin
        d = app.last_nick_change[ws.name] + td(seconds=app.args["nicknamecooldown"])
        with app.database as db:
            if not d < dt.now():
                if not db.is_admin(ws.name):
                    return await send_to_ws(ws, content=f"Please wait before changing your nickname again. " \
                        f"You can next change your nickname {naturaltime(d)}")
        if args == []:
            return await send_to_ws(ws, content=f"You need to specify a nickname to set. " \
                "If you want to clear your nickname, use the command \"{app.args['commandprefix']}{cmd} clear\"")
        nick = " ".join(args)[:15]
        if nick.lower() in [i.lower() for i in app.nicknamenonolist]:
            return await send_to_ws(ws, content=f"This nickname is either already taken " \
                "or is someone else's username")

        # Change the nickname
        with app.database as db:
            if nick.lower() == "clear":
                db.set_user_nickname(ws.name, "")
                ws.nickname = ""
                return await send("system", f"{ws.name} has cleared their nickname")
            if nick.lower() == "clearall":
                if not db.is_admin(ws.name):
                    return await send_to_ws(ws, content="You don't have enough permissions to use this command")
                for u in db("SELECT username FROM users"):
                    usr = u[0]
                    db.set_user_nickname(usr, "")

                b = f"{ws.name} has cleared all user nicknames. "
                b += (", ".join([
                        f"{w.nickname} is now {w.name}"
                        for w in app.websockets
                        if w.nickname != ""
                    ])
                    or "No nicknames to clear"
                )

                for w in app.websockets:
                    w.nickname = ""
                return await send("system", b)

            app.last_nick_change[ws.name] = dt.now()
            db.set_user_nickname(ws.name, nick)
            ws.nickname = nick
            await send("system", f"{ws.name} has changed their nickname to {nick}")
    elif cmd in ["del", "delete"]:
        pass # Don't show a message
        # TODO: Make it so a user can delete their own messages even if they don't have admin
    else:
        return True