wechaty / python-wechaty

Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
https://wechaty.readthedocs.io/zh_CN/latest/
Apache License 2.0
1.56k stars 229 forks source link

🐛🐛 Bug Report: Error occurred when use setter contactSelf.name change the bot username #290

Closed GaryDu0123 closed 2 years ago

GaryDu0123 commented 2 years ago

requirements

Error occurred when use setter contactSelf.name change the bot username

Reproduce your problem

import sys
import asyncio
from datetime import datetime
from wechaty import Wechaty
from wechaty.user import ContactSelf

class MyBot(Wechaty):

    async def on_login(self, contact: ContactSelf) -> None:
        old_name = contact.name
        try:
            contact.name = f"{old_name}{datetime.now()}"
        except Exception as e:
            print("更改名字失败", e, file=sys.stderr)

asyncio.run(MyBot().start())

Error info

2022-01-06 12:54:31,485 - Wechaty - INFO - init_puppet_event_bridge() <<wechaty_puppet_service.puppet.PuppetService object at 0x000001BCA22E57F0>>
2022-01-06 12:54:31,485 - Wechaty - INFO - initPuppetEventBridge() puppet.on(dong) (listenerCount:1) registering...
2022-01-06 12:54:31,485 - Wechaty - INFO - initPuppetEventBridge() puppet.on(error) (listenerCount:1) registering...
2022-01-06 12:54:31,486 - Wechaty - INFO - initPuppetEventBridge() puppet.on(heart-beat) (listenerCount:1) registering...
2022-01-06 12:54:31,486 - Wechaty - INFO - initPuppetEventBridge() puppet.on(ready) (listenerCount:1) registering...
2022-01-06 12:54:31,486 - Wechaty - INFO - receive <reset> event <%s>
2022-01-06 12:54:31,486 - Wechaty - INFO - initPuppetEventBridge() puppet.on(reset) (listenerCount:0) registering...
2022-01-06 12:54:31,487 - Wechaty - INFO - initPuppetEventBridge() puppet.on(friendship) (listenerCount:1) registering...
2022-01-06 12:54:31,487 - Wechaty - INFO - initPuppetEventBridge() puppet.on(login) (listenerCount:1) registering...
2022-01-06 12:54:31,487 - Wechaty - INFO - initPuppetEventBridge() puppet.on(logout) (listenerCount:1) registering...
2022-01-06 12:54:31,487 - Wechaty - INFO - initPuppetEventBridge() puppet.on(message) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-invite) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-join) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-leave) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - initPuppetEventBridge() puppet.on(room-topic) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - initPuppetEventBridge() puppet.on(scan) (listenerCount:1) registering...
2022-01-06 12:54:31,488 - Wechaty - INFO - starting puppet ...
2022-01-06 12:54:31,488 - HostiePuppet - INFO - init puppet
2022-01-06 12:54:31,492 - HostiePuppet - INFO - starting the puppet ...
2022-01-06 12:54:31,630 - HostiePuppet - INFO - puppet has started ...
2022-01-06 12:54:31,630 - HostiePuppet - INFO - listening the event from the puppet ...
2022-01-06 12:54:32,641 - wechaty.plugin - INFO - init the plugins ...
2022-01-06 12:54:32,641 - Wechaty - INFO - receive <login> event <EventLoginPayload(contact_id='wxid_5rxffo4dnb****')>
2022-01-06 12:54:32,645 - Contact - INFO - load contact <Contact <wxid_5rxffo4dnb*> <ソ**>>
更改名字失败 asyncio.run() cannot be called from a running event loop
D:/Project/SaltBot/minBot.py:34: RuntimeWarning: coroutine 'PuppetService.contact_self_name' was never awaited
  print("更改名字失败", e, file=sys.stderr)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
wj-Mcat commented 2 years ago

Please tell me the version of wechaty-puppet-version.

From the description of your log, I believe that you call contact_self_name handly and not awaited which is not correct.

If you want to change the name of bot, you can use ContactSelf. Hope that can help you.

GaryDu0123 commented 2 years ago

https://github.com/wechaty/python-wechaty/pull/289#discussion_r780790984 Because it wasn't decorated with async, so I can't use await right (correct me if I am wrong, thanks)

wj-Mcat commented 2 years ago

After reviewing the code of ContactSelf module, it's absolutely a bug which use async.run code block to run async based method.