hungernetbot 1.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hungernetbot/__init__.py +15 -0
- hungernetbot/__main__.py +4 -0
- hungernetbot/bot.py +126 -0
- hungernetbot/commands/__init__.py +11 -0
- hungernetbot/commands/general.py +42 -0
- hungernetbot/commands/github.py +117 -0
- hungernetbot/commands/posts.py +76 -0
- hungernetbot/commands/server.py +231 -0
- hungernetbot/config.py +81 -0
- hungernetbot/database.py +164 -0
- hungernetbot/embeds.py +31 -0
- hungernetbot/emojis.py +160 -0
- hungernetbot/github/__init__.py +3 -0
- hungernetbot/github/dispatcher.py +83 -0
- hungernetbot/github/events.py +246 -0
- hungernetbot/github/repo.py +16 -0
- hungernetbot/github/security.py +12 -0
- hungernetbot/github/server.py +59 -0
- hungernetbot/posts.py +253 -0
- hungernetbot/runner.py +15 -0
- hungernetbot/webhooks.py +126 -0
- hungernetbot-1.0.dev1.dist-info/METADATA +127 -0
- hungernetbot-1.0.dev1.dist-info/RECORD +27 -0
- hungernetbot-1.0.dev1.dist-info/WHEEL +5 -0
- hungernetbot-1.0.dev1.dist-info/entry_points.txt +2 -0
- hungernetbot-1.0.dev1.dist-info/licenses/LICENSE +674 -0
- hungernetbot-1.0.dev1.dist-info/top_level.txt +1 -0
hungernetbot/webhooks.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import discord
|
|
4
|
+
from mapres import res
|
|
5
|
+
from hungercore.webhook import WebhookServer
|
|
6
|
+
|
|
7
|
+
from .embeds import branded_embed, truncate
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_bridge_webhook(bot):
|
|
14
|
+
settings = bot.settings
|
|
15
|
+
server = WebhookServer(
|
|
16
|
+
port=settings.bridge_webhook_port,
|
|
17
|
+
token=settings.bridge_webhook_token,
|
|
18
|
+
host=settings.webhook_host,
|
|
19
|
+
logLevel='info',
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
async def endpoint_handler(event, data):
|
|
23
|
+
logger.info('Received HungerBridge event %s.', event or 'unknown')
|
|
24
|
+
|
|
25
|
+
async def send_notification(title, fields, alert_owner=False):
|
|
26
|
+
channel = bot.get_channel(settings.notification_channel_id)
|
|
27
|
+
if channel is None:
|
|
28
|
+
try:
|
|
29
|
+
channel = await bot.fetch_channel(settings.notification_channel_id)
|
|
30
|
+
except (discord.NotFound, discord.Forbidden):
|
|
31
|
+
logger.exception('Cannot access the configured notification channel.')
|
|
32
|
+
return
|
|
33
|
+
if not isinstance(channel, discord.abc.Messageable):
|
|
34
|
+
logger.error('Configured notification destination cannot receive messages.')
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
mentions = discord.AllowedMentions(
|
|
38
|
+
users=[discord.Object(id=settings.owner_id)] if alert_owner else False,
|
|
39
|
+
roles=False,
|
|
40
|
+
everyone=False,
|
|
41
|
+
replied_user=False,
|
|
42
|
+
)
|
|
43
|
+
content = f'<@{settings.owner_id}>' if alert_owner else None
|
|
44
|
+
try:
|
|
45
|
+
await channel.send(
|
|
46
|
+
content=content,
|
|
47
|
+
embed=branded_embed(title, fields),
|
|
48
|
+
allowed_mentions=mentions,
|
|
49
|
+
)
|
|
50
|
+
except (discord.Forbidden, discord.HTTPException):
|
|
51
|
+
logger.exception('Could not send HungerBridge event notification.')
|
|
52
|
+
|
|
53
|
+
async def restart_action_sent(data):
|
|
54
|
+
emoji = res(':bolt_blue:')
|
|
55
|
+
await send_notification(
|
|
56
|
+
f'{emoji} Restart action sent',
|
|
57
|
+
{'Server': data.get('server', settings.server_name), 'Details': 'A restart command was issued.'},
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
async def server_back_online(data):
|
|
61
|
+
emoji = res(':status_online:')
|
|
62
|
+
await send_notification(
|
|
63
|
+
f'{emoji} Server back online',
|
|
64
|
+
{'Server': data.get('server', settings.server_name), 'Status': 'The server is back online.'},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
async def server_failed_restart(data):
|
|
68
|
+
emoji = res(':status_warning:')
|
|
69
|
+
await send_notification(
|
|
70
|
+
f'{emoji} Server restart failed',
|
|
71
|
+
{'Server': data.get('server', settings.server_name), 'Status': 'The server failed to restart.'},
|
|
72
|
+
alert_owner=True,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
async def restart_scheduled(data):
|
|
76
|
+
emoji = res(':clock_white:')
|
|
77
|
+
await send_notification(
|
|
78
|
+
f'{emoji} Restart scheduled',
|
|
79
|
+
{'Server': data.get('server', settings.server_name), 'Scheduled time': data.get('time', 'Unknown time')},
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
async def validation_failed(data):
|
|
83
|
+
emoji = res(':shield_alert:')
|
|
84
|
+
await send_notification(
|
|
85
|
+
f'{emoji} Validation failed',
|
|
86
|
+
{'Server': data.get('server', settings.server_name), 'Status': 'Panel or server is unreachable.'},
|
|
87
|
+
alert_owner=True,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
async def unexpected_error(data):
|
|
91
|
+
emoji = res(':circle_alert:')
|
|
92
|
+
await send_notification(
|
|
93
|
+
f'{emoji} Unexpected error',
|
|
94
|
+
{
|
|
95
|
+
'Server': data.get('server', settings.server_name),
|
|
96
|
+
'Error': truncate(data.get('error', 'No traceback provided'), 1800),
|
|
97
|
+
},
|
|
98
|
+
alert_owner=True,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
async def sampling_failed(data):
|
|
102
|
+
emoji = res(':stats_blue:')
|
|
103
|
+
await send_notification(
|
|
104
|
+
f'{emoji} Sampling error',
|
|
105
|
+
{'Server': data.get('server', settings.server_name), 'Error': 'The API returned invalid values.'},
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
async def unknown_event(data):
|
|
109
|
+
emoji = res(':circle_question_red:')
|
|
110
|
+
await send_notification(
|
|
111
|
+
f'{emoji} Unknown event',
|
|
112
|
+
{'Event': data.get('event', 'unknown'), 'Details': 'This event type is unrecognized.'},
|
|
113
|
+
alert_owner=True,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
server.addEndpoint('/webhook', endpoint_handler)
|
|
117
|
+
server.addHandler('restart_action_sent', restart_action_sent)
|
|
118
|
+
server.addHandler('server_back_online', server_back_online)
|
|
119
|
+
server.addHandler('server_failed_restart', server_failed_restart)
|
|
120
|
+
server.addHandler('restart_scheduled', restart_scheduled)
|
|
121
|
+
server.addHandler('validation_fail', validation_failed)
|
|
122
|
+
server.addHandler('unexpected_error', unexpected_error)
|
|
123
|
+
server.addHandler('sampling_fail', sampling_failed)
|
|
124
|
+
server.addHandler('', unknown_event)
|
|
125
|
+
server.addHandler('unknown', unknown_event)
|
|
126
|
+
return server
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hungernetbot
|
|
3
|
+
Version: 1.0.dev1
|
|
4
|
+
Summary: HungerNet's official Discord bot.
|
|
5
|
+
Author: iFamished
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HungerNet/Bot
|
|
8
|
+
Project-URL: Documentation, https://github.com/HungerNet/Bot
|
|
9
|
+
Project-URL: Source, https://github.com/HungerNet/Bot
|
|
10
|
+
Project-URL: Issues, https://github.com/HungerNet/Bot/issues
|
|
11
|
+
Keywords: minecraft,automation,server-management,hungernet,discord
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Development Status :: 4 - Beta
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
21
|
+
Classifier: Topic :: System :: Systems Administration
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: discord.py<3,>=2.4
|
|
26
|
+
Requires-Dist: hungercore>=1.0.dev4
|
|
27
|
+
Requires-Dist: calagoapi
|
|
28
|
+
Requires-Dist: calagoapi-minecraft
|
|
29
|
+
Requires-Dist: mapres
|
|
30
|
+
Requires-Dist: aiosqlite
|
|
31
|
+
Requires-Dist: fastapi
|
|
32
|
+
Requires-Dist: uvicorn
|
|
33
|
+
Requires-Dist: PyYAML
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: pytest; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# HungerNet Bot
|
|
39
|
+
HungerNet's official Discord bot.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
python -m pip install hungernetbot
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Run the bot with `hungernetbot` or `python -m hungernetbot` after configuring the environment.
|
|
48
|
+
|
|
49
|
+
For container deployment, see the [Calagopus egg guide](docs/deployment/calagopus.md).
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
The bot reads configuration from environment variables. Secrets are never stored in the package.
|
|
54
|
+
|
|
55
|
+
| Variable | Required | Purpose |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| `DISCORD_TOKEN` | Yes | Discord bot token. |
|
|
58
|
+
| `PANEL_URL` | Yes | Calagopus panel base URL. |
|
|
59
|
+
| `PANEL_API_KEY` | Yes | Calagopus API key. |
|
|
60
|
+
| `SERVER_ID` | Yes | Minecraft server UUID or short ID. |
|
|
61
|
+
| `SERVER_STAFF_ROLE_ID` | Yes | Role allowed to use server admin commands. |
|
|
62
|
+
| `NOTIFICATION_CHANNEL_ID` | Yes | Ready and HungerBridge event notifications. |
|
|
63
|
+
| `OWNER_ID` | Yes | Owner account for critical alerts and server access. |
|
|
64
|
+
| `BRIDGE_URL` | Yes | HungerBridge endpoint for the Minecraft server. |
|
|
65
|
+
| `BRIDGE_TOKEN_ID` | Yes | HungerBridge token ID. |
|
|
66
|
+
| `BRIDGE_TOKEN_SECRET` | Yes | HungerBridge token secret. |
|
|
67
|
+
| `BRIDGE_WEBHOOK_TOKEN` | Yes | Shared token for HungerCore's webhook receiver. |
|
|
68
|
+
| `GITHUB_WEBHOOK_SECRET` | Yes | GitHub webhook signing secret. |
|
|
69
|
+
| `SERVER_NAME` | No | Display name; defaults to `Hunger SMP`. |
|
|
70
|
+
| `SERVER_DOMAIN` | No | Public server address; defaults to `localhost`. |
|
|
71
|
+
| `MINECRAFT_SERVER_PORT` | No | Public Minecraft server port; defaults to `25565`. |
|
|
72
|
+
| `WEBHOOK_HOST` | No | Bind address for both webhook servers; defaults to `0.0.0.0`. |
|
|
73
|
+
| `BRIDGE_WEBHOOK_PORT` | No | HungerBridge webhook port; defaults to `8081`. |
|
|
74
|
+
| `GITHUB_WEBHOOK_PORT` | No | GitHub webhook port; defaults to the container's `SERVER_PORT`, or `8000` outside Calagopus. |
|
|
75
|
+
| `DATABASE_PATH` | No | New SQLite database path; defaults to `./data/hungernetbot.sqlite3`. |
|
|
76
|
+
| `POSTS_DIRECTORY` | No | Named post directory; defaults to `./posts`. |
|
|
77
|
+
| `DEV_GUILD_IDS` | No | Comma-separated guild IDs for immediate slash command sync. |
|
|
78
|
+
|
|
79
|
+
Set these variables in the process environment or your deployment platform. Rotate credentials that were present in the legacy source before deploying; removing source files does not remove values from Git history.
|
|
80
|
+
|
|
81
|
+
For Calagopus, allocate the primary `SERVER_PORT` for GitHub webhooks and a second port for the HungerBridge webhook. Set `BRIDGE_WEBHOOK_PORT` to the second allocation. The panel must expose both ports publicly or through your reverse proxy.
|
|
82
|
+
|
|
83
|
+
## Commands
|
|
84
|
+
|
|
85
|
+
- `/server status` and `/server players` report server state.
|
|
86
|
+
- `/server admin run`, `/server admin power`, and `/server admin statistics` require the configured owner or staff role.
|
|
87
|
+
- `/github link`, `/github unlink`, `/github list`, and `/github route` manage repository destinations. These require Manage Server permission.
|
|
88
|
+
- `/post <name>` publishes a configured `.md` or `.yaml` file to the channel where it is invoked. This requires Manage Server permission.
|
|
89
|
+
- `/ping` and `/help` provide bot status and command guidance.
|
|
90
|
+
|
|
91
|
+
The GitHub receiver listens at `/github/webhook`. Configure that URL and `GITHUB_WEBHOOK_SECRET` in each GitHub repository's webhook settings. The HungerBridge receiver listens at `/webhook` and checks the `X-Webhook-Token` header.
|
|
92
|
+
|
|
93
|
+
## Post Files
|
|
94
|
+
|
|
95
|
+
The configured posts directory is created on startup. `/post update` resolves `update.md` or `update.yaml`; specify an extension when both exist. Every invocation posts again. The bot does not keep a posting history or suppress duplicates.
|
|
96
|
+
|
|
97
|
+
Use `.md` for regular Discord Markdown message content. Use `.yaml` for structured messages with content, embeds, polls, attachments, mention rules, supported message flags, and URL buttons. YAML is declarative; it cannot run code or define custom button callbacks. User mentions must use Discord mention syntax or IDs, not bare usernames.
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
content: 'A new community update is available.'
|
|
101
|
+
embeds:
|
|
102
|
+
- title: 'What changed'
|
|
103
|
+
description: 'Read the [update notes](https://example.com/updates).'
|
|
104
|
+
color: '#C82828'
|
|
105
|
+
fields:
|
|
106
|
+
- name: 'Highlights'
|
|
107
|
+
value: 'New maps and server improvements'
|
|
108
|
+
inline: false
|
|
109
|
+
buttons:
|
|
110
|
+
- label: 'Read more'
|
|
111
|
+
url: 'https://example.com/updates'
|
|
112
|
+
allowed_mentions:
|
|
113
|
+
everyone: false
|
|
114
|
+
users: true
|
|
115
|
+
roles: true
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Attachments are relative to the posts directory. Embed colors accept an integer or hex string. Polls accept a question, 2–10 answers, a duration from 1–32 hours, and an optional `multiple` boolean. `tts`, `silent`, and `suppress_embeds` control supported Discord message flags.
|
|
119
|
+
|
|
120
|
+
## Development
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
python -m pip install -e '.[dev]'
|
|
124
|
+
pytest
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The bot starts with a fresh SQLite database; it does not import data from previous versions.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
hungernetbot/__init__.py,sha256=pSfXcfE8qW-DWbKOEauVvPBtwSVdidfd8yCEtQMO4SI,354
|
|
2
|
+
hungernetbot/__main__.py,sha256=cDyXdrKoz0pcsyJ16UmdFnmLzTd8sqy8lfAlYi9h1r0,34
|
|
3
|
+
hungernetbot/bot.py,sha256=oyTyG8kwkojgphwF4Ie6gewshKPh7I9DsHDmjlpqCq8,4813
|
|
4
|
+
hungernetbot/config.py,sha256=gbT9pPbZ3Y5WdTrD04f4y4Gs3vYWuIptxcziq4ZnaVg,3218
|
|
5
|
+
hungernetbot/database.py,sha256=mLM0Wzv6AYAm7wBkZ6Ecm8TqkAH3gnua5OOEr_mxuj4,5612
|
|
6
|
+
hungernetbot/embeds.py,sha256=bHPFAwZ8C1rfLBfcF0K3fphXxWfmehfrjZtzwzO-ivs,900
|
|
7
|
+
hungernetbot/emojis.py,sha256=vcXivJEOxeDkCDczha0cibBgktmsgoA5KxABwlvzmUw,9209
|
|
8
|
+
hungernetbot/posts.py,sha256=tBj-kzH29oIMmyRk7FnYZdFT1-qGeuYI5NPffJTEkpI,8957
|
|
9
|
+
hungernetbot/runner.py,sha256=kywGFK-Iuzl_uqxYpzYZ_8n_jMPp7LKXOZID5tF4cdo,388
|
|
10
|
+
hungernetbot/webhooks.py,sha256=5z1M4ElxwG6_q2b7H9ls5-wCRyilp9xhGgFn4HwSi8w,4743
|
|
11
|
+
hungernetbot/commands/__init__.py,sha256=9AiNZxF_T-Czn4inQ3Nh0cE531QjhOh261R8ag72fZk,342
|
|
12
|
+
hungernetbot/commands/general.py,sha256=O3ikTMnd7Uxjeilm3i57ej_kAw3YDMnnGP1j0ComVlo,1638
|
|
13
|
+
hungernetbot/commands/github.py,sha256=NZK3kGgmC4Hy5xmazC-ulVTarEfrQHUNHzuAIC3foQ4,5393
|
|
14
|
+
hungernetbot/commands/posts.py,sha256=M9B0Z11cZFGT3CmzaaMEf2Xgf6JO7Fk0PBVnGci67rU,3075
|
|
15
|
+
hungernetbot/commands/server.py,sha256=ALqKt-qi75P_vgetZMC179sADCQgqzuHUAOZ2m2e4r8,9635
|
|
16
|
+
hungernetbot/github/__init__.py,sha256=N3XUy-OTbyw9ju1B-i-Or5a1vU5gbBnP9g-oGDdN0pQ,71
|
|
17
|
+
hungernetbot/github/dispatcher.py,sha256=ttGj38jkOqSjrjLzBNy3fnom9rQfv8O6SzEmG_BgM70,2830
|
|
18
|
+
hungernetbot/github/events.py,sha256=Oul_ckz9Yuw3zx04NRPbCerjSnaMX6swi7PfFZPF-tI,9587
|
|
19
|
+
hungernetbot/github/repo.py,sha256=giGMScOEeafUzK9ifovKE7I-knaomvZqflfIi1gAjVo,536
|
|
20
|
+
hungernetbot/github/security.py,sha256=E4OBS1BrdThacW1I74Zcoo0ZwFYmjQamR7_7FrCbQUY,347
|
|
21
|
+
hungernetbot/github/server.py,sha256=ZxoqRTUq9ISUrw7xDu-xGDlJB9f2lYJvg0lZnY2afSA,2105
|
|
22
|
+
hungernetbot-1.0.dev1.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
23
|
+
hungernetbot-1.0.dev1.dist-info/METADATA,sha256=wie5dpAt-37vBTOUW8Wg5ZLh9NZoKGzJO46t1kBN8mU,6075
|
|
24
|
+
hungernetbot-1.0.dev1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
25
|
+
hungernetbot-1.0.dev1.dist-info/entry_points.txt,sha256=yE8jKEWrUlS1_b7vAcX7J5AOr5CJHWevgRdGtcAw1mA,58
|
|
26
|
+
hungernetbot-1.0.dev1.dist-info/top_level.txt,sha256=txZ6ElBl4BL_uzvObSszxL7kRI9wxgmHHaTW2Ic565s,13
|
|
27
|
+
hungernetbot-1.0.dev1.dist-info/RECORD,,
|