Guest User

Untitled

a guest
Dec 16th, 2023
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | Source Code | 0 0
  1. import nextcord, os, json, asyncio
  2.  
  3. if not os.path.exists("config.json"):
  4. f = open("config.json","w")
  5. f.write("""{
  6. "bot_token": "",
  7. "server_id": ,
  8. "voice_channel_id": ,
  9. "play_file_path": "",
  10. "ffmpeg_path": ""
  11. }""")
  12. f.close()
  13. print("Please fill out the config in config.json!")
  14. else:
  15. #config = json.load("config.json")
  16. with open("config.json","r") as f:
  17. config = json.load(f)
  18. global bot_token, server, vc_id, song_file, ffmpeg_path
  19. bot_token = config["bot_token"]
  20. server = config["server_id"]
  21. vc_id = config["voice_channel_id"]
  22. song_file = config["play_file_path"]
  23. ffmpeg_path = config["ffmpeg_path"]
  24. config = None
  25.  
  26. async def join_vc(guild:nextcord.Guild, vc:nextcord.VoiceChannel):
  27. voice_client = await vc.connect(timeout=15.0) #type: ignore
  28. await guild.change_voice_state(self_deaf=True, channel=vc) #type: ignore
  29. return voice_client
  30.  
  31.  
  32. async def play_song_loop(voice_client: nextcord.VoiceClient):
  33. while True:
  34. try:
  35. if not voice_client.is_connected():
  36. print("Bot is not connected to a voice channel. Reconnecting...")
  37. guild = voice_client.guild
  38. vc = guild.get_channel(vc_id) # type: ignore
  39. voice_client = await join_vc(guild, vc) # type: ignore
  40. if not voice_client:
  41. print("Failed to reconnect to voice channel.")
  42. return
  43.  
  44. voice_client.play(nextcord.FFmpegPCMAudio(executable=ffmpeg_path, source=song_file))
  45. while voice_client.is_playing():
  46. await asyncio.sleep(1)
  47.  
  48. except Exception as e:
  49. print(f"Error playing song: {e}")
  50.  
  51. finally:
  52. if not voice_client.is_connected():
  53. print("Bot got disconnected from the voice channel. Reconnecting...")
  54. guild = voice_client.guild
  55. vc = guild.get_channel(vc_id) # type: ignore
  56. voice_client = await join_vc(guild, vc) # type: ignore
  57. if not voice_client:
  58. print("Failed to reconnect to voice channel.")
  59. return
  60.  
  61. client = nextcord.Client()
  62.  
  63. @client.event
  64. async def on_ready():
  65. '''nextcord.opus.load_opus("""/opt/homebrew/Cellar/opus/1.4/lib/libopus.dylib""")
  66. if not nextcord.opus.is_loaded():
  67. raise RunTimeError('Opus failed to load')'''
  68. print("Bot is online, connecting to voice channel with id: "+str(vc_id))
  69. await client.change_presence(activity=nextcord.Activity(name="Tuning..."))
  70. guild = client.get_guild(server)
  71. vc = guild.get_channel(vc_id) #type: ignore
  72. voice_client = await join_vc(guild,vc)#type: ignore
  73. print(f'Connected to voice channel "{voice_client.channel}", playing {song_file}...')
  74. await play_song_loop(voice_client)
  75.  
  76.  
  77. client.run(bot_token)
Advertisement
Add Comment
Please, Sign In to add comment