Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7ead406
Remove swear word thing
Ry-DS Jul 30, 2022
541f885
rename music bot
Ry-DS Jul 30, 2022
6ff7ae0
Merge branch 'main' into music-bot
Ry-DS Jul 30, 2022
e2c0189
Merge branch 'main' into music-bot
Ry-DS Jul 30, 2022
148fea2
COG bot
Ry-DS Jul 30, 2022
cf4e5b1
Say if you're not in voice
Ry-DS Jul 30, 2022
61a1e03
Update w5-participant-workbook.md
IQ-TREX Aug 5, 2022
4c66b23
Merge branch 'main' into music-bot
Ry-DS Aug 15, 2022
0680959
Move stuff
Ry-DS Aug 15, 2022
f019fa1
Spelling mistake
Ry-DS Aug 15, 2022
8cb7021
Music bot now works with cogs
Ry-DS Aug 15, 2022
050b967
Bot works so well now
Ry-DS Aug 15, 2022
1115bcb
fix stop command
Ry-DS Aug 15, 2022
936cd39
cleanup and force jdk 13
Ry-DS Aug 16, 2022
03ec0a3
Update README.md
IQ-TREX Aug 17, 2022
24c005e
Update README.md
IQ-TREX Aug 17, 2022
4799a85
progress on musicbot
Ry-DS Aug 17, 2022
27efd8b
Merge remote-tracking branch 'origin/music-bot' into music-bot
Ry-DS Aug 17, 2022
7cbeb00
Update README.md
IQ-TREX Aug 23, 2022
d82dc44
Merge branch 'main' into music-bot
Ry-DS Aug 28, 2022
a243ad8
make music bot work with 2.0.0 (from combined branch)
Ry-DS Aug 28, 2022
774715e
More workbook content
Ry-DS Aug 28, 2022
59ad313
More workbook content
Ry-DS Aug 29, 2022
ea65438
Merge branch 'main' into music-bot
Ry-DS Aug 29, 2022
7c8145c
Move to w5
Ry-DS Sep 4, 2022
0782e0a
Merge branch 'main' into music-bot
Ry-DS Sep 4, 2022
a8f4f59
improvements to bot
Ry-DS Sep 4, 2022
6da2cec
fix newlines
Ry-DS Sep 4, 2022
143caaf
Update README.md
IQ-TREX Sep 5, 2022
d9e3433
Update README.md
IQ-TREX Sep 7, 2022
483ac8b
Notebook stuff
Ry-DS Sep 16, 2022
516eaf1
notebook changes
Ry-DS Sep 18, 2022
326cf5e
Improve notebook
Ry-DS Sep 18, 2022
e115313
Update README.md
Ry-DS Sep 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

*.pyc
/logs/

jdk-13.0.2/
Binary file added Lavalink.jar
Binary file not shown.
49 changes: 49 additions & 0 deletions application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
server: # REST and WS server
port: 2333
address: localhost
lavalink:
server:
password: "youshallnotpass"
sources:
youtube: true
bandcamp: true
soundcloud: true
twitch: true
vimeo: true
http: true
local: false
bufferDurationMs: 400 # The duration of the NAS buffer. Higher values fare better against longer GC pauses
frameBufferDurationMs: 5000 # How many milliseconds of audio to keep buffered
youtubePlaylistLoadLimit: 6 # Number of pages at 100 each
playerUpdateInterval: 5 # How frequently to send player updates to clients, in seconds
youtubeSearchEnabled: true
soundcloudSearchEnabled: true
gc-warnings: true
#ratelimit:
#ipBlocks: ["1.0.0.0/8", "..."] # list of ip blocks
#excludedIps: ["...", "..."] # ips which should be explicit excluded from usage by lavalink
#strategy: "RotateOnBan" # RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch
#searchTriggersFail: true # Whether a search 429 should trigger marking the ip as failing
#retryLimit: -1 # -1 = use default lavaplayer value | 0 = infinity | >0 = retry will happen this numbers times

