Skip to content

Commit

Permalink
Merge pull request #76 from r-Norge/dev
Browse files Browse the repository at this point in the history
Update bot with lots of new features.
  • Loading branch information
Ev-1 authored Feb 12, 2023
2 parents 29cbedf + 768bc49 commit c9973ec
Show file tree
Hide file tree
Showing 49 changed files with 2,215 additions and 1,594 deletions.
Binary file added .github/assets/mixqueue.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/queue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 21 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ name: Test And Build BOT
on: [push, pull_request]

jobs:
flake8:
lint:
runs-on: ubuntu-latest
env:
CheckFiles: "bot.py musicbot/"
steps:
- uses: actions/checkout@v2

- name: Set up Python 3.8
uses: actions/setup-python@v1
- name: Set up Python 3.10
uses: actions/setup-python@v4.2.0
with:
python-version: 3.8
python-version: 3.10.7

- name: Lint with flake8
run: |
Expand All @@ -24,23 +24,27 @@ jobs:
pip install isort
isort --check --sp setup.cfg ${CheckFiles}
- name: Sort with Isort
if: failure()
run: |
isort --sp setup.cfg ${CheckFiles}
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Create Pull Request
if: failure()
uses: peter-evans/create-pull-request@v2
- name: Set up Python 3.10
uses: actions/[email protected]
with:
token: ${{ secrets.GH_TOKEN }}
branch: actions/auto
commit-message: Changes done by tests
committer: Roxedus <[email protected]>
title: Linter changed files
python-version: 3.10.7

