Skip to content

Commit e2e1815

Browse files
committed
✨ Add status push extension
1 parent d366d1e commit e2e1815

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ We provide multiple extensions directly within this project to get you started.
150150
- [`branding`](src/extensions/branding/readme.md): An extension to customize the bot's presence and status, and embed aesthetics.
151151
- [`add-dm`](src/extensions/add-dm/readme.md): An extension to send a direct message to the user who adds the bot to a guild.
152152
- [`nice-errors`](src/extensions/nice-errors/readme.md): An extension to provide user-friendly error messages during command execution.
153+
- [`status-post`](src/extensions/status-post/readme.md): An extension to post the bot's status to a specified URL.
153154

154155
Read the provided documentation for each extension to learn more about their features and how to configure them.
155156

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .main import setup, setup_webserver, on_startup, default, schema

src/extensions/status-post/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import discord
2+
import aiohttp
3+
4+
from discord.ext import commands
5+
from schema import Schema
6+
from src.logging import logger
7+
from discord.ext import tasks
8+
9+
10+
default = {
11+
"enabled": False,
12+
"url": "",
13+
"every": 60,
14+
}
15+
16+
schema = Schema(
17+
{
18+
"enabled": bool,
19+
"url": str,
20+
"every": int,
21+
}
22+
)
23+
24+
25+
class Status(commands.Cog):
26+
def __init__(self, bot: discord.Bot, config: dict):
27+
self.bot = bot
28+
self.config = config
29+
tasks.loop(seconds=self.config["url"])(self.push_status_loop).start()
30+
31+
@commands.Cog.listener(once=True)
32+
async def on_ready(self):
33+
self.push_status_loop.start()
34+
35+
async def push_status_loop(self):
36+
try:
37+
await self.push_status()
38+
logger.info("Pushed status.")
39+
except Exception as e:
40+
logger.error(f"Failed to push status: {e}")
41+
42+
async def push_status(self):
43+
ping = str(round(self.bot.latency * 1000))
44+
async with aiohttp.ClientSession() as session:
45+
async with session.get(self.config["url"] + ping) as resp:
46+
resp.raise_for_status()
47+
48+
def setup(bot: discord.Bot, config: dict):
49+
bot.add_cog(Status(bot, config))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Status Extension
2+
3+
The Status extension is a straightforward, yet essential part of your Botkit.
4+
It requires minimal configuration and can be enabled or disabled as needed.
5+
6+
## Features
7+
8+
The Status extension periodically pushes the bot's status to a specified URL.
9+
This can be useful for monitoring the bot's health and responsiveness.
10+
11+
## Usage
12+
13+
To use the Status extension, configure the `url` and `every` keys in the `config.yml` file or through environment variables. The bot will push its status to the specified URL at the configured interval.
14+
15+
## Configuration
16+
17+
The Status extension requires the following configuration:
18+
19+
- `enabled`: A boolean indicating whether the extension is enabled.
20+
- `url`: The URL to which the bot's status will be pushed.
21+
- `every`: The interval (in seconds) at which the bot's status will be pushed.
22+
23+
Example configuration in `config.yml`:
24+
25+
```yaml
26+
status:
27+
enabled: true
28+
url: "http://example.com/status"
29+
every: 60
30+
```
31+
32+
## Contributing
33+
If you wish to contribute to the development of the Status extension, please feel free to submit a pull request. We appreciate your help in making this extension better.

0 commit comments

Comments
 (0)