updated after a while because local stuff
This commit is contained in:
+240
@@ -0,0 +1,240 @@
|
||||
import asyncio
|
||||
|
||||
import discord
|
||||
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
|
||||
class VoiceEntry:
|
||||
|
||||
def __init__(self, message, player):
|
||||
|
||||
self.requester = message.author
|
||||
|
||||
self.channel = message.channel
|
||||
|
||||
self.player = player
|
||||
|
||||
|
||||
|
||||
def __str__(self):
|
||||
|
||||
fmt = '*{0.title}* verzocht door {1.display_name}'
|
||||
|
||||
duration = self.player.duration
|
||||
|
||||
if duration:
|
||||
|
||||
fmt = fmt + ' [{0[0]}m {0[1]}s]'.format(divmod(duration, 60))
|
||||
|
||||
return fmt.format(self.player, self.requester)
|
||||
|
||||
|
||||
|
||||
class VoiceState:
|
||||
|
||||
def __init__(self, bot):
|
||||
|
||||
self.current = None
|
||||
|
||||
self.voice = None
|
||||
|
||||
self.bot = bot
|
||||
|
||||
self.play_next_song = asyncio.Event()
|
||||
|
||||
self.songs = asyncio.Queue()
|
||||
|
||||
self.skip_votes = set() # a set of user_ids that voted
|
||||
|
||||
self.audio_player = self.bot.loop.create_task(self.audio_player_task())
|
||||
|
||||
|
||||
|
||||
def is_playing(self):
|
||||
|
||||
if self.voice is None or self.current is None:
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
player = self.current.player
|
||||
|
||||
return not player.is_done()
|
||||
|
||||
|
||||
|
||||
@property
|
||||
|
||||
def player(self):
|
||||
|
||||
return self.current.player
|
||||
|
||||
|
||||
|
||||
def skip(self):
|
||||
|
||||
self.skip_votes.clear()
|
||||
|
||||
if self.is_playing():
|
||||
|
||||
self.player.stop()
|
||||
|
||||
|
||||
|
||||
def toggle_next(self):
|
||||
|
||||
self.bot.loop.call_soon_threadsafe(self.play_next_song.set)
|
||||
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
def audio_player_task(self):
|
||||
|
||||
while True:
|
||||
|
||||
self.play_next_song.clear()
|
||||
|
||||
self.current = yield from self.songs.get()
|
||||
|
||||
yield from self.bot.send_message(self.current.channel, 'U luistert naar: ' + str(self.current))
|
||||
|
||||
self.current.player.start()
|
||||
|
||||
yield from self.play_next_song.wait()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if not discord.opus.is_loaded():
|
||||
|
||||
# the 'opus' library here is opus.dll on windows
|
||||
|
||||
# or libopus.so on linux in the current directory
|
||||
|
||||
# you should replace this with the location the
|
||||
|
||||
# opus library is located in and with the proper filename.
|
||||
|
||||
# note that on windows this DLL is automatically provided for you
|
||||
|
||||
discord.opus.load_opus('opus')
|
||||
|
||||
|
||||
|
||||
class Music:
|
||||
|
||||
"""Play that funky music white boy.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, bot):
|
||||
|
||||
self.bot = bot
|
||||
|
||||
self.voice_states = {}
|
||||
|
||||
|
||||
|
||||
def __check(self, ctx):
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
def get_voice_state(self, server):
|
||||
|
||||
state = self.voice_states.get(server.id)
|
||||
|
||||
if state is None:
|
||||
|
||||
state = VoiceState(self.bot)
|
||||
|
||||
self.voice_states[server.id] = state
|
||||
|
||||
|
||||
|
||||
return state
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
def create_voice_client(self, channel):
|
||||
|
||||
voice = yield from self.bot.join_voice_channel(channel)
|
||||
|
||||
state = self.get_voice_state(channel.server)
|
||||
|
||||
state.voice = voice
|
||||
|
||||
|
||||
|
||||
def __unload(self):
|
||||
|
||||
for state in self.voice_states.values():
|
||||
|
||||
try:
|
||||
|
||||
state.audio_player.cancel()
|
||||
|
||||
if state.voice:
|
||||
|
||||
self.bot.loop.create_task(state.voice.disconnect())
|
||||
|
||||
except:
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
def join(self, ctx, *, channel : discord.Channel):
|
||||
|
||||
"""Verplaats stroopie naar een bepaald kanaal."""
|
||||
|
||||
try:
|
||||
|
||||
yield from self.create_voice_client(channel)
|
||||
|
||||
except discord.ClientException:
|
||||
|
||||
yield from self.bot.say('Already in a voice channel...')
|
||||
|
||||
except discord.InvalidArgument:
|
||||
|
||||
yield from self.bot.say('This is not a voice channel...')
|
||||
|
||||
else:
|
||||
|
||||
yield from self.bot.say('Ready to play audio in ' + channel.name)
|
||||
|
||||
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
||||
@asyncio.coroutine
|
||||
|
||||
def summon(self, ctx):
|
||||
|
||||
"""Roep stroopie naar jouw kanaal toe.."""
|
||||
|
||||
summoned_channel = ctx.message.author.voice_channel
|
||||
|
||||
if summoned_channel is None:
|
||||
|
||||
yield from self.bot.say('You are not in a voice channel.')
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
state = self.get_voice_state(ctx.message.server)
|
||||
|
||||
Reference in New Issue
Block a user