用python编写的discordbots.org的简单api包装器
dblp的Python项目详细描述
用python编写的discordbots.org的简单api包装器
安装
通过PIP安装(推荐)
pip install dblpy
从源安装
pip install git+https://github.com/DiscordBotList/DBL-Python-Library
文件
可以找到文档here
特点
- POST服务器计数
- 获取机器人信息、服务器计数、投票信息
- 获取所有机器人程序
- 获取用户信息
- 获取小部件(大部件和小部件),包括自定义部件有关详细信息,请参见discordbots.org/api/docs
- 获取周末状态
- 内置webhook,帮助您处理dbl upvots
- 自动服务器计数发布
- 通过API搜索机器人程序
其他信息
- 在使用此库提供的webhook之前,请确保已指定端口打开。
- webhook_port必须大于1024到49151。
示例
没有webhook:
importdblimportdiscordfromdiscord.extimportcommands,tasksimportasyncioimportloggingclassDBLAPI(commands.Cog):"""Handles interactions with the discordbots.org API"""def__init__(self,bot):self.bot=botself.token='dbl_token'# set this to your DBL tokenself.dblpy=dbl.DBLClient(self.bot,self.token)# The decorator below will work only on discord.py 1.1.0+# In case your discord.py version is below that, you can use self.bot.loop.create_task(self.update_stats())@tasks.loop(minutes=30.0)asyncdefupdate_stats(self):"""This function runs every 30 minutes to automatically update your server count"""logger.info('Attempting to post server count')try:awaitself.dblpy.post_guild_count()logger.info('Posted server count ({})'.format(self.dblpy.guild_count()))exceptExceptionase:logger.exception('Failed to post server count\n{}: {}'.format(type(e).__name__,e))# if you are not using the tasks extension, put the line belowawaitasyncio.sleep(1800)defsetup(bot):globalloggerlogger=logging.getLogger('bot')bot.add_cog(DBLAPI(bot))
使用webhook:
importdblimportdiscordfromdiscord.extimportcommands,tasksimportasyncioimportloggingclassDBLAPI(commands.Cog):"""Handles interactions with the discordbots.org API"""def__init__(self,bot):self.bot=botself.token='dbl_token'# set this to your DBL tokenself.dblpy=dbl.DBLClient(self.bot,self.token,webhook_path='/dblwebhook',webhook_auth='password',webhook_port=5000)# The decorator below will work only on discord.py 1.1.0+# In case your discord.py version is below that, you can use self.bot.loop.create_task(self.update_stats())@tasks.loop(minutes=30.0)asyncdefupdate_stats(self):"""This function runs every 30 minutes to automatically update your server count"""logger.info('Attempting to post server count')try:awaitself.dblpy.post_guild_count()logger.info('Posted server count ({})'.format(self.dblpy.guild_count()))exceptExceptionase:logger.exception('Failed to post server count\n{}: {}'.format(type(e).__name__,e))# if you are not using the tasks extension, put the line belowawaitasyncio.sleep(1800)@commands.Cog.listener()asyncdefon_dbl_vote(self,data):logger.info('Received an upvote')print(data)defsetup(bot):globalloggerlogger=logging.getLogger('bot')bot.add_cog(DBLAPI(bot))
使用autopost:
importdblimportdiscordfromdiscord.extimportcommandsclassDBLAPI(commands.Cog):"""Handles interactions with the discordbots.org API"""def__init__(self,bot):self.bot=botself.token='dbl_token'# set this to your DBL tokenself.dblpy=dbl.DBLClient(self.bot,self.token,autopost=True)# Autopost will post your guild count every 30 minutesasyncdefon_guild_post():print("Server count posted successfully")defsetup(bot):bot.add_cog(DBLAPI(bot))