Skip to content

Commit

Permalink
Add Autobahn autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 25, 2023
1 parent 5f83b46 commit 129d3d1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"key-spacing": 2,
"keyword-spacing": 2,
"max-statements-per-line": 2,
"max-len": [2, { "code": 280 }],
"max-len": [2, { "code": 250 }],
"new-parens": 2,
"no-array-constructor": 2,
"no-compare-neg-zero": 2,
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# Bundbot
Discord-Bot, welcher die [bund.dev-API](https://bund.dev/) nutzt, um mehr (oder weniger) hilfreiche Informationen anzuzeigen

## Autobahn
## Setup

`config.json`:
```json
{
"token": "<Discord-Bot-Token>"
}
```

## Features

### Autobahn
- Alle Autobahnen auflisten (`-list`)
- Webcams einer Autobahn auflisten (`-listwebcams <Autobahn>`)
- Webcams einer Autobahn auflisten (`-listwarnings <Autobahn>`)
Expand All @@ -10,10 +21,10 @@ Discord-Bot, welcher die [bund.dev-API](https://bund.dev/) nutzt, um mehr (oder
- Rastplätze einer Autobahn auflisten (`-listrastplätze <Autobahn>`)
- Elektrische Ladestationen einer Autobahn auflisten (`-listladestationen <Autobahn>`)

## NINA
### NINA
- Katwarn-Meldungen anzeigen (`-katwarn`)
- Biwapp-Meldungen anzeigen (`-biwapp`)
- Mowas-Meldungen anzeigen (`-mowas`)

## Lebensmittel- und Produktwarnungen
### Lebensmittel- und Produktwarnungen
- Lebensmittel- und Produktwarnungen anzeigen (`-produktwarn`)
35 changes: 24 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ function migrateSlashOptions(options) {
return options
}

let roads = []
bot.on("ready", async () => {
bot.user.setPresence({activities: [{name: "Custom Status", state: "/help", type: Discord.ActivityType.Custom}]})
console.log("Bot wurde gestartet .-.")
bot.user.setPresence({activities: [{name: "Custom Status", state: "bund.dev-Bot - Slashcommands", type: Discord.ActivityType.Custom}]})
console.log("Bot wurde als " + bot.user.tag + " gestartet!")

const res = await fetch("https://verkehr.autobahn.de/o/autobahn", {
headers: {
Accept: "application/json"
}
})
const json = await res.json()
roads = json.roads

const commands = [
{
Expand Down Expand Up @@ -60,6 +69,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
},{
Expand All @@ -70,6 +80,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
},{
Expand All @@ -80,6 +91,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
},{
Expand All @@ -90,6 +102,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
},{
Expand All @@ -100,6 +113,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
},{
Expand All @@ -110,6 +124,7 @@ bot.on("ready", async () => {
name: "id",
type: "STRING",
description: "Der Name der Autobahn",
autocomplete: true,
required: true
}]
}]
Expand Down Expand Up @@ -137,6 +152,11 @@ const checkAutobahn = str => /A\d{1,3}/g.test(str)
bot.on("interactionCreate", async interaction => {
if (interaction.user.bot || interaction.channel.type == Discord.ChannelType.DM) return

if (interaction.type == Discord.InteractionType.ApplicationCommandAutocomplete) {
const input = interaction.options.getFocused(true)
return interaction.respond(roads.filter(road => road.toLowerCase().includes(input.value)).map(road => ({name: road, value: road})).slice(0, 25))
}

const reply = data => {
if (typeof data == "string" && data.length > 2000) data = data.substring(0, 1997) + "..."
interaction.reply(data)
Expand All @@ -145,14 +165,7 @@ bot.on("interactionCreate", async interaction => {
if (interaction.commandName == "autobahn") {
const args = [interaction.options.getSubcommand(), interaction.options.getString("id", false)]
if (args[0] == "list") {
const res = await fetch("https://verkehr.autobahn.de/o/autobahn", {
headers: {
Accept: "application/json"
}
})
const json = await res.json()

interaction.reply("Liste aller Autobahnen in Deutschland:\n\n" + json.roads.join(" "))
interaction.reply("Liste aller Autobahnen in Deutschland:\n\n" + roads.join(" "))
} else if (args[0] == "webcams") {
if (!checkAutobahn(args.join(" "))) return interaction.reply("Du musst eine gültige Autobahn angeben!")
const res = await fetch("https://verkehr.autobahn.de/o/autobahn/" + args[1] + "/services/webcam", {
Expand Down Expand Up @@ -220,7 +233,7 @@ bot.on("interactionCreate", async interaction => {

const rastplatze = []
json.parking_lorry.forEach(rastplatz => {
rastplatze.push(rastplatz.title + ": " + rastplatz.description.join(", ").trim())
rastplatze.push(rastplatz.title + ": " + rastplatz.description.map(desc => desc.trim()).join(", "))
})
reply("Liste aller Rastplätze der **" + args[1] + "**:\n\n" + rastplatze.join("\n"))
} else if (args[0] == "ladestationen") {
Expand Down
90 changes: 51 additions & 39 deletions package-lock.json

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

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
"type": "git",
"url": "git+https://github.com/DEVTomatoCake/Bundbot.git"
},
"bugs": {
"url": "https://github.com/DEVTomatoCake/Bundbot/issues"
},
"homepage": "https://github.com/DEVTomatoCake/Bundbot#readme",
"dependencies": {
"discord.js": "^14.14.1"
Expand Down

0 comments on commit 129d3d1

Please sign in to comment.