Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XXX: Wechaty User Class (WUC) can not be instantiated directly! #1217

Closed
huan opened this issue May 19, 2018 · 6 comments
Closed

XXX: Wechaty User Class (WUC) can not be instantiated directly! #1217

huan opened this issue May 19, 2018 · 6 comments
Labels

Comments

@huan
Copy link
Member

huan commented May 19, 2018

TL;DR

If you come here by following the error message:

UserClass: Wechaty User Class (WUC) can not be instantiated directly! See: https://github.com/wechaty/wechaty/issues/1217**

Solution

Use wechaty.UserClass instead of using UserClass directly.

For examples:

- const rooms = await Room.findAll(/topic/i)
+ const rooms = await wechaty.Room.findAll(/topic/i)

Background

Error Message

  1. Error: static puppet not found for Contact/Room/Friendship/Message
  2. Error: class can not be instanciated directly!
  3. Error: must not use the global Message/Contact/Room. use a cloned child via cloneClass instead

Currently, the Contact, FriendRequest, Message, and Room classes will not be able to instantiate directly, because they must attach with a Puppet first.

They need to be cloneClass()-ed first, then attach the puppet, and at last they will be ready for use by:
wechaty.Contact

Do Not

import { Room } from 'wechaty'
const room = await Room.create(...)

The above code will throw error.

Do

- import { Room } from 'wechaty'
+ import { Wechaty } from 'wechaty'

- const room = await Room.create(...)
+ const wechaty = new Wechaty()
+ const room = await wechaty.Room.create(...)

So does Contact, FriendRequest, and Message.

See Also

  1. Wechaty Multi-Instance Suport [todo] allow Wechaty to be multi-instance #518
  2. NPM clone-class
  3. Behavior not expected when the class static property initialized in defination. huan/clone-class#5
@lijiarui
Copy link
Member

It may should be

const wechaty = Wechaty.instance()
const room = await wechaty.Room.create(...)

@yarray
Copy link

yarray commented Jun 1, 2018

I'm quite confused by "So does Contact, FriendRequest, and Message"... The property wechaty.Contact seems to be a Function without a method named "create". So how should I create a contact with id actually?

@huan
Copy link
Member Author

huan commented Jun 1, 2018

@yarray there's an example that demonstrated how to create a new room, hope that could help you to understand.

https://github.com/Chatie/wechaty-getting-started/blob/50540d93fec3566ae1880cca42fc5d4ee70607d1/examples/advanced/room-bot.js

BTW: You can never create a contact with id because you can only find it. wechaty.Contact.Find() is the right way to fulfill your case.

@huan huan changed the title Error: class can not be instanciated directly! Error: static puppet not found for XXX Jul 3, 2018
huan added a commit that referenced this issue Jul 3, 2018
@lhr0909
Copy link

lhr0909 commented Nov 30, 2018

@lijiarui @huan What happens if we need to use the type definitions for these classes?

See _contactPayload and the WechatBotAdapter, which currently we get yelled at for the extended WechatBotAdapter for the same issue.

image

@lhr0909
Copy link

lhr0909 commented Nov 30, 2018

Well, crisis averted. Sorry for the ping

image

qhduan added a commit to qhduan/botbuilder-wechaty-adapter that referenced this issue Jan 14, 2019
huan pushed a commit to wechaty/botbuilder-wechaty-adapter that referenced this issue Jan 15, 2019
#25)

* fix: error "The global Contact class can not be used directly! #wechaty/wechaty#1217 "

* fix: remove global contact define because we donnot use it anymore

* 3.0.2
@huan huan changed the title Error: static puppet not found for XXX XXX: Wechaty User Class (WUC) can not be instantiated directly! Sep 26, 2021
@zulkifalaziz
Copy link

zulkifalaziz commented Nov 2, 2023

How can I receive and display an image in wechaty Python? I have tried finding the solution in documentation but no success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants