不协调webhook的(a)同步包装
dhooks的Python项目详细描述
不和谐的网络挂钩
这个simple库使您能够轻松地与不协调的webhook交互,允许您轻松地格式化不协调消息和不协调嵌入,检索webhook信息,修改和删除webhook。也支持异步使用。
安装
要安装库,只需使用pipenv(当然是pip)。
pipenv install dhooks
快速启动
发送消息:
fromdhooksimportWebhookhook=Webhook('url')hook.send("Hello there! I'm a webhook :open_mouth:")
不和谐嵌入:
您可以使用此库轻松格式化和发送嵌入。
注意:discord.py
中的Embed
对象也与此库兼容。
fromdhooksimportWebhook,Embedhook=Webhook('url')embed=Embed(description='This is the **description** of the embed! :smiley:',color=0x1e0f3,timestamp=True# sets the timestamp to current time)image1='https://i.imgur.com/rdm3W9t.png'image2='https://i.imgur.com/f1LOr4q.png'embed.set_author(name='Author Goes Here',icon_url=image1)embed.add_field(name='Test Field',value='Value of the field :open_mouth:')embed.add_field(name='Another Field',value='1234 :smile:')embed.set_footer(text='Here is my footer text',icon_url=image1)embed.set_thumbnail(image1)embed.set_image(image2)hook.send(embeds=embed)
发送文件:
如图所示,您可以轻松地发送文件。
fromdhooksimportWebhook,FilefromioimportBytesIOimportrequestshook=Webhook('url')file=File('path/to/file.png',name='cat.png')# optional name for discordhook.send('Look at this:',file=file)
您还可以传递类似文件的对象:
response=requests.get('https://i.imgur.com/rdm3W9t.png')file=File(BytesIO(response.content),name='wow.png')hook.send('Another one:',file=file)
获取webhook信息:
您可以通过discord的api获得一些与webhook相关的基本信息。
hook.get_info()
以下属性将填充来自discord的数据:
hook.guild_id
hook.channel_id
hook.default_name
hook.default_avatar_url
修改和删除webhooks:
您可以轻松更改webhook的默认名称和虚拟形象。
withopen('img.png',rb)asf:img=f.read()# byteshook.modify(name='Bob',avatar=img)hook.delete()# webhook deleted permanently
异步使用:
要使用aiohttp
异步发出请求,只需使用Webhook.Async
创建对象。一个例子如下。调用api方法时只需使用await
关键字。
fromdhooksimportWebhookasyncdefmain():hook=Webhook.Async('url')awaithook.send('hello')awaithook.modify('bob')awaithook.get_info()awaithook.delete()awaithook.close()# close the client session
或者,可以使用async with
块(异步上下文管理器)在完成后自动关闭会话。
asyncdefmain():asyncwithWebhook.Async('url')ashook:awaithook.send('hello')
文档
您可以找到完整的api引用here。
许可证
这个项目是麻省理工学院授权的。
贡献
请随时为这个项目做贡献,我们永远感激您的帮助。