matthileo

Untitled

Apr 22nd, 2023
1,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. import io
  2. import discord
  3. import asyncio
  4. from discord.ext import commands
  5. from discord import FFmpegPCMAudio
  6. from elevenlabslib import ElevenLabsUser, ElevenLabsVoice
  7. from elevenlabslib.helpers import save_bytes_to_path
  8. from gtts import gTTS
  9. from gtts.lang import tts_langs
  10. from tempfile import TemporaryFile
  11.  
  12. bot = commands.Bot(command_prefix="$", intents=discord.Intents.all())
  13. TOKEN = ''
  14. ELEVEN_API_KEY = ""
  15. VOICECHANNEL =
  16. VOICETEXTCHANNEL =
  17. TTSDISCORDROLE = "TTS"
  18. vc = None
  19. eleven = ElevenLabsUser(ELEVEN_API_KEY)
  20. voice: ElevenLabsVoice = eleven.get_voices_by_name("Rachel")[0]
  21.  
  22.  
  23. async def play_audio(audio):
  24.     vc.play(audio)
  25.     while vc.is_playing():
  26.         await asyncio.sleep(1)
  27.     vc.stop()
  28.  
  29. def voice_to_text(message, user_id, useEleven=False):
  30.     text = message
  31.     try:
  32.         if useEleven:
  33.             data = voice.generate_audio_bytes(text, 0.5)
  34.             save_bytes_to_path("tts.mp3", data)
  35.         else:
  36.             accent = "us"
  37.             tts = gTTS(text=text, lang='en',tld=accent)
  38.             tts.save('tts.mp3')
  39.         audio = FFmpegPCMAudio(executable="D:/Google Drive/Scripts/ffmpeg/bin/ffmpeg.exe",source='tts.mp3')
  40.         return audio
  41.     except Exception as e:
  42.         print(f"Error transcribing audio: {e}")
  43.     except:
  44.         print("other error")
  45.  
  46. @bot.event
  47. async def on_ready():
  48.     global vc
  49.     channel = bot.get_channel(VOICECHANNEL)
  50.     vc = await channel.connect()
  51.  
  52. @bot.event
  53. async def on_message(message):
  54.     # Ignore messages that are commands
  55.     if message.content.startswith(bot.command_prefix):
  56.         await bot.process_commands(message)
  57.         return
  58.     # Process non-command messages
  59.     tts_role = discord.utils.get(message.guild.roles, name=TTSDISCORDROLE)
  60.     if message.channel.id == VOICETEXTCHANNEL and tts_role in message.author.roles:
  61.         audio = voice_to_text(message.content, message.author.id)
  62.         await play_audio(audio)
  63.  
  64.  
  65. @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.")
  66. async def tts(ctx, arg=None):
  67.     try:
  68.         # Get the TTS role
  69.         tts_role = discord.utils.get(ctx.guild.roles, name=TTSDISCORDROLE)
  70.  
  71.         # If no argument is provided, check if the user has the TTS role
  72.         if arg is None:
  73.             if tts_role in ctx.author.roles:
  74.                 await ctx.send(f"{ctx.author.mention}, you have the TTS role.")
  75.             else:
  76.                 await ctx.send(f"{ctx.author.mention}, you do not have the TTS role.")
  77.         # If "remove" argument is provided, remove the TTS role from the user
  78.         elif arg == "remove":
  79.             if tts_role in ctx.author.roles:
  80.                 await ctx.author.remove_roles(tts_role)
  81.                 await ctx.send(f"{ctx.author.mention}, the TTS role has been removed.")
  82.             else:
  83.                 await ctx.send(f"{ctx.author.mention}, you do not have the TTS role.")
  84.         # If "add" argument is provided, add the TTS role to the user
  85.         elif arg == "add":
  86.             if tts_role in ctx.author.roles:
  87.                 await ctx.send(f"{ctx.author.mention}, you already have the TTS role.")
  88.             else:
  89.                 await ctx.author.add_roles(tts_role)
  90.                 await ctx.send(f"{ctx.author.mention}, the TTS role has been added.")
  91.     except discord.Forbidden as e:
  92.         await ctx.send(f"I do not have permission to perform that action: {e}")
  93.     except Exception as e:
  94.         await ctx.send(f"An error occurred: {e}")
  95.  
  96. bot.run(TOKEN)
  97.  
Advertisement
Add Comment
Please, Sign In to add comment