turdbot/.py.old/cogs/voice.py

121 lines
4.8 KiB
Python
Raw Normal View History

2023-01-24 21:07:47 -07:00
import discord
2023-01-30 12:02:42 -07:00
from discord.ext import bridge, commands
2023-02-09 19:42:08 -07:00
import wavelink
2023-01-24 21:07:47 -07:00
from bin.storage import Config
class Voice(commands.Cog):
def __init__(self, bot):
self.bot = bot
2023-03-09 22:06:51 -07:00
self.queue = wavelink.Queue()
2023-01-24 21:07:47 -07:00
2023-02-09 19:42:08 -07:00
@commands.Cog.listener()
async def on_ready(self):
await wavelink.NodePool.create_node(bot=self.bot,
host=self.bot.config["wavelink"]["host"],
port=self.bot.config["wavelink"]["port"],
password=self.bot.config["wavelink"]["password"],)
2023-03-08 12:15:55 -07:00
@commands.Cog.listener()
2023-03-11 18:30:45 -07:00
async def on_wavelink_track_end(self, player, track, reason):
print("track ended")
2023-03-11 19:21:39 -07:00
if not self.queue.is_empty and reason == 'FINISHED':
2023-03-09 22:06:51 -07:00
await player.play(self.queue.get())
2023-02-06 14:12:15 -07:00
@bridge.bridge_command(alises=["j"])
2023-03-09 22:06:51 -07:00
async def join(self, ctx, *, args: str=""):
2023-01-30 12:02:42 -07:00
await ctx.defer()
2023-03-09 22:06:51 -07:00
if ctx.author.voice is None and args == "":
2023-01-24 21:07:47 -07:00
await ctx.respond("You are not in a voice channel")
2023-03-09 22:06:51 -07:00
elif args != "":
channel = self.bot.get_channel(discord.utils.get(ctx.guild.channels, name=args).id)
await channel.connect(cls=wavelink.Player)
2023-01-24 21:07:47 -07:00
else:
2023-03-09 22:06:51 -07:00
await ctx.author.voice.channel.connect(cls=wavelink.Player)
2023-01-24 21:07:47 -07:00
2023-02-06 14:12:15 -07:00
@bridge.bridge_command(alises=["l"])
2023-01-24 21:07:47 -07:00
async def leave(self, ctx):
2023-01-30 12:02:42 -07:00
await ctx.defer()
2023-01-24 21:07:47 -07:00
if ctx.voice_client is None:
await ctx.respond("I am not in a voice channel")
else:
await ctx.voice_client.disconnect()
2023-02-06 14:12:15 -07:00
@bridge.bridge_command(aliases=["p"])
2023-03-08 12:15:55 -07:00
async def play(self, ctx, *, link: str=""):
2023-03-09 22:06:51 -07:00
await ctx.defer()
providedchannel = False
queueoverride = False
earape = False
channel = ""
args = link.split(" -")
if len(args) > 1:
for arg in range(len(args)):
if args[arg].startswith("channel") or args[arg].startswith("c"):
channel = self.bot.get_channel(discord.utils.get(ctx.guild.channels, name=args[arg].split(" ")[1]).id)
providedchannel = True
if args[arg].startswith("now") or args[arg].startswith("n"):
queueoverride = True
if args[arg].startswith("earrape") or args[arg].startswith("e"):
earape = True
track = await wavelink.YouTubeTrack.search(args[0], return_first=True)
2023-02-06 14:12:15 -07:00
2023-03-09 22:06:51 -07:00
if providedchannel and ctx.author.guild_permissions.administrator == False:
await ctx.respond("You do not have permission to specify a channel")
return
if queueoverride and ctx.author.guild_permissions.administrator == False:
await ctx.respond("You do not have permission to override the queue")
return
if earape and ctx.author.guild_permissions.administrator == False:
await ctx.respond("You do not have permission to earrape")
return
2023-01-29 10:10:32 -07:00
if ctx.author.voice is None and channel == "":
2023-01-30 12:02:42 -07:00
await ctx.respond("You are not in a voice channel, to specify a channel use `play <link> -channel <channel>`")
2023-01-29 10:10:32 -07:00
return
2023-02-06 14:12:15 -07:00
2023-01-24 21:07:47 -07:00
if ctx.voice_client is None:
2023-02-09 19:42:08 -07:00
if channel == "":
channel = ctx.author.voice.channel
2023-03-08 12:15:55 -07:00
await channel.connect(cls=wavelink.Player)
2023-02-06 14:12:15 -07:00
2023-01-30 12:47:27 -07:00
if link == "" and ctx.voice_client.is_paused():
2023-03-08 12:15:55 -07:00
await ctx.voice_client.resume()
2023-01-29 10:10:32 -07:00
return
2023-02-06 14:12:15 -07:00
2023-01-29 10:10:32 -07:00
if ctx.voice_client.is_paused():
ctx.voice_client.resume()
2023-01-24 21:07:47 -07:00
2023-03-09 22:06:51 -07:00
if (self.queue.is_empty and not ctx.voice_client.is_playing()) or queueoverride:
2023-03-11 18:30:45 -07:00
#not implemented
#if earape:
#print("earrape")
#await ctx.voice_client.set_filter(wavelink.Filter(distortion=wavelink.Distortion(sin_offset=2,sin_scale=2,tan_offset=3)),seek=True)
2023-03-08 12:15:55 -07:00
await ctx.voice_client.play(track)
await ctx.respond(f"Now playing: {track.title} by {track.author}\n {track.uri}")
else:
2023-03-09 22:06:51 -07:00
self.queue.put(item=track)
2023-03-08 12:15:55 -07:00
await ctx.respond(f"Added to queue: {track.title} by {track.author}\n {track.uri}")
@bridge.bridge_command(aliases=["stop","s"])
2023-01-29 10:10:32 -07:00
async def pause(self, ctx):
2023-01-30 12:02:42 -07:00
await ctx.defer()
2023-01-29 10:10:32 -07:00
if ctx.voice_client is None:
await ctx.respond("I am not in a voice channel")
else:
2023-02-09 19:42:08 -07:00
await ctx.voice_client.pause()
2023-03-09 22:06:51 -07:00
await ctx.respond("Paused")
2023-01-30 12:02:42 -07:00
2023-03-09 22:06:51 -07:00
@bridge.bridge_command(aliases=["next","n","sk"])
2023-01-30 12:02:42 -07:00
async def skip(self, ctx):
await ctx.defer()
if ctx.voice_client is None:
await ctx.respond("I am not in a voice channel")
else:
2023-03-09 22:06:51 -07:00
await ctx.voice_client.play(self.queue.get())
2023-01-30 12:02:42 -07:00
await ctx.respond("Skipped")
2023-01-24 21:07:47 -07:00
def setup(bot):
2023-03-11 18:30:45 -07:00
bot.add_cog(Voice(bot))