|
1 | 1 | import nextcord |
2 | 2 | from nextcord.ext import commands |
3 | 3 | from shared.config import CHANNEL_ID, DISCORD_TOKEN, GUILD_ID |
| 4 | +from prometheus_client import start_http_server, Gauge |
| 5 | +import time |
| 6 | + |
| 7 | +BOT_STATUS = Gauge('discord_bot_status', 'Status of the Discord bot', ['bot_name']) |
| 8 | +bot_name = 'Kernel' |
4 | 9 |
|
5 | 10 | # Create an instance of the bot with all intents enabled |
6 | 11 | intents = nextcord.Intents.all() |
@@ -36,17 +41,30 @@ async def update_message(content): |
36 | 41 |
|
37 | 42 | @bot.event |
38 | 43 | async def on_ready(): |
39 | | - await bot.tree.sync() |
40 | 44 | """ |
41 | 45 | An event listener for when the bot is ready and operational. |
42 | 46 |
|
43 | 47 | Prints a message to the console to indicate that the bot is ready. |
44 | 48 | """ |
45 | | - print('Bot is ready.') |
| 49 | + print(f'{bot_name} has connected to Discord!') |
| 50 | + # Mark the bot as "up" when it is online |
| 51 | + BOT_STATUS.labels(bot_name=bot_name).set(1) |
46 | 52 |
|
47 | 53 | # Importing slash commands and context menus |
48 | 54 | # from . import slash_commands, context_menus |
49 | 55 |
|
| 56 | +@bot.event |
| 57 | +async def on_disconnect(): |
| 58 | + print(f'{bot_name} has disconnected from Discord!') |
| 59 | + # Mark the bot as "down" when it is offline |
| 60 | + BOT_STATUS.labels(bot_name=bot_name).set(0) |
| 61 | + |
| 62 | +def start_prometheus_server(): |
| 63 | + # Start a Prometheus HTTP server on port 8000 |
| 64 | + start_http_server(8000) |
| 65 | + print("Prometheus server started on port 8000") |
| 66 | + |
50 | 67 | if __name__ == '__main__': |
| 68 | + start_prometheus_server() |
51 | 69 | # Run the bot with the specified token |
52 | 70 | bot.run(DISCORD_TOKEN) |
0 commit comments