import os
import shutil
from os import system

import discord
import asyncio
import os.path
import linecache
import datetime

import urllib

import requests
from bs4 import BeautifulSoup

from discord.utils import get
from discord.ext import commands


from discord.ext.commands import CommandNotFound
import logging
import itertools
import sys
import traceback
import random
import itertools
import math
from async_timeout import timeout
from functools import partial
import functools
from youtube_dl import YoutubeDL
import youtube_dl
from io import StringIO
import time
import urllib.request
from gtts import gTTS

from urllib.request import URLError
from urllib.request import HTTPError
from urllib.request import urlopen
from urllib.request import Request, urlopen
from urllib.parse import quote
import re
import warnings
import unicodedata
import json

from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.tools import argparser

##################### 로깅 ###########################
log_stream = StringIO()    
logging.basicConfig(stream=log_stream, level=logging.WARNING)

#ilsanglog = logging.getLogger('discord')
#ilsanglog.setLevel(level = logging.WARNING)
#handler = logging.StreamHandler()
#handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
#ilsanglog.addHandler(handler)
#####################################################

def init():
	global command

	command = []
	fc = []

	command_inidata = open('command.ini', 'r', encoding = 'utf-8')
	command_inputData = command_inidata.readlines()

	############## 뮤직봇 명령어 리스트 #####################
	for i in range(len(command_inputData)):
		tmp_command = command_inputData[i][12:].rstrip('\n')
		fc = tmp_command.split(', ')
		command.append(fc)
		fc = []

	del command[0]

	command_inidata.close()

	#print (command)

init()

#mp3 파일 생성함수(gTTS 이용, 남성목소리)
async def MakeSound(saveSTR, filename):
	
	tts = gTTS(saveSTR, lang = 'ko')
	tts.save('./' + filename + '.wav')
	'''
	try:
		encText = urllib.parse.quote(saveSTR)
		urllib.request.urlretrieve("https://clova.ai/proxy/voice/api/tts?text=" + encText + "%0A&voicefont=1&format=wav",filename + '.wav')
	except Exception as e:
		print (e)
		tts = gTTS(saveSTR, lang = 'ko')
		tts.save('./' + filename + '.wav')
		pass
	'''
#mp3 파일 재생함수	
async def PlaySound(voiceclient, filename):
	source = discord.FFmpegPCMAudio(filename)
	try:
		voiceclient.play(source)
	except discord.errors.ClientException:
		while voiceclient.is_playing():
			await asyncio.sleep(1)
	while voiceclient.is_playing():
		await asyncio.sleep(1)
	voiceclient.stop()
	source.cleanup()

# Silence useless bug reports messages
youtube_dl.utils.bug_reports_message = lambda: ''


class VoiceError(Exception):
	pass


class YTDLError(Exception):
	pass


class YTDLSource(discord.PCMVolumeTransformer):
	YTDL_OPTIONS = {
		'format': 'bestaudio/best',
		'extractaudio': True,
		'audioformat': 'mp3',
		'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
		'restrictfilenames': True,
		'noplaylist': False,
		'nocheckcertificate': True,
		'ignoreerrors': False,
		'logtostderr': False,
		'quiet': True,
		'no_warnings': True,
		'default_search': 'auto',
		'source_address': '0.0.0.0',
		'force-ipv4' : True,
    		'-4': True
	}

	FFMPEG_OPTIONS = {
		'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
		'options': '-vn',
	}

	ytdl = youtube_dl.YoutubeDL(YTDL_OPTIONS)

	def __init__(self, ctx: commands.Context, source: discord.FFmpegPCMAudio, *, data: dict, volume: float = 0.5):
		super().__init__(source, volume)
		self.requester = ctx.author
		self.channel = ctx.channel
		self.data = data

		self.uploader = data.get('uploader')
		self.uploader_url = data.get('uploader_url')
		date = data.get('upload_date')
		self.upload_date = date[6:8] + '.' + date[4:6] + '.' + date[0:4]
		self.title = data.get('title')
		self.thumbnail = data.get('thumbnail')
		self.description = data.get('description')
		self.duration = self.parse_duration(int(data.get('duration')))
		self.tags = data.get('tags')
		self.url = data.get('webpage_url')
		self.views = data.get('view_count')
		self.likes = data.get('like_count')
		self.dislikes = data.get('dislike_count')
		self.stream_url = data.get('url')

	def __str__(self):
		return '**{0.title}** by **{0.uploader}**'.format(self)

	@classmethod
	async def create_source(cls, bot, ctx: commands.Context, search: str, *, loop: asyncio.BaseEventLoop = None):
		loop = loop or asyncio.get_event_loop()

		if "http" not in search:
			partial = functools.partial(cls.ytdl.extract_info, f"ytsearch5:{search}", download=False, process=False)

			data = await loop.run_in_executor(None, partial)

			if data is None:
				raise YTDLError('Couldn\'t find anything that matches `{}`'.format(search))

			emoji_list : list = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "🚫"]
			song_list_str : str = ""
			cnt : int = 0
			song_index : int = 0

			for data_info in data["entries"]:
				cnt += 1
				if 'title' not in data_info:
					data_info['title'] = f"{search} - 제목 정보 없음"
				song_list_str += f"`{cnt}.` [**{data_info['title']}**](https://www.youtube.com/watch?v={data_info['url']})\n"

			embed = discord.Embed(description= song_list_str)
			embed.set_footer(text=f"10초 안에 미선택시 취소됩니다.")

			song_list_message = await ctx.send(embed = embed)

			for emoji in emoji_list:
				await song_list_message.add_reaction(emoji)

			def reaction_check(reaction, user):
				return (reaction.message.id == song_list_message.id) and (user.id == ctx.author.id) and (str(reaction) in emoji_list)
			try:
				reaction, user = await bot.wait_for('reaction_add', check = reaction_check, timeout = 10)
			except asyncio.TimeoutError:
				reaction = "🚫"

			for emoji in emoji_list:
				await song_list_message.remove_reaction(emoji, bot.user)

			await song_list_message.delete(delay = 10)
			
			if str(reaction) == "1️⃣":
				song_index = 0
			elif str(reaction) == "2️⃣":
				song_index = 1
			elif str(reaction) == "3️⃣":
				song_index = 2
			elif str(reaction) == "4️⃣":
				song_index = 3
			elif str(reaction) == "5️⃣":
				song_index = 4
			else:
				return False
			
			result_url = f"https://www.youtube.com/watch?v={data['entries'][song_index]['url']}"
		else:
			result_url = search

		webpage_url = result_url
		partial = functools.partial(cls.ytdl.extract_info, webpage_url, download=False)
		processed_info = await loop.run_in_executor(None, partial)
		if processed_info is None:
			raise YTDLError('Couldn\'t fetch `{}`'.format(webpage_url))
		
		if 'entries' not in processed_info:
			info = processed_info
		else:
			info = None
			while info is None:
				try:
					info = processed_info['entries'].pop(0)
				except IndexError:
					raise YTDLError('Couldn\'t retrieve any matches for `{}`'.format(webpage_url))

		return cls(ctx, discord.FFmpegPCMAudio(info['url'], **cls.FFMPEG_OPTIONS), data=info)

	@staticmethod
	def parse_duration(duration: int):
		return time.strftime('%H:%M:%S', time.gmtime(duration))


