Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added event for auto-reply to dms #17

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions .idea/runConfigurations/Docs___Build_locally.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions docs/user/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ As described on the landing page, the `/opt/disco/` directory should be a volume
so that you can manage the configuration files on the host system. This also makes it so that logs and databases are
persistent.

## Secrets
## Secrets (required)
Location of the file: `/opt/disco/run/secrets.toml`

### Secrets schema documentation
Expand All @@ -21,7 +21,21 @@ language: toml
---
```

## Podcasts
## Bot (optional)
Location of the file: `/opt/disco/run/bot.toml`

### Bot schema documentation
```{include} ../../res/schemas/bot.v1.md
```

### Bot example
```{literalinclude} ../../res/schemas/bot.v1.example.toml
---
language: toml
---
```

## Podcasts (optional)
Location of the file: `/opt/disco/run/podcasts.toml`

### Podcasts schema documentation
Expand Down
65 changes: 0 additions & 65 deletions res/misc/example_autoresponder.py

This file was deleted.

5 changes: 5 additions & 0 deletions res/schemas/bot.v1.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"$schema" = "https://raw.githubusercontent.com/FlotterCodername/disco/refs/heads/main/res/schemas/bot.v1.schema.json"

[no-reply]
enabled = true
message = "⚠️ I am a bot. My inbox is not monitored."
14 changes: 14 additions & 0 deletions res/schemas/bot.v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This configuration stores everything related to the bot itself.

### Bot top level
| Property | Type | Required | Description |
|----------|:----:|:--------:|-------------|
| $schema | string (uri) | | Which JSONSchema the file follows. |
| no-reply | object | | Configuration for the 'no-reply' feature. |

#### `no-reply` (Optional)

| Property | Type | Required | Description |
|----------|:----:|:--------:|-------------|
| enabled | boolean | Yes | Whether the 'no-reply' feature is enabled. |
| message | string | | The message to send back when the bot receives a message. If empty or not set, a default message in English will be sent. |
33 changes: 33 additions & 0 deletions res/schemas/bot.v1.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$id": "https://raw.githubusercontent.com/FlotterCodername/disco/refs/heads/main/res/schemas/bot.v1.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": false,
"description": "This configuration stores everything related to the bot itself.",
"properties": {
"$schema": {
"description": "Which JSONSchema the file follows.",
"format": "uri",
"type": "string"
},
"no-reply": {
"additionalProperties": false,
"description": "Configuration for the 'no-reply' feature.",
"properties": {
"enabled": {
"description": "Whether the 'no-reply' feature is enabled.",
"type": "boolean"
},
"message": {
"description": "The message to send back when the bot receives a message. If empty or not set, a default message in English will be sent.",
"type": "string"
}
},
"required": [
"enabled"
],
"type": "object"
}
},
"title": "Bot",
"type": "object"
}
18 changes: 18 additions & 0 deletions src/disco/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Discord bot setup
intents = discord.Intents.default()
intents.message_content = True
intents.dm_messages = True
bot = commands.Bot(command_prefix="!", intents=intents)


Expand Down Expand Up @@ -102,6 +103,23 @@ async def on_ready() -> None: # noqa RUF029
synchronize_podcasts.start()


@bot.event
async def on_message(message) -> None:
"""
Auto response in case of retrieving a DM.

:param message: The message that was sent to the bot by a user
"""
# Check if the message is a DM and that it is not from the bot itself
if message.guild is None and message.author != bot.user:
# Send an automatic reply
await message.channel.send(
"Ich bin ein Bot. Meine Inbox wird daher nicht überwacht. Mehr Antworten bekommst du von mir nicht."
)

await bot.process_commands(message)


def main() -> int:
"""
Entry point for the Discord bot.
Expand Down
5 changes: 3 additions & 2 deletions src/disco/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import tomli_w

from disco.helpers.atomicwrites import atomic_write
from disco.paths import PODCASTS_TOML, SECRETS_TOML
from disco.schemas import podcasts, secrets
from disco.paths import BOT_TOML, PODCASTS_TOML, SECRETS_TOML
from disco.schemas import bot, podcasts, secrets

if TYPE_CHECKING:
# noinspection PyProtectedMember
Expand Down Expand Up @@ -48,6 +48,7 @@ def content(self) -> dict:
class Configuration:
"""All configurations for the application."""

bot = _DcConfiguration(BOT_TOML, bot)
podcasts = _DcConfiguration(PODCASTS_TOML, podcasts)
secrets = _DcConfiguration(SECRETS_TOML, secrets)

Expand Down
1 change: 1 addition & 0 deletions src/disco/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

DISCO_RUN = DISCO_HOME / "run"
DISCO_RUN.mkdir(parents=True, exist_ok=True)
BOT_TOML = DISCO_RUN / "bot.toml"
PODCASTS_TOML = DISCO_RUN / "podcasts.toml"
SECRETS_TOML = DISCO_RUN / "secrets.toml"

Expand Down
42 changes: 39 additions & 3 deletions src/disco/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from disco.paths import RES_SCHEMAS
from disco.schemas.docgen import JsonSchemaMarkdownGenerator

__all__ = ["podcasts", "secrets"]
__all__ = ["bot", "podcasts", "secrets"]

_BASE_URI = "https://raw.githubusercontent.com/FlotterCodername/disco/refs/heads/main/res/schemas"
_DOC_GENERATOR = JsonSchemaMarkdownGenerator(indent_size=2)
Expand Down Expand Up @@ -69,6 +69,42 @@ def get_path(self, suffix: Literal["md", "schema.json", "example.toml"]) -> path
return RES_SCHEMAS / f"{self.name}.v{self.version}.{suffix}"


bot = _DcSchema(
"bot",
1,
{
"additionalProperties": false,
"description": "This configuration stores everything related to the bot itself.",
"properties": {
"$schema": {"description": "Which JSONSchema the file follows.", "format": "uri", "type": "string"},
"no-reply": {
"additionalProperties": false,
"description": "Configuration for the 'no-reply' feature.",
"properties": {
"enabled": {"description": "Whether the 'no-reply' feature is enabled.", "type": "boolean"},
"message": {
"description": (
"The message to send back when the bot receives a message. If empty or not set, a "
"default message in English will be sent."
),
"type": "string",
},
},
"required": ["enabled"],
"type": "object",
},
},
"title": "Bot",
"type": "object",
},
{
"no-reply": {
"enabled": true,
"message": "⚠️ I am a bot. My inbox is not monitored.",
}
},
)

podcasts = _DcSchema(
"podcasts",
1,
Expand Down Expand Up @@ -181,11 +217,11 @@ def _dump() -> int:
target.write_text(new_text)
# TOML example
target = schema.get_path("example.toml")
old_text = target.read_text() if target.is_file() else ""
old_text = target.read_text("utf-8") if target.is_file() else ""
new_text = tomli_w.dumps(schema.example, multiline_strings=True)
if old_text != new_text:
changed = 1
target.write_text(new_text)
target.write_text(new_text, "utf-8")
# Markdown
target = schema.get_path("md")
old_text = target.read_text("utf-8") if target.is_file() else ""
Expand Down
3 changes: 1 addition & 2 deletions src/disco/schemas/docgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def generate_markdown(self, schema: dict[str, Any]) -> str:
Generate Markdown documentation from a JSONSchema.

:param schema: The JSONSchema dictionary
Returns:
str: Generated Markdown documentation
:return: Generated Markdown documentation
"""
sections: list[str] = []
if schema.get("description"):
Expand Down