Skip to content

Commit

Permalink
bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
franc-ist committed Mar 13, 2018
1 parent e09376b commit c0b6fb1
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
dblpy.egg-info/
dbl/__pycache__/
dbl/webhookserverhandler.py
build/
dist/
/docs/_build
/docs/_static/
/docs/_templates
.vscode
.vscode
22 changes: 9 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ DBL Python Library
:alt: View on PyPi
.. image:: https://img.shields.io/pypi/pyversions/dblpy.svg
:target: https://pypi.python.org/pypi/dblpy
:alt: v0.1.4
.. image:: https://readthedocs.org/projects/dblpy/badge/?version=v0.1.4
:target: http://dblpy.readthedocs.io/en/latest/?badge=v0.1.4
:alt: v0.1.6
.. image:: https://readthedocs.org/projects/dblpy/badge/?version=v0.1.6
:target: http://dblpy.readthedocs.io/en/latest/?badge=v0.1.6
:alt: Documentation Status

A simple API wrapper for `discordbots.org`_ written in Python
Expand Down Expand Up @@ -66,25 +66,21 @@ Example
def __init__(self, bot):
self.bot = bot
self.token = 'dbl_token' # set this to your DBL token
self.dblpy = dbl.Client(self.bot, self.token)
self.bot.loop.create_task(self.update_stats())
self.dblpy = dbl.Client(self.bot, self.token, loop=bot.loop)
self.updating = bot.loop.create_task(self.update_stats())
async def update_stats(self):
"""This function runs every 30 minutes to automatically update your server count"""
while True:
logger.info('attempting to post server count')
await self.bot.is_ready()
while not bot.is_closed:
logger.info('Attempting to post server count')
try:
await self.dblpy.post_server_count()
logger.info('posted server count ({})'.format(len(self.bot.guilds)))
logger.info('Posted server count ({})'.format(len(self.bot.guilds)))
except Exception as e:
logger.exception('Failed to post server count\n{}: {}'.format(type(e).__name__, e))
await asyncio.sleep(1800)
def __unload(self):
self.bot.loop.create_task(self.session.close())
def setup(bot):
global logger
logger = logging.getLogger('bot')
Expand Down
4 changes: 2 additions & 2 deletions dbl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__author__ = 'Francis Taylor'
__license__ = 'MIT'
__copyright__ = 'Copyright 2018 Francis Taylor'
__version__ = '0.1.4'
__version__ = '0.1.6'

from collections import namedtuple

Expand All @@ -22,4 +22,4 @@

VersionInfo = namedtuple('VersionInfo', 'major minor micro releaselevel serial')

version_info = VersionInfo(major=0, minor=1, micro=4, releaselevel='final', serial=0)
version_info = VersionInfo(major=0, minor=1, micro=6, releaselevel='final', serial=0)
85 changes: 65 additions & 20 deletions dbl/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
"""

import asyncio
#import string
#import random
import logging

from . import __version__ as library_version
from .errors import *
from .http import HTTPClient
#from .errors import InvalidArgument

log = logging.getLogger(__name__)


class Client:
Expand Down Expand Up @@ -67,15 +72,16 @@ async def __ainit__(self):
self.bot_id = self.bot.user.id

def guild_count(self):
"""Gets the guild count from the bot object"""
try:
return len(self.bot.guilds)
except AttributeError:
return len(self.bot.servers)

async def post_server_count(
self,
shard_count: int = None,
shard_no: int = None
self,
shard_count: int = None,
shard_no: int = None
):
"""This function is a coroutine.
Expand Down Expand Up @@ -246,15 +252,15 @@ async def get_user_info(self, user_id: int):
return await self.http.get_user_info(user_id)

async def generate_widget_large(
self,
bot_id: int = None,
top: str = '2C2F33',
mid: str = '23272A',
user: str = 'FFFFFF',
cert: str = 'FFFFFF',
data: str = 'FFFFFF',
label: str = '99AAB5',
highlight: str = '2C2F33'
self,
bot_id: int = None,
top: str = '2C2F33',
mid: str = '23272A',
user: str = 'FFFFFF',
cert: str = 'FFFFFF',
data: str = 'FFFFFF',
label: str = '99AAB5',
highlight: str = '2C2F33'
):
"""This function is a coroutine.
Expand Down Expand Up @@ -313,13 +319,13 @@ async def get_widget_large(self, bot_id: int = None):
return url

async def generate_widget_small(
self,
bot_id: int = None,
avabg: str = '2C2F33',
lcol: str = '23272A',
rcol: str = '2C2F33',
ltxt: str = 'FFFFFF',
rtxt: str = 'FFFFFF'
self,
bot_id: int = None,
avabg: str = '2C2F33',
lcol: str = '23272A',
rcol: str = '2C2F33',
ltxt: str = 'FFFFFF',
rtxt: str = 'FFFFFF'
):
"""This function is a coroutine.
Expand Down Expand Up @@ -374,6 +380,45 @@ async def get_widget_small(self, bot_id: int = None):
url = 'https://discordbots.org/api/widget/lib/{0}.png'.format(bot_id)
return url

# async def start_vote_post(self, url: str = None, auth: str = None):
# """This function is a coroutine.
#
# Sets up webhooks for posting a message to a Discord channel whenever someone votes on your bot.
#
# .. note::
#
# This function will automatically start a webserver to listen to incoming requests. You should point the webhooks to the IP or domain of your host.
#
# Parameters
# ==========
#
# auth: str[Optional]
# The authorization token (password) that will be used to verify requests coming back from DBL. Generate a random token with ``auth_generator()``
#
# Returns
# =======
#
# bot_id: int
# ID of the bot that received a vote.
# user_id: int s.wfile.write('Hello!')
# ID of the user who voted.
# type: str
# The type of vote. 'upvote' or 'none' (unvote)
# query?: str
# Query string params found on the `/bot/:ID/vote` page.
# """
# if auth is None:
# log.warn(
# 'Webhook validation token is Null. Please set one, or generate one using `auth_generator()`.')
#
# await self.http.initialize_webhooks(url, auth)

# async def auth_generator(self, size=32, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
# """This function is a coroutine
#
# Generates a random auth token for webhook validation."""
# return ''.join(random.SystemRandom().choice(chars) for _ in range(size))

async def close(self):
"""This function is a coroutine.
Closes all connections."""
Expand Down
2 changes: 1 addition & 1 deletion dbl/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Unauthorized(HTTPException):
pass


class Unauthorized_Detected(DBLException):
class UnauthorizedDetected(DBLException):
"""Exception that's thrown when no API Token is provided
Subclass of :exc:`DBLException`
Expand Down
Loading

0 comments on commit c0b6fb1

Please sign in to comment.