Browse Source

youtube stream

dp 5 years ago
parent
commit
cab136c71c
1 changed files with 101 additions and 0 deletions
  1. 101
    0
      nec_discord_bot.py

+ 101
- 0
nec_discord_bot.py View File

@@ -0,0 +1,101 @@
1
+import discord
2
+import youtube_dl
3
+from discord.ext import commands
4
+from discord import FFmpegPCMAudio
5
+import sys
6
+
7
+client = commands.Bot(command_prefix = "!")
8
+
9
+youtube_dl.utils.bug_reports_message = lambda: ''
10
+
11
+ytdl_format_options = {
12
+	'format': 'bestaudio/best',
13
+	'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
14
+	'restrictfilenames': True,
15
+	'noplaylist': True,
16
+	'nocheckcertificate': True,
17
+	'ignoreerrors': False,
18
+	'logtostderr': False,
19
+	'quiet': True,
20
+	'no_warnings': True,
21
+	'default_search': 'auto',
22
+	'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
23
+}
24
+
25
+ffmpeg_options = {
26
+	'options': '-vn'
27
+}
28
+
29
+ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
30
+
31
+
32
+class YTDLSource(discord.PCMVolumeTransformer):
33
+	def __init__(self, source, *, data, volume=0.5):
34
+		super().__init__(source, volume)
35
+
36
+		self.data = data
37
+
38
+		self.title = data.get('title')
39
+		self.url = data.get('url')
40
+
41
+	@classmethod
42
+	async def from_url(cls, url, *, loop=None, stream=False):
43
+		loop = loop or asyncio.get_event_loop()
44
+		data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
45
+
46
+		if 'entries' in data:
47
+			# take first item from a playlist
48
+			data = data['entries'][0]
49
+
50
+		filename = data['url'] if stream else ytdl.prepare_filename(data)
51
+		return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
52
+
53
+@client.event
54
+async def on_ready():
55
+	print('We have logged in as {0.user}'.format(client))
56
+
57
+@client.command(pass_context=True)
58
+async def join(ctx):
59
+	print("join")
60
+	channel = ctx.message.author.voice.channel
61
+	await channel.connect()
62
+
63
+@client.command(pass_context=True)
64
+async def leave(ctx):
65
+	print("leave")
66
+	channel = ctx.message.author.voice.channel
67
+	await ctx.voice_client.disconnect()
68
+
69
+@client.command(pass_context=True)
70
+async def yt(ctx, url):
71
+	async with ctx.typing():
72
+		player = await YTDLSource.from_url(url, loop=client.loop, stream=True)
73
+		ctx.voice_client.play(player, after=lambda e: print("Player error: %s" % e) if e else None)
74
+
75
+	await ctx.send("Now playing: {}".format(player.title))
76
+
77
+@client.command(pass_context=True)
78
+async def volume(ctx, volume: int):
79
+	if ctx.voice_client is None:
80
+		return await ctx.send("Kein Audiochannel gefunden")
81
+	ctx.voice_client.source.volume = volume / 100
82
+	await ctx.send("Changed volume to {}".format(volume))
83
+
84
+
85
+@yt.before_invoke
86
+async def ensure_voice(ctx):
87
+	if ctx.voice_client is None:
88
+		if ctx.author.voice:
89
+			await ctx.author.voice.channel.connect()
90
+		else:
91
+			await ctx.send("You are not connected to a voice channel.")
92
+			raise commands.CommandError("Author not connected to a voice channel.")
93
+	elif ctx.voice_client.is_playing():
94
+		ctx.voice_client.stop()
95
+
96
+@client.command(pass_context=True)
97
+async def shutdown(ctx):
98
+	sys.exit()
99
+
100
+
101
+client.run("NTAyMTY4Njk1MjIxNjQ5NDIw.DqkBOw.01VYdcjPC-wN1ARVVBPpix-1Fj8")

Loading…
Cancel
Save