metrics:
prometheus:
enabled: false
endpoint: /metrics

sentry:
dsn: ""
environment: ""
# tags:
# some_key: some_value
# another_key: another_value

logging:
file:
max-history: 30
max-size: 1GB
path: ./logs/

level:
root: INFO
lavalink: INFO
129 changes: 129 additions & 0 deletions cogs/musicbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Inspired by https://github.com/afazio1/robotic-nation-proj/blob/main/projects/discord-bot/new-music-bot/music-yt.py

import os
import subprocess
import time

import discord
import requests
import wavelink
from discord.ext import commands
from dotenv import load_dotenv


class MusicBot(commands.Cog):
voice_clients = {}

def __init__(self, client):
self.client = client

@commands.command()
async def join(self, ctx: commands.Context):
if not ctx.message.author.voice:
await ctx.send('You are not in a voice channel')
return
# Leave current channel
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()
channel = ctx.message.author.voice.channel
self.voice_clients[ctx.guild.id] = await channel.connect(cls=wavelink.Player())
await ctx.send(f'Joined {channel}')

@commands.command()
async def leave(self, ctx: commands.Context):
if ctx.guild.id in self.voice_clients:
await self.voice_clients[ctx.guild.id].stop()
del self.voice_clients[ctx.guild.id]
await ctx.send('Left')
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()

@commands.command()
async def play(self, ctx: commands.Context, *, search: wavelink.YouTubeTrack):
if ctx.guild.id not in self.voice_clients:
# Join the user's voice channel
await self.join(ctx)
if ctx.guild.id not in self.voice_clients:
return
voice = self.voice_clients[ctx.guild.id]
await voice.play(search)
embed = discord.Embed(
title=voice.source.title,
url=voice.source.uri,
description=f"Playing {voice.source.title} in {voice.channel}"
)
embed.set_image(url=voice.source.thumbnail)
if hasattr(ctx.author, 'avatar'):
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)

@commands.command()
async def stop(self, ctx: commands.Context):
if ctx.guild.id in self.voice_clients:
await self.voice_clients[ctx.guild.id].stop()
await ctx.send('Stopped')
else:
await ctx.send('Not in a voice channel')

@commands.command()
async def pause(self, ctx: commands.Context):
if ctx.guild.id in self.voice_clients:
await self.voice_clients[ctx.guild.id].pause()
await ctx.send('Paused')
else:
await ctx.send('Not in a voice channel')

@commands.command()
async def resume(self, ctx: commands.Context):
if ctx.guild.id in self.voice_clients:
await self.voice_clients[ctx.guild.id].resume()
await ctx.send('Resumed')
else:
await ctx.send('Not in a voice channel')

# Start the bot
@commands.Cog.listener()
async def on_ready(self):
print('Logged in as')
print(self.client.user.name)
print(self.client.user.id)
print('------')
# Try start Lavalink server
subprocess.Popen(["./jdk-13.0.2/bin/java", "-jar", "Lavalink.jar"])
# wait for port to open
while True:
try:
r = requests.get('http://localhost:2333')
break
except requests.exceptions.ConnectionError:
print("Waiting for lavalink to go live...")
time.sleep(1)
continue

async def connect_wavelink():
await self.client.wait_until_ready()
await wavelink.NodePool.create_node(
bot=self.client,
host='localhost',
port=2333,
password='youshallnotpass'
)

self.client.loop.create_task(connect_wavelink())

@commands.Cog.listener()
async def on_wavelink_node_ready(self, node: wavelink.Node):
print(f'Connected to wavefront! ID: {node.identifier}')


async def setup(bot):
await bot.add_cog(MusicBot(bot))


if __name__ == '__main__':
load_dotenv()
TOKEN = os.getenv('TOKEN')

client = commands.Bot(command_prefix='!')
client.add_cog(MusicBot(client))
client.run(TOKEN)
Binary file added w5/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading