General clean code

This commit is contained in:
Choco
2023-04-19 18:38:11 +08:00
parent 095f55842d
commit f1c23961bc
5 changed files with 29 additions and 33 deletions

View File

@@ -13,8 +13,7 @@ from function import (
tokens,
settings,
cooldown_check,
get_aliases,
connect_channel
get_aliases
)
from addons import lyricsPlatform
@@ -75,7 +74,7 @@ class Basic(commands.Cog):
async def connect(self, ctx: commands.Context, channel: discord.VoiceChannel = None) -> None:
"Connect to a voice channel."
try:
player = await connect_channel(ctx, channel)
player = await voicelink.connect_channel(ctx, channel)
except discord.errors.ClientException:
return await ctx.send(get_lang(ctx.guild.id, "alreadyConnected"))
@@ -88,7 +87,7 @@ class Basic(commands.Cog):
"Loads your input and added it to the queue."
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await connect_channel(ctx)
player = await voicelink.connect_channel(ctx)
if not player.is_user_join(ctx.author):
return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True)
@@ -128,7 +127,7 @@ class Basic(commands.Cog):
player: voicelink.Player = interaction.guild.voice_client
if not player:
player = await connect_channel(interaction)
player = await voicelink.connect_channel(interaction)
if not player.is_user_join(interaction.user):
return await interaction.response.send_message(player.get_msg('notInChannel').format(interaction.user.mention, player.channel.mention), ephemeral=True)
@@ -171,7 +170,7 @@ class Basic(commands.Cog):
"Loads your input and added it to the queue."
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await connect_channel(ctx)
player = await voicelink.connect_channel(ctx)
if not player.is_user_join(ctx.author):
return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True)
@@ -216,7 +215,7 @@ class Basic(commands.Cog):
"Adds a song with the given url or query on the top of the queue."
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await connect_channel(ctx)
player = await voicelink.connect_channel(ctx)
if not player.is_user_join(ctx.author):
return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True)

View File

@@ -15,8 +15,7 @@ from function import (
playlist_name,
settings,
get_aliases,
cooldown_check,
connect_channel
cooldown_check
)
from datetime import datetime
@@ -111,7 +110,7 @@ class Playlists(commands.Cog, name="playlist"):
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await connect_channel(ctx)
player = await voicelink.connect_channel(ctx)
if result['playlist']['type'] == 'link':
tracks = await search_playlist(result['playlist']['uri'], ctx.author, timeNeed=False)

View File

@@ -3,7 +3,6 @@ import json
import aiohttp
import os
from importlib import import_module
from discord.ext import commands
from random import choice
from datetime import datetime
@@ -200,26 +199,6 @@ async def similar_track(player) -> bool:
return False
async def connect_channel(ctx: Union[commands.Context, discord.Interaction], channel: discord.VoiceChannel = None):
voicelink = import_module("voicelink");
try:
channel = channel or ctx.author.voice.channel if isinstance(ctx, commands.Context) else ctx.user.voice.channel
except:
raise voicelink.VoicelinkException(get_lang(ctx.guild.id, 'noChannel'))
check = channel.permissions_for(ctx.guild.me)
if check.connect == False or check.speak == False:
raise voicelink.VoicelinkException(get_lang(ctx.guild.id, 'noPermission'))
player: voicelink.Player = await channel.connect(cls=voicelink.Player(
ctx.bot if isinstance(ctx, commands.Context) else ctx.client, channel, ctx
))
if player.is_ipc_connected:
await player.send_ws({"op": "createPlayer", "members_id": [member.id for member in channel.members]})
return player
def time(millis:int) -> str:
seconds=(millis/1000)%60
minutes=(millis/(1000*60))%60
@@ -268,4 +247,4 @@ def cooldown_check(ctx: commands.Context) -> Optional[commands.Cooldown]:
return commands.Cooldown(cooldown[0], cooldown[1])
def get_aliases(name: str) -> list:
return settings.aliases_settings.get(name, [])
return settings.aliases_settings.get(name, [])

View File

@@ -31,7 +31,7 @@ from .events import *
from .exceptions import *
from .filters import *
from .objects import *
from .player import Player
from .player import Player, connect_channel
from .pool import *
from .queue import *
from .placeholders import Placeholders, build_embed

View File

@@ -60,6 +60,25 @@ from .queue import Queue, FairQueue
from .placeholders import Placeholders, build_embed
from random import shuffle
async def connect_channel(ctx: Union[commands.Context, Interaction], channel: VoiceChannel = None):
try:
channel = channel or ctx.author.voice.channel if isinstance(ctx, commands.Context) else ctx.user.voice.channel
except:
raise VoicelinkException(func.get_lang(ctx.guild.id, 'noChannel'))
check = channel.permissions_for(ctx.guild.me)
if check.connect == False or check.speak == False:
raise VoicelinkException(func.get_lang(ctx.guild.id, 'noPermission'))
player: Player = await channel.connect(cls=Player(
ctx.bot if isinstance(ctx, commands.Context) else ctx.client, channel, ctx
))
if player.is_ipc_connected:
await player.send_ws({"op": "createPlayer", "members_id": [member.id for member in channel.members]})
return player
class Player(VoiceProtocol):
"""The base player class for Voicelink.
In order to initiate a player, you must pass it in as a cls when you connect to a channel.