Python discord bot connect to voice channel

Discord Bot Join Voice Channel

I want my Discord bot to join Voice Channel, but I am running into a problem that whenever I want it to join VC nothing happens — even no errors. I have tried other solutions from SO/Git but none of them worked for me (one of them below):

client = commands.Bot(command_prefix=get_prefix) @client.command() async def join(ctx): channel = ctx.author.voice.channel await channel.connect() 

2 Answers 2

channel = client.get_channel(ctx.author.voice.channel.id) await channel.connect() 

I know you have already solved your issue, but I’ll show you how I solved it anyway, especially if you ever use @client.event .

I’m not sure if this will work, but have a try. Firstly, I went into my terminal and used pip install PyNaCl . Then I used await ctx.author.voice.channel.connect()

From there, you can play your audio file. Though first, I’d install ffmpeg just in case. Just keep this in mind, but anyways, congratulations on finding a solution!

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to Make a Discord Bot Join a Voice Channel and Play an Audio File When a Member Joins the Channel Using Discord.py Cogs

I am currently trying to make a cog for a Discord bot in Python 3 that, when running, plays a specific audio file when someone joins a specific Discord voice channel.

My Problem

  1. Convert it to a cog, and
  2. Make it work for a specific voice channel, and not every one in the Discord server.
import discord from discord.ext import commands from time import sleep rpgmusicpath = r"C:\Users\lones\OneDrive\Desktop\Bot\music\rpgmusic.mp3" class VoiceChannelIntro(commands.Cog): def __init__(self, client): self.bot = client @commands.Cog.listener() async def on_ready(self): print('Channel Intro cog successfully loaded.') @commands.Cog.event async def on_voice_state_update(member: discord.Member, before, after): #replace this with the path to your audio file path = r"path\to\music.mp3" vc_before = before.channel vc_after = after.channel if vc_before == vc_after: return elif vc_before is None: channel = member.voice.channel vc = await channel.connect() vc.play(discord.FFmpegPCMAudio(path)) with audioread.audio_open(path) as f: #Start Playing sleep(f.duration) await vc.disconnect() elif vc_after is None: return else: channel = member.voice.channel vc = await channel.connect() vc.play(discord.FFmpegPCMAudio(path)) with audioread.audio_open(path) as f: #Start Playing sleep(f.duration) await vc.disconnect() def setup(bot): bot.add_cog(VoiceChannelIntro(bot)) 

Источник

Читайте также:  Html tables with fixed headers

Python discord bot — join voice channel

I’m interested in making a discord bot using python. Im trying to make a bot that could join voice channel. This is my code, I already ran it but it’s not working. I already did some research on the internet, but I think this code is completely fine?

 #Join Command @client.command() async def join(ctx): if(ctx.author.voice): #If in voice channel channel = ctx.author.voice.channel await channel.connect() else: #If not in voice channel await ctx.send("You must be in voice channel first !") 

1 Answer 1

@bot.command(name='join', invoke_without_subcommand=True) async def join(ctx): 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() await ctx.send(f"Joined Voice Channel") 

this is how we can join a discord bot to a voice channel.

destination is where the person already connected and want to add bot to that channel.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to make a bot join a voice channel?

When I say !join or join , it doesn’t join at all. I have tried adding name in the @bot.command, but it still does not work. I have been trying this for over a week. At the bottom, I also added client.run, but I didn’t put it in as it will reveal the bot. What I want it to basically do is, when I say %join and in a voice channel, I want it to join the channel I’m in, but if I’m not in a channel, it will say to join a voice channel. When I say %leave , I want it to leave the voice chat.

import os import random import discord from dotenv import load_dotenv import spotipy global counter import asyncio from discord.ext.commands import Bot bot = Bot("!") counter = 0 global bonercounter bonercounter = 0 global randomhardness randomhardness = random.randint(0, 100) load_dotenv() TOKEN = os.getenv('TOKEN') urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu' sp = spotipy.Spotify() client = discord.Client() @client.event async def on_ready(): print(f' has connected to Discord!') @client.event async def on_member_join(member): await member.create_dm() global name name = member.name await member.dm_channel.send( f'Hi , welcome to my Discord server!') @client.event async def on_message(message): global userID userID = message.author.id userID = (f">") if message.author == client.user: return if message.content == '%69': #UserID = '@229122227767869440' #await message.channel.send(f'Nice ') await message.channel.send(f'Nice job ') if message.content == '%gif': global randomgif randomgif = random.randint(0, 7) if randomgif == 0: await message.channel.send("http://gph.is/1sx2Uad") if randomgif == 1: await message.channel.send( "https://media.giphy. com/media/SggILpMXO7Xt6/giphy.gif") if randomgif == 2: await message.channel.send( "https://media.giphy. com/media/Y4WDXbagwPoepikUdJ/giphy.gif") if randomgif == 3: await message.channel.send( "https://media.giphy. com/media/mUrBX1TF0kCRi/giphy.gif") if randomgif == 4: await message.channel.send( "https://media.giphy. com/media/1wqqlaQ7IX3TXibXZE/giphy.gif") if randomgif == 5: await message.channel.send( "https://media.giphy. com/media/Mtqip7Jor0DgAvzn6U/giphy.gif") if randomgif == 6: await message.channel.send( "https://media.giphy. com/media/as521kub4b68hW2JhK/giphy.gif") if randomgif == 7: await message.channel.send( "https://media.giphy. com/media/JIX9t2j0ZTN9S/giphy.gif") if message.content == '%meme': global randomimagememe randomimagememe = random.randint(0, 9) if randomimagememe == 0: await message.channel.send ("https://i.redd.it/kaojpi0grh871.jpg") if randomimagememe == 1: await message.channel.send ("https://i.redd.it/w1dz6q9lmh871.jpg") if randomimagememe == 2: await message.channel.send ("https://i.redd.it/ssmmlv7i3k771.jpg") if randomimagememe == 3: await message.channel.send ("https://i.redd.it/p3f3euk5fe871.png") if randomimagememe == 4: await message.channel.send ("https://i.redd.it/mywojo9yee871.jpg") if randomimagememe == 5: await message.channel.send ("https://i.redd.it/2mzj12xdff671.jpg") if randomimagememe == 6: await message.channel.send ("https://i.redd.it/537ffquizr671.png") if randomimagememe == 7: await message.channel.send ("https://i.redd.it/y2r395p0n0571.png") if randomimagememe == 8: await message.channel.send( "https://preview.redd.it/m7e4r4b3ft571.gif? format=mp4& s=27cdbd5e93b3b5b4a62ca45859a74a0a129721e3" ) if randomimagememe == 9: await message.channel.send ("https://i.redd.it/5sht2b8tgf671.jpg") #if message.content == '%song' if message.content == '%coom': global counter counter = counter + 1 UserID = "@860684625763368990" await message.channel.send(f' has coomed times!') if message.content == '%boner': global bonercounter bonercounter = bonercounter + 1 UserID = '@229122227767869440' await message.channel.send( f' has had a boner times!') if message.content == '%bonercheck': global randomhardness randomhardness = random.randint(0, 100) await message.channel.send(f" is % hard") if message.content == 'Hi': await message.channel.send(f"Hello there ") if message.content == "%commands": await message.channel.send("hi,69,coom,boner, bonercheck,meme,gif") ##--Voice Chat Functions--## @bot.command(aliases=['jvc']) async def joinvc(ctx): try: channel = ctx.message.author.voice.channel except AttributeError: await ctx.send("You're Not in a Voice Chat Lmao :joy:" ) global voice voice = client.get(client.voice_clients, guild=ctx.guild) voice = await channel.connect(timeout=3600) @bot.command(aliases=["lvc"]) async def leavevc(ctx): global voice channel = ctx.message.author.voice.channel voice = await voice.disconnect() 

Источник

Оцените статью