- name: Install dependencies
run: |
pip install pytest
pip install -r requirements.txt
- name: Run pytest
run: |
pytest
build-and-push:
needs: flake8
needs: [lint, test]
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' && contains(github.ref, 'actions') != 'true'
env:
Expand All @@ -60,17 +64,6 @@ jobs:
echo "::set-output name=username::$(echo ${_username} | cut -d/ -f1 | tr '[:upper:]' '[:lower:]')"
echo "::set-output name=repo::$(echo ${_repo} | tr '[:upper:]' '[:lower:]')"
- name: Bump TAG
if: github.ref == 'refs/heads/master'
id: tagger
run: |
version=$(grep -i bot_version musicbot/utils/bot_version.py | cut -d" " -f3 | tr -d \")
a=(${version//./ })
((a[2]++))
new_version=$(echo "${a[0]}.${a[1]}.${a[2]}")
sed -i "s/${version}/${new_version}/g" musicbot/utils/bot_version.py
echo "::set-output name=tag::${new_version}"
- name: Push changed files, and tags
if: github.ref == 'refs/heads/master'
uses: EndBug/add-and-commit@v4
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM amd64/python:3.8.3-alpine
FROM python:3.10.7-alpine

LABEL maintainer="Roxedus"

Expand Down
109 changes: 83 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,113 @@
# WIP - Shite Music Bot
# Shite Music Bot

[![Build Status](https://jenkins.roxedus.net/buildStatus/icon?job=MusicBot%2Fdev)](https://jenkins.roxedus.net/job/MusicBot/job/dev/)
[![MicroBadger](https://images.microbadger.com/badges/image/rnorge/music:dev-amd64.svg)](https://microbadger.com/images/rnorge/music:dev-amd64 "Get your own image badge on microbadger.com")
A discord music bot you can host yourself!

## Features

- Translations, get the bot in your preferred langauge! Works for both commands and replies.
- Round Robin style queue, you won't have to wait forever for your songs to play.
- Interactive menus
- Translations, get the bot in your preferred language! Works for both commands and replies.
- Noice per server customization options, including per server localization.
- Toit embeds.
- DJ roles. Grants members in the role some more control over the queue.
- Sound-manipulation features, bass boost and nightcore.
- Lyrics command that sometimes gives the correct lyrics
- DJ roles

### Setup
### Fair queue behavior

Each user has their own queue and the bot mixes them together for fair time sharing.

![Round robin queue](.github/assets/mixqueue.gif)

### Interactive menus

![a](.github/assets/queue.png)

![b](.github/assets/search.png)

## Requirements

- Python 3.10 or greater
- A [lavalink](https://github.com/Freyacodes/Lavalink) server

## Setup

1. Change or copy data/config.yaml.example to config.yaml
2. Create or find a [lavalink](https://github.com/Frederikam/Lavalink) server you can use.
3. Add your bot token and lavalink server to the bot settings in your config.yaml.
4. Edit any other settings you want.
5. (Optional but recommended) Create a virtual environment and activate it.
6. install requirements `python -m pip install -r requirements.txt`
7. run the bot `python bot.py` :)
- If using docker, the config file will be moved into `/data` on the first run.
2. Configure Lavalink
- When self-hosting:
- Get a `application.yml` file from your lavalink distribution, to provide lavalink.
- Secure the instance by changing the password.
- This bot is written with the Youtube, Soundcloud, Bandcamp and Vimeo source in mind. Others might work
- Self-hosted and hosted:
- Configure the bot to connect to your nodes.
- If you wish to pre-seed the bot with lavalink nodes, you can do so in `data/config.yml`

```yml
lavalink nodes:
- host: localhost
port: 2333
password: youshallnotpass
region: eu
name: main
```
3. Add your bot token to `config.yaml`.
4. Prepare your environment:
- Native install:
- Clone, and enter the repository.
- `git clone https://github.com/r-Norge/ShiteMusicBot.git && cd ShiteMusicBot`
- (Recommended) Create a virtual environment and activate it.
- `python3 -m pip venv .venv && . .venv/bin/activate`
- Install requirements `python3 -m pip install -r requirements.txt`
- Docker Compose:
- Copy the example that fits your needs
- Change the volume path(s) to store persistent data on your host.
- `docker compose pull`
5. Run the bot:
- Native install:
- `python3 bot.py`
- Docker Compose:
- `docker compose run -d`
6. Make sure the bot responds, by invoking the info command. `@ShiteMusicBot info`, substitute `ShiteMusicBot` with the name of your bot.

### Docker
### Docker Compose Example

### Compose Example
#### Self-hosted lavalink

````yaml
```yaml
version: '3'
networks:
internal:
driver: bridge
services:
backbone:
lavalink:
hostname: lavalink
image: fredboat/lavalink:dev
ports:
- 2333:2333
networks:
internal:
aliases:
- lavalink
- internal
volumes:
- /compose/BottStack/lavalink/application.yml:/opt/Lavalink/application.yml
- ./application.yml:/opt/Lavalink/application.yml
bot:
container_name: ProdMaster
container_name: ShiteMusicBot
image: rnorge/music
networks:
- internal
command: python3 bot.py
volumes:
- /compose/BottStack/ProdBetaMaster/script:/diks
- /compose/BottStack/ProdBetaMaster/data:/app/data
````
- ./data:/app/data
```

#### Hosted lavalink

```yaml
version: '3'
services:
bot:
container_name: ShiteMusicBot
image: rnorge/music
command: python3 bot.py
volumes:
- ./data:/app/data
```
37 changes: 18 additions & 19 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Discord Packages
import discord
import lavalink
from discord.ext import commands
from discord.flags import MemberCacheFlags

Expand All @@ -8,26 +9,25 @@
import time
import traceback
from argparse import ArgumentParser, RawTextHelpFormatter
from typing import Optional

import aiohttp
import yaml

# Bot Utilities
from musicbot.utils.localisation import Aliaser, LocalizedContext, Localizer, LocalizerWrapper
from musicbot.utils.logger import BotLogger
from musicbot.utils.settingsmanager import Settings

initial_extensions = [
on_ready_extensions = [
'musicbot.cogs.nodemanager',
'musicbot.cogs.errors',
'musicbot.cogs.cogmanager',
'musicbot.cogs.settings',
'musicbot.cogs.misc',
'musicbot.cogs.helpformatter'
]

on_ready_extensions = [
'musicbot.cogs.nodemanager'
]


def _get_prefix(bot, message):
if not message.guild:
Expand All @@ -37,7 +37,7 @@ def _get_prefix(bot, message):
return commands.when_mentioned_or(*prefixes)(bot, message)


class Bot(commands.Bot):
class MusicBot(commands.Bot):
def __init__(self, datadir, debug: bool = False):
intents = discord.Intents.all()
super().__init__(command_prefix=_get_prefix,
Expand All @@ -57,12 +57,7 @@ def __init__(self, datadir, debug: bool = False):
self.main_logger = logger
self.logger = self.main_logger.bot_logger.getChild("Bot")
self.logger.debug("Debug: %s" % debug)

for extension in initial_extensions:
try:
self.load_extension(extension)
except Exception:
self.logger.exception("Loading of extension %s failed" % extension)
self.lavalink: Optional[lavalink.Client] = None

async def on_message(self, message):
if message.author.bot:
Expand Down Expand Up @@ -90,14 +85,18 @@ async def on_ready(self):
for extension in on_ready_extensions:
try:
self.logger.debug("Loading extension %s" % extension)
self.load_extension(extension)
await self.load_extension(extension)
except Exception:
self.logger.exception("Loading of extension %s failed" % extension)

print(f'\nLogged in as: {self.user.name}' +
f' in {len(self.guilds)} servers.')
print(f'Version: {discord.__version__}\n')
self.logger.debug("Bot Ready\n\n\n")
if self.user:
info = f'Logged in as: {self.user.name} in {len(self.guilds)} servers.'
border = "="*len(info)
self.logger.info(border)
self.logger.info(info)
self.logger.info(f'Version: {discord.__version__}')
self.logger.info(border)
self.logger.debug("Bot Ready")

self.session = aiohttp.ClientSession(loop=self.loop)
await self.change_presence(activity=discord.Game(type=0,
Expand All @@ -110,11 +109,11 @@ def run(self):
except Exception as e:
tb = e.__traceback__
traceback.print_tb(tb)
print(e)
self.logger.exception(e)


def run_bot(datadir, debug: bool = False):
bot = Bot(datadir, debug=debug)
bot = MusicBot(datadir, debug=debug)
bot.run()


Expand Down
53 changes: 53 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
env_name := "venv"
python := "./" + env_name+'/bin/python3'

default:
@just --list

# Make a new virtual environment
[private]
make_venv:
python3.10 -m venv {{env_name}}
{{python}} -m pip install --upgrade pip
{{python}} -m pip install -r requirements.txt
{{python}} -m pip install flake8
{{python}} -m pip install isort
{{python}} -m pip install pytest

# Make the environment if it does not exit
[private]
@venv:
[ -d {{env_name}} ] || just make_venv

# Run the bot
run: venv
{{python}} bot.py

# Run with debug logging enabled
debug: run
--debug

clean:
rm -rf {{env_name}}
# Lint with flake8
flake: venv
{{python}} -m flake8 bot.py musicbot

# Fix import order with isort
isort: venv
{{python}} -m isort --sp setup.cfg bot.py musicbot

# Run both isort and flake8
lint: venv
just isort
just flake

# Run tests
test: venv
{{python}} -m pytest

# Run lints and test
pre_commit: venv
just lint
just test

Loading

0 comments on commit c9973ec

Please sign in to comment.