小编Iqb*_*wan的帖子

即使在 django 和discord.py 中使用sync_to_async 后也出现 SynchronousOnlyOperation 错误

我正在尝试为 Django 网站制作一个不和谐的机器人。在实现的时候我发现Django数据库不允许异步操作,我们必须使用线程或sync_to_async.

我使用过sync_to_async并且似乎有效。但后来我遇到了一个问题,我什至无法弄清楚它出了什么问题以及那里发生了什么。

谁能解释我在那里做错了什么以及那里发生了什么?

@sync_to_async
def get_customer(id):
    try:
        return Customer.objects.get(discord_id=id)
    except:
        return None
#------

@bot.command()
async def categories(ctx):
    customer = await get_customer(ctx.author.id) #this line of code works well
    categories = await sync_to_async(Category.objects.all().filter)(customer=customer)
    #categories = await sync_to_async(Category.objects.filter)(customer=customer)#I tried this also, didn't work
    print(categories) #problem is in the line
    embed = Embed(title='', description="{}".format('\n'.join([ x.name for x in categories])))
    await ctx.send(embed=embed)
Run Code Online (Sandbox Code Playgroud)

我收到的回溯错误是:

Running bot
Ignoring exception in command categories:
Traceback (most recent call last):
  File "/Users/rhidwan/Desktop/personal_transaction/venv/lib/python3.7/site-packages/discord/ext/commands/core.py", line 83, …
Run Code Online (Sandbox Code Playgroud)

python django django-orm python-asyncio discord.py

1
推荐指数
1
解决办法
8321
查看次数

使用 Telethon 从 event.message 对象获取发件人的用户名

当消息到达组时,我无法获取 event.message 对象的发件人的用户名。

我尝试了 get_entity、get_input_entity 方法。但这显示错误。AttributeError: 'coroutine' 对象没有属性 'chat' 我什至不知道如何处理 coroutine 对象

@client.on(events.NewMessage(chats=input_groups_entity))
    async def handler(event):
        print(event)
        sender = event.message.chat_id
        perticipants = client.get_participants(event.message.to_id)
        np = client.get_input_entity(event.message.from_id)
        await client.send_message(event.message.to_id, message=event.message.message)
Run Code Online (Sandbox Code Playgroud)

我只想从消息对象中提取用户名

telegram telegram-bot telethon

0
推荐指数
1
解决办法
3052
查看次数