class Song:
	__slots__ = ('source', 'requester')

	def __init__(self, source: YTDLSource):
		self.source = source
		self.requester = source.requester

	def create_embed(self):
		embed = (discord.Embed(title='Now playing',
							description='**```fix\n{0.source.title}\n```**'.format(self),
							color=discord.Color.blurple())
				.add_field(name='Duration', value=self.source.duration)
				.add_field(name='Requested by', value=self.requester.mention)
				.add_field(name='Uploader', value='[{0.source.uploader}]({0.source.uploader_url})'.format(self))
				.add_field(name='URL', value='[Click]({0.source.url})'.format(self))
				.set_thumbnail(url=self.source.thumbnail))

		return embed


class SongQueue(asyncio.Queue):
	def __getitem__(self, item):
		if isinstance(item, slice):
			return list(itertools.islice(self._queue, item.start, item.stop, item.step))
		else:
			return self._queue[item]

	def __iter__(self):
		return self._queue.__iter__()

	def __len__(self):
		return self.qsize()

	def clear(self):
		self._queue.clear()

	def shuffle(self):
		random.shuffle(self._queue)

	def select(self, index : int, loop : bool = False):
		for i in range(index-1):
			if not loop:
				del self._queue[0]
			else:
				self._queue.append(self._queue[0])
				del self._queue[0]

	def remove(self, index: int):
		del self._queue[index]


class VoiceState:
	def __init__(self, bot: commands.Bot, ctx: commands.Context):
		self.bot = bot
		self._ctx = ctx
		self._cog = ctx.cog

		self.current = None
		self.voice = None
		self.next = asyncio.Event()
		self.songs = SongQueue()

		self._loop = False
		self._volume = 0.5
		self.skip_votes = set()

		self.audio_player = bot.loop.create_task(self.audio_player_task())

	def __del__(self):
		self.audio_player.cancel()

	@property
	def loop(self):
		return self._loop

	@loop.setter
	def loop(self, value: bool):
		self._loop = value

	@property
	def volume(self):
		return self._volume

	@volume.setter
	def volume(self, value: float):
		self._volume = value

	@property
	def is_playing(self):
		return self.voice and self.current

	async def audio_player_task(self):
		while True:
			self.next.clear()

			if self.loop and self.current is not None:
				source1 = await YTDLSource.create_source(self.bot, self._ctx, self.current.source.url, loop=self.bot.loop)
				song1 = Song(source1)
				await self.songs.put(song1)
			else:
				pass

			try:
				async with timeout(180):  # 3 minutes
					self.current = await self.songs.get()
			except asyncio.TimeoutError:
				self.bot.loop.create_task(self.stop())
				return

			self.current.source.volume = self._volume
			self.voice.play(self.current.source, after=self.play_next_song)
			play_info_msg = await self.current.source.channel.send(embed=self.current.create_embed())
#			await play_info_msg.delete(delay = 20)

			await self.next.wait()

	def play_next_song(self, error=None):
		if error:
			raise VoiceError(str(error))

		self.next.set()

	def skip(self):
		self.skip_votes.clear()

		if self.is_playing:
			self.voice.stop()

	async def stop(self):
		self.songs.clear()

		if self.voice:
			await self.voice.disconnect()
			self.voice = None

		self.bot.loop.create_task(self._cog.cleanup(self._ctx))

