
# discord-media-server

A self-hosted Discord bot that scans your local media folder for movie files, displays recently added movies and lets you browse/search titles from Discord. Built with Node.js + SQLite/MySQL.

---

## Features

- Scans your movie folder for `.mp4`, `.mkv`, `.avi`
- Fetches metadata from [TMDb](https://www.themoviedb.org/)
- Stores info in SQLite or MySQL
- Search and browse directly from Discord
- Auto-download movie posters (optional)
- Update and re-scan your library anytime
- Easy CLI setup and config
- Docker container installation

---

## To-Do Future implementation

- Show, music and game support

---

## Installation

> Requires [Node.js 18+](https://nodejs.org/) and an existing Discord bot token & TMDb API key (optional).

### 1. Install globally via NPM:

```bash
npm install -g discord-media-server
```

### 2. Run initialization:

```bash
discord-media-server init
```
or (short hand)
```bash
dms [init|setup|start|scan|reset]
```

You’ll be prompted for:
- Media folder path (e.g., `D:\Movies`)
- Database type (SQLite or MySQL)
- Discord bot token
- TMDb API key
- Scan media and start server


## - OR SEPARATELY -


### 2. Setup the server (not needed if started init):

```bash
discord-media-server setup
```

This will:
- Start the setup process
- Get bot tokens, TMDb key, media directory and create database

### 3. Scan the media (not needed if started in init):

```bash
discord-media-server scan
```

This will:
- Scans media and inserts into database
- Creates a cache file with scanned data

### 4. Start the server (not needed if started in init):

```bash
discord-media-server start
```

This will:
- Start the Discord bot
- Launch a local Express server (http://localhost:8080)
- Enable commands in your Discord server

### 5. Reset database:

```bash
discord-media-server reset
```

This will:
- Clears database and deletes cache file

---

## 🧰 Unraid Installation

You can run this project via Docker or directly install it using **Unraid's Community Applications**.

### 🐳 Install via Custom Template

1. Open Unraid → **Docker** → **Add Container**
2. Under **Template Repositories**, click **Add Template Repository**
3. Paste this GitHub URL: https://github.com/Lukedeez/discord-media-server
4. Now search for `discord-media-server` under the **Apps** tab.

### ⚙️ Docker Paths

| Variable     | Description            | Example                                  |
|--------------|------------------------|------------------------------------------|
| `/media`     | Your movie/media folder| `/mnt/user/Media/Videos/Movies`          |
| `/config`    | App settings & cache   | `/mnt/user/appdata/discord-media-server`|

---

If you prefer `docker run` or `docker-compose` follow ***Docker Quick Start*** below.

---

## 🐳 Docker Quick Start
### 1. Create `.env` in the root folder:
```env
Copy
Edit
DISCORD_TOKEN=your-bot-token-here
MEDIA_DIR=/media
```
### 2. Run Docker:
```bash
Copy
Edit
docker build -t discord-media-server .
docker run -d \
  --name dms \
  -p 8080:8080 \
  -v /path/to/your/media:/media \
  --env-file .env \
  discord-media-server
Replace /path/to/your/media with the actual path to your movie folder (e.g., /mnt/user/Media/Videos/Movies).
```
### 3. Visit http://localhost:8080 to setup, view your media and start the bot.


## 📌 Notes
- Inside the container, your media is always referenced as /media.

- For persistent setup values, setupConfig.js uses MEDIA_DIR from .env, defaulting to /media in Docker.

- Make sure your media files are readable by Docker and your bot token is valid.- 


---

## Usage

### In Discord:

- `.scan` — Scan media folder and update the database (only used by admin)


### Single Movie Command: `.m` or `.movie`
- Args (can be in any order):
-	year: `-y [year]`
- `.m [searchTitle]` — Search movies by title returns one movie
- `.m [searchTitle] -y [year]` — Search more specific using the year and title


### Multiple Movie Command: `.ml` or `.movie-list`
- Args (can be in any order):
-	year: `-y [year]`
-	limit: `-[limit]` or `-l [limit]`
- `.ml [searchTerm]` — Search movies by title returns a list
- `.ml [searchTerm] -y [year] -[limit]` — Search movies by title with year and limit of how many return

---

## Example Commands

```
.scan

.m The Matrix
.m Star Wars -y 1977

.ml The Matrix
.ml Matrix -10

```

---

## Database Info

By default, a local SQLite file will be used (stored in `/data/media.sqlite`). 
You can also choose MySQL during setup.
Movie data is cached in: `/data/media.json`.

---

## TMDb + Discord Setup

- Get a free TMDb API key: https://www.themoviedb.org/settings/api
- Create a Discord bot: https://discord.com/developers/applications
- Add bot to your server:  
  ```
  https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&scope=bot&permissions=274877975552
  ```

---

## Manual Commands

You can also run:

```bash
discord-media-server init     # Runs setup, scan and start
discord-media-server setup    # Sets up database and APIs
discord-media-server scan     # Scans media directory and updates the database
discord-media-server start    # Starts server and discord bot
discord-media-server reset    # Clears the database and cache file
```

---

## File Structure

```
bin/          # CLI entry points (setup, start, scan)
lib/          # Core logic (bot, scanner, database)
data/         # SQLite DB and cache file
public/       # Web UI Pages
.env          # API keys and tokens

```

---

## Credits

Created by [@Lukedeez](https://github.com/Lukedeez)  

---

## Disclaimer

This is a personal self-hosted project. TMDb API usage is subject to their [terms of service](https://www.themoviedb.org/documentation/api/terms-of-use).
