Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Discord.py:如何创建自定义函数notify和call来触发Discord客户端调用它_Python 3.x_Discord.py_Python Asyncio - Fatal编程技术网

Python 3.x Discord.py:如何创建自定义函数notify和call来触发Discord客户端调用它

Python 3.x Discord.py:如何创建自定义函数notify和call来触发Discord客户端调用它,python-3.x,discord.py,python-asyncio,Python 3.x,Discord.py,Python Asyncio,我想让discord客户机执行一个特定的函数,而不会被on消息触发,或者被on ready触发,或者让循环自身重复 基本上,我需要一个函数notify(msg)并使discord客户端向通道发送消息 我试过以下方法,但没有成功 async notify(message, client): await client.wait_until_ready() channel = client.get_channel(os.getenv('CHANNEL_ID')) await c

我想让discord客户机执行一个特定的函数,而不会被on消息触发,或者被on ready触发,或者让循环自身重复

基本上,我需要一个函数notify(msg)并使discord客户端向通道发送消息

我试过以下方法,但没有成功

async notify(message, client):
    await client.wait_until_ready()
    channel = client.get_channel(os.getenv('CHANNEL_ID'))
    await channel.send(message)

client.run(os.getenv('CLIENT_TOKEN'))

# Notice that I try to run the task after the client run
# Because I may call notify multiple times after the client has been created

client.loop.create_task(notify())

我希望在创建客户端之后调用此函数,而不无限期地循环

也试着跑

asyn def main():
    await client.connect()
    await client.log(client.run(os.getenv('CLIENT_TOKEN')))
    await notify('My message', client)

# then

if __name__ = "__main__":
  asyncio.run(main())

这只是不运行。。。http请求等出现错误。

client.loop.create_任务(notify(*args))

它传递要在客户机循环中执行的函数,并且在需要时只调用一次。

我正在寻找的是Webhook。 客户端的角色是对预定义的事件(如启动时、收到消息时等)进行操作。 如果我们想从外部触发某些东西,Discord提供了Webhook:

基本上,Webhook是我们可以向其发送数据的不协调通道的唯一URL,最小的数据形式为{'content':“MyMessage} 它发送了一个简单的消息。您可以通过包含嵌入来进一步改进这一点

下面是一个使用aihttp的基本示例

从discord导入Webhook,AsyncWebhookAdapter
进口aiohttp
异步def foo():
与aiohttp.ClientSession()作为会话异步:
webhook=webhook.from_url('url-here',adapter=AsyncWebhookAdapter(会话))
等待webhook.send('helloworld',username='Foo')
这里是另一个使用请求库的示例

导入请求
从discord导入Webhook,请求SwebhookAdapter
webhook=webhook.partial(123456,'abcdefg',adapter=RequestsWebhookAdapter())
webhook.send('helloworld',username='Foo')