Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nextcord, os, json, asyncio
- if not os.path.exists("config.json"):
- f = open("config.json","w")
- f.write("""{
- "bot_token": "",
- "server_id": ,
- "voice_channel_id": ,
- "play_file_path": "",
- "ffmpeg_path": ""
- }""")
- f.close()
- print("Please fill out the config in config.json!")
- else:
- #config = json.load("config.json")
- with open("config.json","r") as f:
- config = json.load(f)
- global bot_token, server, vc_id, song_file, ffmpeg_path
- bot_token = config["bot_token"]
- server = config["server_id"]
- vc_id = config["voice_channel_id"]
- song_file = config["play_file_path"]
- ffmpeg_path = config["ffmpeg_path"]
- config = None
- async def join_vc(guild:nextcord.Guild, vc:nextcord.VoiceChannel):
- voice_client = await vc.connect(timeout=15.0) #type: ignore
- await guild.change_voice_state(self_deaf=True, channel=vc) #type: ignore
- return voice_client
- async def play_song_loop(voice_client: nextcord.VoiceClient):
- while True:
- try:
- if not voice_client.is_connected():
- print("Bot is not connected to a voice channel. Reconnecting...")
- guild = voice_client.guild
- vc = guild.get_channel(vc_id) # type: ignore
- voice_client = await join_vc(guild, vc) # type: ignore
- if not voice_client:
- print("Failed to reconnect to voice channel.")
- return
- voice_client.play(nextcord.FFmpegPCMAudio(executable=ffmpeg_path, source=song_file))
- while voice_client.is_playing():
- await asyncio.sleep(1)
- except Exception as e:
- print(f"Error playing song: {e}")
- finally:
- if not voice_client.is_connected():
- print("Bot got disconnected from the voice channel. Reconnecting...")
- guild = voice_client.guild
- vc = guild.get_channel(vc_id) # type: ignore
- voice_client = await join_vc(guild, vc) # type: ignore
- if not voice_client:
- print("Failed to reconnect to voice channel.")
- return
- client = nextcord.Client()
- @client.event
- async def on_ready():
- '''nextcord.opus.load_opus("""/opt/homebrew/Cellar/opus/1.4/lib/libopus.dylib""")
- if not nextcord.opus.is_loaded():
- raise RunTimeError('Opus failed to load')'''
- print("Bot is online, connecting to voice channel with id: "+str(vc_id))
- await client.change_presence(activity=nextcord.Activity(name="Tuning..."))
- guild = client.get_guild(server)
- vc = guild.get_channel(vc_id) #type: ignore
- voice_client = await join_vc(guild,vc)#type: ignore
- print(f'Connected to voice channel "{voice_client.channel}", playing {song_file}...')
- await play_song_loop(voice_client)
- client.run(bot_token)
Advertisement
Add Comment
Please, Sign In to add comment