class Music(commands.Cog):
	def __init__(self, bot: commands.Bot):
		self.bot = bot
		self.voice_states = {}

	def get_voice_state(self, ctx: commands.Context):
		state = self.voice_states.get(ctx.guild.id)
		if not state:
			state = VoiceState(self.bot, ctx)
			self.voice_states[ctx.guild.id] = state

		return state

	def cog_unload(self):
		for state in self.voice_states.values():
			self.bot.loop.create_task(state.stop())

	def cog_check(self, ctx: commands.Context):
		if not ctx.guild:
			raise commands.NoPrivateMessage('This command can\'t be used in DM channels.')

		return True

	async def cog_before_invoke(self, ctx: commands.Context):
		ctx.voice_state = self.get_voice_state(ctx)

	async def cog_command_error(self, ctx: commands.Context, error: commands.CommandError):
		await ctx.send('에러 : {}'.format(str(error)))
	'''
	@commands.command(name='join', invoke_without_subcommand=True)
	async def _join(self, ctx: commands.Context):
		destination = ctx.author.voice.channel
		if ctx.voice_state.voice:
			await ctx.voice_state.voice.move_to(destination)
			return
		ctx.voice_state.voice = await destination.connect()
	'''
	async def cleanup(self, ctx: commands.Context):
		del self.voice_states[ctx.guild.id]

	@commands.command(name=command[0][0], aliases=command[0][1:]) #음성 채널 입장
	#@commands.has_permissions(manage_guild=True)
	async def _summon(self, ctx: commands.Context, *, channel: discord.VoiceChannel = None):
		channel = ctx.message.author.voice.channel
		if not channel and not ctx.author.voice:
			raise VoiceError(':no_entry_sign: 현재 접속중인 음악채널이 없습니다.')

		destination = channel or ctx.author.voice.channel
		if ctx.voice_state.voice:
			await ctx.voice_state.voice.move_to(destination)
			return

		ctx.voice_state.voice = await destination.connect()

	@commands.command(name=command[1][0], aliases=command[1][1:]) #음성 채널 퇴장
	#@commands.has_permissions(manage_guild=True)
	async def _leave(self, ctx: commands.Context):
		if not ctx.voice_state.voice:
			return await ctx.send(embed=discord.Embed(title=":no_entry_sign: 현재 접속중인 음악채널이 없습니다.",colour = 0x2EFEF7))

		await ctx.voice_state.stop()
		del self.voice_states[ctx.guild.id]

	@commands.command(name=command[8][0], aliases=command[8][1:]) #볼륨 조절
	async def _volume(self, ctx: commands.Context, *, volume: int):
		vc = ctx.voice_client

		if not ctx.voice_state.is_playing:
			return await ctx.send(embed=discord.Embed(title=":mute: 현재 재생중인 음악이 없습니다.",colour = 0x2EFEF7))

		if not 0 < volume < 101:
			return await ctx.send(embed=discord.Embed(title=":no_entry_sign: 볼륨은 1 ~ 100 사이로 입력 해주세요.",colour = 0x2EFEF7))

		if vc.source:
			vc.source.volume = volume / 100

		ctx.voice_state.volume = volume / 100
		await ctx.send(embed=discord.Embed(title=f":loud_sound: 볼륨을 {volume}%로 조정하였습니다.",colour = 0x2EFEF7))

	@commands.command(name=command[7][0], aliases=command[7][1:]) #현재 재생 중인 목록
	async def _now(self, ctx: commands.Context):
		await ctx.send(embed=ctx.voice_state.current.create_embed())

	@commands.command(name=command[3][0], aliases=command[3][1:]) #음악 일시 정지
	#@commands.has_permissions(manage_guild=True)
	async def _pause(self, ctx: commands.Context):
		if ctx.voice_state.is_playing and ctx.voice_state.voice.is_playing():
			ctx.voice_state.voice.pause()
			await ctx.message.add_reaction('⏸')

	@commands.command(name=command[4][0], aliases=command[4][1:]) #음악 다시 재생
	#@commands.has_permissions(manage_guild=True)
	async def _resume(self, ctx: commands.Context):
		if ctx.voice_state.is_playing and ctx.voice_state.voice.is_paused():
			ctx.voice_state.voice.resume()
			await ctx.message.add_reaction('⏯')

	@commands.command(name=command[9][0], aliases=command[9][1:]) #음악 정지
	#@commands.has_permissions(manage_guild=True)
	async def _stop(self, ctx: commands.Context):
		ctx.voice_state.songs.clear()

		if ctx.voice_state.is_playing:
			ctx.voice_state.voice.stop()
			await ctx.message.add_reaction('⏹')

	@commands.command(name=command[5][0], aliases=command[5][1:]) #현재 음악 스킵
	async def _skip(self, ctx: commands.Context, *, args: int = 1):
		if not ctx.voice_state.is_playing:
			return await ctx.send(embed=discord.Embed(title=':mute: 현재 재생중인 음악이 없습니다.',colour = 0x2EFEF7))

		await ctx.message.add_reaction('⏭')

		if args != 1:
			ctx.voice_state.songs.select(args, ctx.voice_state.loop)

		ctx.voice_state.skip()
		'''	
		voter = ctx.message.author
		if voter == ctx.voice_state.current.requester:
			await ctx.message.add_reaction('⏭')
			ctx.voice_state.skip()
		elif voter.id not in ctx.voice_state.skip_votes:
			ctx.voice_state.skip_votes.add(voter.id)
			total_votes = len(ctx.voice_state.skip_votes)
			if total_votes >= 3:
				await ctx.message.add_reaction('⏭')
				ctx.voice_state.skip()
			else:
				await ctx.send('Skip vote added, currently at **{}/3**'.format(total_votes))
		else:
			await ctx.send('```이미 투표하셨습니다.```')
		'''
	@commands.command(name=command[6][0], aliases=command[6][1:]) #재생 목록
	async def _queue(self, ctx: commands.Context, *, page: int = 1):

		if len(ctx.voice_state.songs) == 0:
			return await ctx.send(embed=discord.Embed(title=':mute: 재생목록이 없습니다.',colour = 0x2EFEF7))
		
		items_per_page = 10
		pages = math.ceil(len(ctx.voice_state.songs) / items_per_page)

		start = (page - 1) * items_per_page
		end = start + items_per_page

		queue = ''
		for i, song in enumerate(ctx.voice_state.songs[start:end], start=start):
			queue += '`{0}.` [**{1.source.title}**]({1.source.url})\n'.format(i + 1, song)

		if ctx.voice_state.loop:
			embed = discord.Embed(title = '🔁  Now playing', description='**```fix\n{0.source.title}\n```**'.format(ctx.voice_state.current))
		else:
			embed = discord.Embed(title = 'Now playing', description='**```fix\n{0.source.title}\n```**'.format(ctx.voice_state.current))
		embed.add_field(name ='\u200B\n**{} tracks:**\n'.format(len(ctx.voice_state.songs)), value = f"\u200B\n{queue}")
		embed.set_thumbnail(url=ctx.voice_state.current.source.thumbnail)
		embed.set_footer(text='Viewing page {}/{}'.format(page, pages))
		await ctx.send(embed=embed)

	@commands.command(name=command[11][0], aliases=command[11][1:]) #음악 셔플
	async def _shuffle(self, ctx: commands.Context):
		if len(ctx.voice_state.songs) == 0:
			return await ctx.send(embed=discord.Embed(title=':mute: 재생목록이 없습니다.',colour = 0x2EFEF7))

		ctx.voice_state.songs.shuffle()
		result = await ctx.send(embed=discord.Embed(title=':twisted_rightwards_arrows: 셔플 완료!',colour = 0x2EFEF7))
		await result.add_reaction('🔀')

	@commands.command(name=command[10][0], aliases=command[10][1:]) #음악 삭제
	async def _remove(self, ctx: commands.Context, index: int):
		if len(ctx.voice_state.songs) == 0:
			return ctx.send(embed=discord.Embed(title=':mute: 재생목록이 없습니다.',colour = 0x2EFEF7))
		
#		remove_result = '`{0}.` [**{1.source.title}**] 삭제 완료!\n'.format(index, ctx.voice_state.songs[index - 1])
		result = await ctx.send(embed=discord.Embed(title='`{0}.` [**{1.source.title}**] 삭제 완료!\n'.format(index, ctx.voice_state.songs[index - 1]),colour = 0x2EFEF7))
		ctx.voice_state.songs.remove(index - 1)
		await result.add_reaction('✅')


	@commands.command(name=command[14][0], aliases=command[14][1:]) #음악 반복
	async def _loop(self, ctx: commands.Context):
		if not ctx.voice_state.is_playing:
			return await ctx.send(embed=discord.Embed(title=':mute: 현재 재생중인 음악이 없습니다.',colour = 0x2EFEF7))

		# Inverse boolean value to loop and unloop.
		ctx.voice_state.loop = not ctx.voice_state.loop
		if ctx.voice_state.loop :
			result = await ctx.send(embed=discord.Embed(title=':repeat: 반복재생이 설정되었습니다!',colour = 0x2EFEF7))
		else:
			result = await ctx.send(embed=discord.Embed(title=':repeat_one: 반복재생이 취소되었습니다!',colour = 0x2EFEF7))
		await result.add_reaction('🔁')

	@commands.command(name=command[2][0], aliases=command[2][1:]) #음악 재생
	async def _play(self, ctx: commands.Context, *, search: str):
		if not ctx.voice_state.voice:
			await ctx.invoke(self._summon)

		async with ctx.typing():
			try:
				source = await YTDLSource.create_source(self.bot, ctx, search, loop=self.bot.loop)
				if not source:
					return await ctx.send(f"노래 재생/예약이 취소 되었습니다.")
			except YTDLError as e:
				await ctx.send('에러가 발생했습니다 : {}'.format(str(e)))
			else:
				song = Song(source)

				await ctx.channel.purge(limit=1)
				await ctx.voice_state.songs.put(song)
				await ctx.send(embed=discord.Embed(title=f':musical_note: 재생목록 추가 : {str(source)}',colour = 0x2EFEF7))

#	@commands.command(name=command[13][0], aliases=command[13][1:]) #지우기
#	async def clear_channel_(self, ctx: commands.Context, *, msg: int = 1):
#		try:
#			msg = int(msg)
#		except:
#			await ctx.send(f"```지우고 싶은 줄수는 [숫자]로 입력해주세요!```")
#		await ctx.channel.purge(limit = msg)

	@_summon.before_invoke
	@_play.before_invoke
	async def ensure_voice_state(self, ctx: commands.Context):
		if not ctx.author.voice or not ctx.author.voice.channel:
			raise commands.CommandError('음성채널에 접속 후 사용해주십시오.')

		if ctx.voice_client:
			if ctx.voice_client.channel != ctx.author.voice.channel:
				raise commands.CommandError('봇이 이미 음성채널에 접속해 있습니다.')

#	@commands.command(name=command[12][0], aliases=command[12][1:])   #도움말
#	async def menu_(self, ctx):
#		command_list = ''
#		command_list += '!인중 : 봇상태가 안좋을 때 쓰세요!'     #!
#		command_list += ','.join(command[0]) + '\n'     #!들어가자
#		command_list += ','.join(command[1]) + '\n'     #!나가자
#		command_list += ','.join(command[2]) + ' [검색어] or [url]\n'     #!재생
#		command_list += ','.join(command[3]) + '\n'     #!일시정지
#		command_list += ','.join(command[4]) + '\n'     #!다시재생
#		command_list += ','.join(command[5]) + ' (숫자)\n'     #!스킵
#		command_list += ','.join(command[6]) + ' 혹은 [명령어] + [숫자]\n'     #!목록
#		command_list += ','.join(command[7]) + '\n'     #!현재재생
#		command_list += ','.join(command[8]) + ' [숫자 1~100]\n'     #!볼륨
#		command_list += ','.join(command[9]) + '\n'     #!정지
#		command_list += ','.join(command[10]) + '\n'     #!삭제
#		command_list += ','.join(command[11]) + '\n'     #!섞기
#		command_list += ','.join(command[14]) + '\n'     #!
#		command_list += ','.join(command[13]) + ' [숫자]\n'     #!경주
#		embed = discord.Embed(
#				title = "----- 명령어 -----",
#				description= '```' + command_list + '```',
#				color=0xff00ff
#				)
#		await ctx.send( embed=embed, tts=False)

	################ 음성파일 생성 후 재생 ################ 			
	@commands.command(name="==인중")
	async def playText_(self, ctx):
		#msg = ctx.message.content[len(ctx.invoked_with)+1:]
		#sayMessage = msg
		await MakeSound('뮤직봇이 많이 아파요. 잠시 후 사용해주세요.', './say' + str(ctx.guild.id))
		await ctx.send("```뮤직봇이 많이 아파요. 잠시 후 사용해주세요.```", tts=False)
		
		if not ctx.voice_state.voice:
			await ctx.invoke(self._summon)
			
		if ctx.voice_state.is_playing:
			ctx.voice_state.voice.stop()
		
		await PlaySound(ctx.voice_state.voice, './say' + str(ctx.guild.id) + '.wav')


		await ctx.voice_state.stop()
		del self.voice_states[ctx.guild.id]



#client = commands.Bot(command_prefix='==', help_command = None)
client = commands.Bot('', help_command = None)
client.add_cog(Music(client))

access_client_id = os.environ["client_id"]
access_client_secret = os.environ["client_secret"]

client_id = access_client_id
client_secret = access_client_secret

def create_soup(url, headers):
    res = requests.get(url, headers=headers)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, 'lxml')
    return soup

@client.event
async def on_ready():
    print(f'로그인 성공: {client.user.name}!')
    game = discord.Game("==명령어")
    await client.change_presence(status=discord.Status.online, activity=game)

@client.event 
async def on_command_error(ctx, error):
	if isinstance(error, CommandNotFound):
		return
	elif isinstance(error, discord.ext.commands.MissingRequiredArgument):
		return
	raise error

@client.command(pass_context = True, aliases=['==명령어'])
async def cmd_cmd_abc(ctx):
    await ctx.channel.purge(limit=1)
    emoji_list : list = ["🅰️", "1️⃣", "2️⃣", "3️⃣", "🚫"]

    embed = discord.Embed(title = "캬루봇 명령어 목록", colour = 0x30e08b)
    embed.add_field(name = ':a: 전체', value = '전체 명령어 보기', inline = False)
    embed.add_field(name = ':one: 일반', value = '일반 명령어 보기', inline = False)
    embed.add_field(name = ':two: TruckersMP', value = 'TruckersMP 관련 명령어 보기', inline = False)
    embed.add_field(name = ':three: 음악', value = '음악 재생 관련 명령어 보기', inline = False)
    embed.add_field(name = ':no_entry_sign: 취소', value = '실행 취소', inline = False)
    cmd_message = await ctx.send(embed = embed)
    for emoji in emoji_list:
        await cmd_message.add_reaction(emoji)

    def reaction_check(reaction, user):
        return (reaction.message.id == cmd_message.id) and (user.id == ctx.author.id) and (str(reaction) in emoji_list)
    try:
        reaction, user = await client.wait_for('reaction_add', check = reaction_check, timeout = 10)
    except asyncio.TimeoutError:
        reaction = "🚫"

    for emoji in emoji_list:
#        await cmd_message.remove_reaction(emoji, client.user)
        await cmd_message.delete(delay = 0)

    await cmd_message.delete(delay = 10)
			
    if str(reaction) == "1️⃣":
        embed1 = discord.Embed(title = "캬루봇 명령어 목록 [일반 명령어]", colour = 0x30e08b)
        embed1.add_field(name = '==지우기 <숫자>', value = '최근 1~99개의 메세지를 삭제합니다.', inline = False)
        embed1.add_field(name = '==내정보', value = '자신의 디스코드 정보를 보여줍니다.', inline = False)
        embed1.add_field(name = '==실검', value = '네이버의 급상승 검색어 TOP10을 보여줍니다.', inline = False)
        embed1.add_field(name = '==날씨 <지역>', value = '<지역>의 날씨를 알려줍니다.', inline = False)
        embed1.add_field(name = '==말해 <text>', value = '<text>를 말합니다.', inline = False)
        embed1.add_field(name = '==번역 <언어> <text>', value = '<text>를 번역합니다.', inline = False)
        embed1.add_field(name = '==유튜브 <text>', value = '유튜브에서 <text>를 검색합니다.', inline = False)
        embed1.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
        await ctx.channel.send(embed = embed1)
    elif str(reaction) == "2️⃣":
        embed2 = discord.Embed(title = "캬루봇 명령어 목록 [TruckersMP]", colour = 0x30e08b)
        embed2.add_field(name = '==T정보, ==ts', value = 'TruckersMP의  정보를 보여줍니다.', inline = False)
        embed2.add_field(name = '==T프로필 <TMPID>, ==tp', value = '해당 TMPID 아이디를 가진 사람의 프로필을 보여줍니다.', inline = False)
        embed2.add_field(name = '==T트래픽순위, ==ttr', value = 'TruckersMP의 트래픽 순위 TOP5를 보여줍니다.', inline = False)
        embed2.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
        await ctx.channel.send(embed = embed2)
    elif str(reaction) == "3️⃣":
        embed3 = discord.Embed(title = "캬루봇 명령어 목록 [음악 재생]", colour = 0x30e08b)
        embed3.add_field(name = '==들어와', value = '봇이 음성 통화방에 들어옵니다.', inline = False)
        embed3.add_field(name = '==나가', value = '봇이 음성 통화방에서 나갑니다.', inline = False)
        embed3.add_field(name = '==재생', value = '봇이 음악을 재생합니다.', inline = False)
        embed3.add_field(name = '==일시정지', value = '현재 재생 중인 음악을 일시 정지합니다.', inline = False)
        embed3.add_field(name = '==다시재생', value = '일시 정지한 음악을 다시 재생합니다.', inline = False)
        embed3.add_field(name = '==스킵', value = '현재 재생 중인 음악을 스킵합니다.', inline = False)
        embed3.add_field(name = '==목록', value = '재생 목록을 보여줍니다.', inline = False)
        embed3.add_field(name = '==현재재생', value = '현재 재생 중인 음악을 보여줍니다.', inline = False)
        embed3.add_field(name = '==볼륨', value = '봇의 볼륨을 조절합니다.', inline = False)
        embed3.add_field(name = '==정지', value = '현재 재생 중인 음악을 정지합니다.', inline = False)
        embed3.add_field(name = '==삭제 <트랙 번호>', value = '재생 목록에 있는 특정 음악을 삭제합니다.', inline = False)
        embed3.add_field(name = '==섞기', value = '재생 목록을 섞습니다.', inline = False)
        embed3.add_field(name = '==반복', value = '현재 재생 중인 음악을 반복 재생합니다.', inline = False)
        embed3.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
        await ctx.channel.send(embed = embed3)
    elif str(reaction) == "🅰️":
        embed6 = discord.Embed(title = "캬루봇 명령어 목록 [전체 명령어]", colour = 0x30e08b)
        embed6.add_field(name = '==지우기 <숫자>', value = '최근 1~99개의 메세지를 삭제합니다.', inline = False)
        embed6.add_field(name = '==내정보', value = '자신의 디스코드 정보를 보여줍니다.', inline = False)
        embed6.add_field(name = '==실검', value = '네이버의 급상승 검색어 TOP10을 보여줍니다.', inline = False)
        embed6.add_field(name = '==날씨 <지역>', value = '<지역>의 날씨를 알려줍니다.', inline = False)
        embed6.add_field(name = '==말해 <내용>', value = '<내용>을 말합니다.', inline = False)
        embed6.add_field(name = '==번역 <언어> <내용>', value = '<내용>을 번역합니다.', inline = False)
        embed6.add_field(name = '==유튜브 <text>', value = '유튜브에서 <text>를 검색합니다.', inline = False)
        embed6.add_field(name = '==T정보, ==ts', value = 'TruckersMP의 서버 정보를 보여줍니다.', inline = False)
        embed6.add_field(name = '==T프로필 <TMPID>, ==tp', value = '해당 TMPID 아이디를 가진 사람의 프로필을 보여줍니다.', inline = False)
        embed6.add_field(name = '==T트래픽순위, ==ttr', value = 'TruckersMP의 트래픽 순위 TOP5를 보여줍니다.', inline = False)
        embed6.add_field(name = '==들어와', value = '봇이 음성 통화방에 들어옵니다.', inline = False)
        embed6.add_field(name = '==나가', value = '봇이 음성 통화방에서 나갑니다.', inline = False)
        embed6.add_field(name = '==재생', value = '봇이 음악을 재생합니다.', inline = False)
        embed6.add_field(name = '==일시정지', value = '현재 재생 중인 음악을 일시 정지합니다.', inline = False)
        embed6.add_field(name = '==다시재생', value = '일시 정지한 음악을 다시 재생합니다.', inline = False)
        embed6.add_field(name = '==스킵', value = '현재 재생 중인 음악을 스킵합니다.', inline = False)
        embed6.add_field(name = '==목록', value = '재생 목록을 보여줍니다.', inline = False)
        embed6.add_field(name = '==현재재생', value = '현재 재생 중인 음악을 보여줍니다.', inline = False)
        embed6.add_field(name = '==볼륨', value = '봇의 볼륨을 조절합니다.', inline = False)
        embed6.add_field(name = '==정지', value = '현재 재생 중인 음악을 정지합니다.', inline = False)
        embed6.add_field(name = '==삭제 <트랙 번호>', value = '재생 목록에 있는 특정 음악을 삭제합니다.', inline = False)
        embed6.add_field(name = '==섞기', value = '재생 목록을 섞습니다.', inline = False)
        embed6.add_field(name = '==반복', value = '현재 재생 중인 음악을 반복 재생합니다.', inline = False)
        embed6.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
        await ctx.channel.send(embed = embed6)
    elif str(reaction) == "🚫":
        await cmd_message.delete(delay = 0)
    else:
        return False

@client.command(pass_context = True, aliases=['==지우기'])
@commands.has_permissions(administrator=True)
async def claer_clear_abc(ctx, amount):
    amount = int(amount)
    if amount < 100:
        await ctx.channel.purge(limit=amount)
        embed = discord.Embed(title=f":put_litter_in_its_place: {amount}개의 채팅을 삭제했어요.",colour = 0x2EFEF7)
        embed.set_footer(text = 'Service provided by RyuZU')
        await ctx.channel.send(embed=embed)
    else:
        await ctx.channel.purge(limit=1)
        await ctx.channel.send(embed=discord.Embed(title=f":no_entry_sign: 숫자를 99 이하로 입력해 주세요.",colour = 0x2EFEF7)) 
        embed = discord.Embed(title=f":put_litter_in_its_place: {amount}개의 채팅을 삭제했어요.",colour = 0x2EFEF7)
        embed.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
        await ctx.channel.send(embed=embed)

@client.command(aliases=['==핑'])
async def ping_ping_abc(ctx):
    await ctx.channel.send('퐁! `{}ms`'.format(round(client.latency * 1000)))

@client.command(pass_context = True, aliases=['==내정보'])
async def my_my_abc_profile(ctx):
    date = datetime.datetime.utcfromtimestamp(((int(ctx.author.id) >> 22) + 1420070400000) / 1000)
    embed = discord.Embed(title = ctx.author.display_name + "님의 정보", colour = 0x2EFEF7)
    embed.add_field(name = '사용자명', value = ctx.author.name, inline = False)
    embed.add_field(name = '가입일', value = str(date.year) + "년" + str(date.month) + "월" + str(date.day) + "일", inline = False)
    embed.add_field(name = '아이디', value = ctx.author.id, inline = False)
    embed.set_thumbnail(url = ctx.author.avatar_url)
    embed.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
    await ctx.channel.send(embed = embed)

@client.command(pass_context = True, aliases=['==카페'])
async def cafe_cafe_abc(ctx):
    embed = discord.Embed(title = "KCTG 공식 카페", colour = 0x2EFEF7)
    embed.add_field(name = 'https://cafe.naver.com/kctgofficial', value = "\n\u200b", inline = False)
    embed.set_thumbnail(url = "https://cdn.discordapp.com/attachments/740877681209507880/744451389396353106/KCTG_Wolf_1.png")
    embed.set_footer(text = 'Service provided by RyuZU', icon_url="https://cdn.discordapp.com/attachments/740877681209507880/755440825667813497/20200817_184231.jpg")
    await ctx.channel.send(embed = embed)

@client.command(pass_context = True, aliases=['==실검'])
async def search_search_abc_rank(ctx):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Whale/2.8.105.22 Safari/537.36'}
    url = "https://datalab.naver.com/keyword/realtimeList.naver?where=main"
    soup = create_soup(url, headers)
    rank_list = soup.find("ul", attrs={"class":"ranking_list"})
    one = rank_list.find_all("span", attrs={"class":"item_title"})[0].get_text().strip().replace("1", "") #순서대로 실검 1~10위
    two = rank_list.find_all("span", attrs={"class":"item_title"})[1].get_text().strip().replace("2", "")
    three = rank_list.find_all("span", attrs={"class":"item_title"})[2].get_text().strip().replace("3", "")
    four = rank_list.find_all("span", attrs={"class":"item_title"})[3].get_text().strip().replace("4", "")
    five = rank_list.find_all("span", attrs={"class":"item_title"})[4].get_text().strip().replace("5", "")
    six = rank_list.find_all("span", attrs={"class":"item_title"})[5].get_text().strip().replace("6", "")
    seven = rank_list.find_all("span", attrs={"class":"item_title"})[6].get_text().strip().replace("7", "")
    eight = rank_list.find_all("span", attrs={"class":"item_title"})[7].get_text().strip().replace("8", "")
    nine = rank_list.find_all("span", attrs={"class":"item_title"})[8].get_text().strip().replace("9", "")
    ten = rank_list.find_all("span", attrs={"class":"item_title"})[9].get_text().strip().replace("10", "")
    time = soup.find("span", attrs={"class":"time_txt _title_hms"}).get_text() #현재 시간
    await ctx.channel.send(f'Ⅰ ``{one}``\nⅡ ``{two}``\nⅢ ``{three}``\nⅣ ``{four}``\nⅤ ``{five}``\nⅥ ``{six}``\nⅦ ``{seven}``\nⅧ ``{eight}``\nⅨ ``{nine}``\nⅩ ``{ten}``\n\n``Time[{time}]``')

@client.command(pass_context = True, aliases=['==날씨'])
async def weather_weather_abc(ctx, arg1):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Whale/2.8.105.22 Safari/537.36'}
    url = f"https://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&query={arg1}+날씨&oquery=날씨&tqi=U1NQ%2FsprvmsssUNA1MVssssssPN-224813"
    soup = create_soup(url, headers)
    rotate = soup.find("span", attrs={"class":"btn_select"}).get_text() #지역
    cast = soup.find("p", attrs={"class":"cast_txt"}).get_text() #맑음, 흐림 같은거
    curr_temp = soup.find("p", attrs={"class":"info_temperature"}).get_text().replace("도씨", "") #현재 온도
    sen_temp = soup.find("span", attrs={"class":"sensible"}).get_text().replace("체감온도", "체감") #체감 온도
    min_temp = soup.find("span", attrs={"class":"min"}).get_text() #최저 온도
    max_temp = soup.find("span", attrs={"class":"max"}).get_text() #최고 온도
    # 오전, 오후 강수 확률
    morning_rain_rate = soup.find("span", attrs={"class":"point_time morning"}).get_text().strip() #오전
    afternoon_rain_rate = soup.find("span", attrs={"class":"point_time afternoon"}).get_text().strip() #오후

    # 미세먼지, 초미세먼지
    dust = soup.find("dl", attrs={"class":"indicator"})
    pm10 = dust.find_all("dd")[0].get_text() #미세먼지
    pm25 = dust.find_all("dd")[1].get_text() #초미세먼지

    daylist = soup.find("ul", attrs={"class":"list_area _pageList"})
    tomorrow = daylist.find_all("li")[1]
    #내일 온도
    to_min_temp = tomorrow.find_all("span")[12].get_text() #최저
    to_max_temp = tomorrow.find_all("span")[14].get_text() #최고
    #내일 강수
    to_morning_rain_rate = daylist.find_all("span", attrs={"class":"point_time morning"})[1].get_text().strip() #오전
    to_afternoon_rain_rate = daylist.find_all("span", attrs={"class":"point_time afternoon"})[1].get_text().strip() #오후

    await ctx.channel.send((rotate) + f'\n오늘의 날씨 ``' + (cast) + f'``\n__기온__ ``현재 {curr_temp}({sen_temp}) 최저 {min_temp} 최고 {max_temp}``\n__강수__ ``오전 {morning_rain_rate}`` ``오후 {afternoon_rain_rate}``\n__대기__ ``미세먼지 {pm10}`` ``초미세먼지 {pm25}``\n\n내일의 날씨\n__기온__ ``최저 {to_min_temp}˚`` ``최고 {to_max_temp}˚``\n__강수__ ``오전 {to_morning_rain_rate}`` ``오후 {to_afternoon_rain_rate}``')

@client.command(pass_context = True, aliases=['==말해'])
async def tell_tell_abc(ctx, *, arg):
    tell = str(arg)
    await ctx.channel.purge(limit=1)
    await ctx.channel.send(tell)

@client.command(pass_context = True, aliases=['==T정보', '==TS', '==t정보', '==ts'])
async def tmp_tmp_abc_server_status(ctx):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Whale/2.8.105.22 Safari/537.36'}
    url = "https://stats.truckersmp.com/"
    soup = create_soup(url, headers)
    #현재 접속중인 플레이어
    curr_status = soup.find("div", attrs={"class":"container-fluid"})
    sim1 = curr_status.find_all("div", attrs={"class":"server-count"})[0].get_text().strip()
    sim2 = curr_status.find_all("div", attrs={"class":"server-count"})[1].get_text().strip()
    sim_us = curr_status.find_all("div", attrs={"class":"server-count"})[2].get_text().strip()
    sim_sgp = curr_status.find_all("div", attrs={"class":"server-count"})[3].get_text().strip()
    arc = curr_status.find_all("div", attrs={"class":"server-count"})[4].get_text().strip()
    pro = curr_status.find_all("div", attrs={"class":"server-count"})[5].get_text().strip()
    pro_arc = curr_status.find_all("div", attrs={"class":"server-count"})[6].get_text().strip()
    #서버 온오프 여부
    sim1_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[0].get_text().strip().replace("LINE", "")
    sim2_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[1].get_text().strip().replace("LINE", "")
    sim_us_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[2].get_text().strip().replace("LINE", "")
    sim_sgp_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[3].get_text().strip().replace("LINE", "")
    arc_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[4].get_text().strip().replace("LINE", "")
    pro_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[5].get_text().strip().replace("LINE", "")
    pro_arc_sta = curr_status.find_all("div", attrs={"class":"server-status ONLINE"})[6].get_text().strip().replace("LINE", "")
    #서버 시간
    curr_game_time = soup.find("span", attrs={"id":"game_time"}).get_text().strip()

    embed = discord.Embed(title = "[ETS2] TruckersMP 서버 현황", colour = 0x2EFEF7)
    embed.add_field(name = f'`[{sim1_sta}]` Simulation 1', value = f"{sim1}", inline = False)
    embed.add_field(name = f'`[{sim2_sta}]` Simulation 2', value = f"{sim2}", inline = False)
    embed.add_field(name = f'`[{sim_us_sta}]` [US] Simulation', value = f"{sim_us}", inline = False)
    embed.add_field(name = f'`[{sim_sgp_sta}]` [SGP] Simulation', value = f"{sim_sgp}", inline = False)
    embed.add_field(name = f'`[{arc_sta}]` Arcade', value = f"{arc}", inline = False)
    embed.add_field(name = f'`[{pro_sta}]` ProMods', value = f"{pro}", inline = False)
    embed.add_field(name = f'`[{pro_arc_sta}]` ProMods Arcade', value = f"{pro_arc}", inline = False)
    embed.set_footer(text=f"서버 시간: {curr_game_time}")
    await ctx.channel.send(embed = embed)

@client.command(pass_context = True, aliases=['==T트래픽순위', '==TTR', '==t트래픽순위', '==ttr'])
async def tmp_tmp_abc_traffic(ctx):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Whale/2.8.105.22 Safari/537.36'}
    url = "https://traffic.krashnz.com/"
    soup = create_soup(url, headers)
    #실시간 트래픽 순위
    traffic_top = soup.find("ul", attrs={"class":"list-group mb-3"})
    rank1 = traffic_top.find_all("div")[1].get_text().strip()
    rank2 = traffic_top.find_all("div")[2].get_text().strip()
    rank3 = traffic_top.find_all("div")[3].get_text().strip()
    rank4 = traffic_top.find_all("div")[4].get_text().strip()
    rank5 = traffic_top.find_all("div")[5].get_text().strip()
    g_set = soup.find("div", attrs={"class":"row text-center mb-2"})
    g_player = g_set.find_all("span", attrs={"class":"stats-number"})[0].get_text().strip()
    g_time = g_set.find_all("span", attrs={"class":"stats-number"})[1].get_text().strip()

    embed = discord.Embed(title = "[ETS2] TruckersMP 실시간 트래픽 TOP5", colour = 0x2EFEF7)
    embed.add_field(name = f'{rank1}', value = "\n\u200b", inline = False)
    embed.add_field(name = f'{rank2}', value = "\n\u200b", inline = False)
    embed.add_field(name = f'{rank3}', value = "\n\u200b", inline = False)
    embed.add_field(name = f'{rank4}', value = "\n\u200b", inline = False)
    embed.add_field(name = f'{rank5}', value = f"\n{g_player} players tracked / {g_time} in-game time", inline = False)
    await ctx.channel.send(embed = embed)

@client.command(pass_context = True, aliases=['==T프로필', '==TP', '==t프로필', '==tp'])
async def tmp_tmp_abc_user_profile(ctx, arg):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Whale/2.8.105.22 Safari/537.36'}
    url = f"https://truckersmp.com/user/{arg}"
    soup = create_soup(url, headers)
    #플레이어 정보
    user_status = soup.find("div", attrs={"class":"profile-bio"})
    name = user_status.find_all("span")[0].get_text().strip()
    check = user_status.find_all("strong")[0].get_text()
    if check == "Also known as":
        steam = user_status.find_all("span")[3].get_text().strip().replace("Steam ID:", "")
        birt = user_status.find_all("span")[5].get_text().strip().replace("Member since:", "")
        bans = user_status.find_all("span")[6].get_text().strip().replace("Active bans:", "")
    else:
        steam = user_status.find_all("span")[2].get_text().strip().replace("Steam ID:", "")
        birt = user_status.find_all("span")[4].get_text().strip().replace("Member since:", "")
        bans = user_status.find_all("span")[5].get_text().strip().replace("Active bans:", "")

    vtc_check = soup.find_all("h2", attrs={"class":"panel-title heading-sm pull-left"})[2].get_text()
    if vtc_check == " VTC":
        vtc_find = soup.find_all("div", attrs={"class":"panel panel-profile"})[2]
        vtc_name =  vtc_find.find("h5", attrs={"class":"text-center break-all"}).get_text().strip()
    else:
        vtc_name = "없음"

    #프로필 이미지
    img = soup.find_all("div", attrs={"class": "col-md-3 md-margin-bottom-40"})[0]
    imgs = img.find("img", attrs={"class": "img-responsive profile-img margin-bottom-20 shadow-effect-1"})
    prof_image = imgs.get("src")

    embed = discord.Embed(title = f"[TruckersMP] {arg}'s 프로필", colour = 0x2EFEF7)
    embed.add_field(name = 'Name', value = f"{name}", inline = False)
    embed.add_field(name = 'Steam ID', value = f"{steam}", inline = False)
    embed.add_field(name = 'Member since', value = f"{birt}", inline = False)
    embed.add_field(name = 'Active bans', value = f"{bans}", inline = False)
    embed.add_field(name = 'VTC', value = f"{vtc_name}", inline = False)
    embed.set_thumbnail(url=prof_image)
    await ctx.channel.send(embed = embed)
	
@client.command(aliases=['==번역'])
async def _translator_abc(ctx, arg, *, content):
    content = str(content)
    if arg[0] == '한':
        langso = "Korean"
        so = "ko"
    elif arg[0] == '영':
        langso = "English"
        so = "en"
    elif arg[0] == '일':
        langso = "Japanese"
        so = "ja"
    elif arg[0] == '중':
        langso = "Chinese"
        so = "zh-CN"
    else:
        pass
    if arg[1] == '한':
        langta = "Korean"
        ta = "ko"
    elif arg[1] == '영':
        langta = "English"
        ta = "en"
    elif arg[1] == '일':
        langta = "Japanese"
        ta = "ja"
    elif arg[1] == '중':
        langta = "Chinese"
        ta = "zh-CN"
    else:
        pass
    url = "https://openapi.naver.com/v1/papago/n2mt"
    #띄어쓰기 : split처리후 [1:]을 for문으로 붙인다.
    trsText = str(content)
    try:
        if len(trsText) == 1:
            await ctx.channel.send("단어 혹은 문장을 입력해주세요.")
        else:
            trsText = trsText[0:]
            combineword = ""
            for word in trsText:
                combineword += "" + word
            sourcetext = combineword.strip()
            combineword = quote(sourcetext)
            dataParmas = f"source={so}&target={ta}&text=" + combineword
            request = Request(url)
            request.add_header("X-Naver-Client-Id", client_id)
            request.add_header("X-Naver-Client-Secret", client_secret)
            response = urlopen(request, data=dataParmas.encode("utf-8"))
            responsedCode = response.getcode()
            if (responsedCode == 200):
                response_body = response.read()
                # response_body -> byte string : decode to utf-8
                api_callResult = response_body.decode('utf-8')
                # JSON data will be printed as string type. So need to make it back to type JSON(like dictionary)
                api_callResult = json.loads(api_callResult)
                #번역 결과
                translatedText = api_callResult['message']['result']["translatedText"]
                embed = discord.Embed(title=f"번역 ┃ {langso} → {langta}", description="", color=0x2e9fff)
                embed.add_field(name=f"{langso}", value=sourcetext, inline=False)
                embed.add_field(name=f"{langta}", value=translatedText, inline=False)
                embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/740877681209507880/755471340227526706/papago_og.png")
                embed.set_footer(text="Provided by Naver Open API",
                                 icon_url='https://cdn.discordapp.com/attachments/740877681209507880/755471340227526706/papago_og.png')
                await ctx.channel.send(embed=embed)
            else:
                await ctx.channel.send("Error Code : " + responsedCode)
    except HTTPError as e:
        await ctx.channel.send("번역 실패. HTTP에러 발생.")

@client.command(pass_context = True, aliases=['==유튜브'])
async def _youtube_abc_search(ctx, * , arg):
    arg_title = str(arg)
    arg = str(arg).replace(" ", "%20")
    DEVELOPER_KEY = os.environ["DEVELOPER_KEY"]
    YOUTUBE_API_SERVICE_NAME="youtube"
    YOUTUBE_API_VERSION="v3"
    youtube = build(YOUTUBE_API_SERVICE_NAME,YOUTUBE_API_VERSION,developerKey=DEVELOPER_KEY)
    search_response = youtube.search().list(
        q = f"{arg_title}",
        order = "relevance",
        part = "snippet",
        maxResults = 6
        ).execute()

    thumbnail_img = search_response['items'][1]['snippet']['thumbnails']['high']['url']
    title1 = search_response['items'][1]['snippet']['title'].replace('&quot;', '"').replace("&#39;", "'")
    title2 = search_response['items'][2]['snippet']['title'].replace('&quot;', '"').replace("&#39;", "'")
    title3 = search_response['items'][3]['snippet']['title'].replace('&quot;', '"').replace("&#39;", "'")
    title4 = search_response['items'][4]['snippet']['title'].replace('&quot;', '"').replace("&#39;", "'")
    title5 = search_response['items'][5]['snippet']['title'].replace('&quot;', '"').replace("&#39;", "'")
    link = "https://www.youtube.com/watch?v="
    link1 = link + search_response['items'][1]['id']['videoId']
    link2 = link + search_response['items'][2]['id']['videoId']
    link3 = link + search_response['items'][3]['id']['videoId']
    link4 = link + search_response['items'][4]['id']['videoId']
    link5 = link + search_response['items'][5]['id']['videoId']
    url = f"https://www.youtube.com/results?search_query={arg}"


    embed = discord.Embed(title = f":movie_camera: {arg_title} 검색 결과", colour = 0xb30e11)
    embed.set_author(name = '더보기', url = url)
    embed.add_field(name = "\n\u200b", value = f'**1. [{title1}]({link1})**', inline = False)
    embed.add_field(name = "\n\u200b", value = f'**2. [{title2}]({link2})**', inline = False)
    embed.add_field(name = "\n\u200b", value = f'**3. [{title3}]({link3})**', inline = False)
    embed.add_field(name = "\n\u200b", value = f'**4. [{title4}]({link4})**', inline = False)
    embed.add_field(name = "\n\u200b", value = f'**5. [{title5}]({link5})**\n\u200b', inline = False)
    embed.set_thumbnail(url=thumbnail_img)
    embed.set_footer(text='Provided by Youtube API')
    await ctx.channel.send(embed = embed)
	
access_token = os.environ["BOT_TOKEN"]
client.run(access_token)
