代码之家  ›  专栏  ›  技术社区  ›  MW2023

如何创建一个不和谐的Bot,它可以接受Python关于向谁发送什么消息的参数,并且能够关闭自己?

  •  0
  • MW2023  · 技术社区  · 5 年前

    我想做一些很简单的事。我只希望Python能够调用一个快速函数,通过Discord发送直接消息。基本上是 FAQ example

    import discord
    class MyClient(discord.Client):
        async def on_ready(self):
            print('Logged on as {0}!'.format(self.user))
            user = await self.fetch_user(self.destination_user)
            await user.send(self.message)
            await self.close()
        def __init__(self, user_id, user_message):
            self.destination_user = user_id
            self.message = user_message
    
    client = MyClient(desired_id, desired_message)
    client.run('My Client ID')
    #wait for client to end and then continue
    

    但是,我遇到了两个问题。看起来不和谐。客户不允许 __init__() __初始化() 蟒蛇 ,而不是对不和谐的信息做出反应?也, self.close() 结果 "RuntimeError: Cannot close a running event loop" on_ready() 完成然后关闭它?

    __初始化() 只是不关门。然而,出于某种原因,即便如此 self.get_user() 不起作用,只是 self.fetch_user() . 医生说用 get_user() 获取用户() 继续返回“无”,而 fetch_user()

    0 回复  |  直到 5 年前