Discord.py: 如何创建自定义函数通知和调用触发不和谐客户端调用它
Discord.py: how to create a custom function notify and call trigger the discord client to invoke it
我想要求一个不和谐的客户端执行一个特定的功能,而不是由 on 消息触发,或者准备好或有一个重复的循环。
基本上,我想要一个函数 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_task(notify(*args))
在客户端循环中传递要执行的函数,只要你想调用一次。
我要找的是 webhook。
客户端的作用是对预定义的事件(如开始、收到消息等)进行操作。
如果我们想在外部触发一些东西,Discord 提供了 webhooks:
https://discordpy.readthedocs.io/en/stable/api.html?highlight=webhooks#discord.TextChannel.webhooks
基本上,webhooks 是我们可以向其发送数据的不和谐频道的唯一 url,最小数据的形式为 {'content': "my message}
它发送一个简单的消息。您可以通过包含嵌入来进一步改进这一点。
这是一个使用 aihttp 的基本示例
from discord import Webhook, AsyncWebhookAdapter
import aiohttp
async def foo():
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url('url-here', adapter=AsyncWebhookAdapter(session))
await webhook.send('Hello World', username='Foo')
这是另一个使用 Requests 库的例子
import requests
from discord import Webhook, RequestsWebhookAdapter
webhook = Webhook.partial(123456, 'abcdefg', adapter=RequestsWebhookAdapter())
webhook.send('Hello World', username='Foo')
我想要求一个不和谐的客户端执行一个特定的功能,而不是由 on 消息触发,或者准备好或有一个重复的循环。
基本上,我想要一个函数 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_task(notify(*args))
在客户端循环中传递要执行的函数,只要你想调用一次。
我要找的是 webhook。 客户端的作用是对预定义的事件(如开始、收到消息等)进行操作。 如果我们想在外部触发一些东西,Discord 提供了 webhooks:
https://discordpy.readthedocs.io/en/stable/api.html?highlight=webhooks#discord.TextChannel.webhooks
基本上,webhooks 是我们可以向其发送数据的不和谐频道的唯一 url,最小数据的形式为 {'content': "my message} 它发送一个简单的消息。您可以通过包含嵌入来进一步改进这一点。
这是一个使用 aihttp 的基本示例
from discord import Webhook, AsyncWebhookAdapter
import aiohttp
async def foo():
async with aiohttp.ClientSession() as session:
webhook = Webhook.from_url('url-here', adapter=AsyncWebhookAdapter(session))
await webhook.send('Hello World', username='Foo')
这是另一个使用 Requests 库的例子
import requests
from discord import Webhook, RequestsWebhookAdapter
webhook = Webhook.partial(123456, 'abcdefg', adapter=RequestsWebhookAdapter())
webhook.send('Hello World', username='Foo')