Auto-post API Reference¶
- class topgg.autopost.AutoPoster(client: DBLClient)[source]¶
A helper class for autoposting. Takes in a
DBLClient
to instantiate.Note
You should not instantiate this unless you know what you’re doing. Generally, you’d better use the
autopost()
method.- Parameters
client (
DBLClient
) – An instance of DBLClient.
- cancel() None [source]¶
Cancels the autoposting loop.
Note
This differs from
stop()
because this will stop the loop right away.
- on_error(callback: None) Callable[[Callable[[...], Any]], Callable[[...], Any]] [source]¶
- on_error(callback: Callable[[...], Any]) AutoPoster
Registers an autopost error callback. The callback can be either sync or async.
The callback is expected to take in the exception being raised, you can also have injected
data
. This method can be used as a decorator or a decorator factory.Note
If you don’t provide an error callback, the default error handler will be called.
- Example
# The following are valid. autopost = dblclient.autopost().on_error(lambda exc: print("Failed posting stats!", exc)) # Used as decorator, the decorated function will become the AutoPoster object. @autopost.on_error def autopost(exc: Exception): ... # Used as decorator factory, the decorated function will still be the function itself. @autopost.on_error() def on_error(exc: Exception): ...
- on_success(callback: None) Callable[[Callable[[...], Any]], Callable[[...], Any]] [source]¶
- on_success(callback: Callable[[...], Any]) AutoPoster
Registers an autopost success callback. The callback can be either sync or async.
The callback is not required to take in arguments, but you can have injected
data
. This method can be used as a decorator or a decorator factory.- Example
# The following are valid. autopost = dblclient.autopost().on_success(lambda: print("Success!")) # Used as decorator, the decorated function will become the AutoPoster object. @autopost.on_success def autopost(): ... # Used as decorator factory, the decorated function will still be the function itself. @autopost.on_success() def on_success(): ...
- set_interval(seconds: Union[float, datetime.timedelta]) topgg.autopost.AutoPoster [source]¶
Sets the interval between posting stats.
- Parameters
seconds (
typing.Union
[float
,datetime.timedelta
]) – The interval.- Raises
ValueError – If the provided interval is less than 900 seconds.
- start() asyncio.Task[None] [source]¶
Starts the autoposting loop.
Note
This method must be called when the event loop has already running!
- Raises
TopGGException – If there’s no callback provided or the autopost is already running.
- stats(callback: None) Callable[[Callable[[], topgg.types.StatsWrapper]], Callable[[], topgg.types.StatsWrapper]] [source]¶
- stats(callback: Callable[[], topgg.types.StatsWrapper]) AutoPoster
Registers a function that returns an instance of
StatsWrapper
.The callback can be either sync or async. The callback is not required to take in arguments, but you can have injected
data
. This method can be used as a decorator or a decorator factory.- Example
import topgg # In this example, we fetch the stats from a Discord client instance. client = Client(...) dblclient = topgg.DBLClient(TOKEN).set_data(client) autopost = ( dblclient .autopost() .on_success(lambda: print("Successfully posted the stats!") ) @autopost.stats() def get_stats(client: Client = topgg.data(Client)): return topgg.StatsWrapper(guild_count=len(client.guilds), shard_count=len(client.shards)) # somewhere after the event loop has started autopost.start()