Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io
- import discord
- import asyncio
- from discord.ext import commands
- from discord import FFmpegPCMAudio
- from elevenlabslib import ElevenLabsUser, ElevenLabsVoice
- from elevenlabslib.helpers import save_bytes_to_path
- from gtts import gTTS
- from gtts.lang import tts_langs
- from tempfile import TemporaryFile
- bot = commands.Bot(command_prefix="$", intents=discord.Intents.all())
- TOKEN = ''
- ELEVEN_API_KEY = ""
- VOICECHANNEL =
- VOICETEXTCHANNEL =
- TTSDISCORDROLE = "TTS"
- vc = None
- eleven = ElevenLabsUser(ELEVEN_API_KEY)
- voice: ElevenLabsVoice = eleven.get_voices_by_name("Rachel")[0]
- async def play_audio(audio):
- vc.play(audio)
- while vc.is_playing():
- await asyncio.sleep(1)
- vc.stop()
- def voice_to_text(message, user_id, useEleven=False):
- text = message
- try:
- if useEleven:
- data = voice.generate_audio_bytes(text, 0.5)
- save_bytes_to_path("tts.mp3", data)
- else:
- accent = "us"
- tts = gTTS(text=text, lang='en',tld=accent)
- tts.save('tts.mp3')
- audio = FFmpegPCMAudio(executable="D:/Google Drive/Scripts/ffmpeg/bin/ffmpeg.exe",source='tts.mp3')
- return audio
- except Exception as e:
- print(f"Error transcribing audio: {e}")
- except:
- print("other error")
- @bot.event
- async def on_ready():
- global vc
- channel = bot.get_channel(VOICECHANNEL)
- vc = await channel.connect()
- @bot.event
- async def on_message(message):
- # Ignore messages that are commands
- if message.content.startswith(bot.command_prefix):
- await bot.process_commands(message)
- return
- # Process non-command messages
- tts_role = discord.utils.get(message.guild.roles, name=TTSDISCORDROLE)
- if message.channel.id == VOICETEXTCHANNEL and tts_role in message.author.roles:
- audio = voice_to_text(message.content, message.author.id)
- await play_audio(audio)
- @bot.command(name="tts",help="Add or remove the tts roll.\nUse '$tts add' to give yourself the tts role.\nUse '$tts remove' to remove the tts role.")
- async def tts(ctx, arg=None):
- try:
- # Get the TTS role
- tts_role = discord.utils.get(ctx.guild.roles, name=TTSDISCORDROLE)
- # If no argument is provided, check if the user has the TTS role
- if arg is None:
- if tts_role in ctx.author.roles:
- await ctx.send(f"{ctx.author.mention}, you have the TTS role.")
- else:
- await ctx.send(f"{ctx.author.mention}, you do not have the TTS role.")
- # If "remove" argument is provided, remove the TTS role from the user
- elif arg == "remove":
- if tts_role in ctx.author.roles:
- await ctx.author.remove_roles(tts_role)
- await ctx.send(f"{ctx.author.mention}, the TTS role has been removed.")
- else:
- await ctx.send(f"{ctx.author.mention}, you do not have the TTS role.")
- # If "add" argument is provided, add the TTS role to the user
- elif arg == "add":
- if tts_role in ctx.author.roles:
- await ctx.send(f"{ctx.author.mention}, you already have the TTS role.")
- else:
- await ctx.author.add_roles(tts_role)
- await ctx.send(f"{ctx.author.mention}, the TTS role has been added.")
- except discord.Forbidden as e:
- await ctx.send(f"I do not have permission to perform that action: {e}")
- except Exception as e:
- await ctx.send(f"An error occurred: {e}")
- bot.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment