Cameron Butcher
Cameron Butcher

Reputation: 29

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'{client.user.name} 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 {member.name}, welcome to my Discord server!')
@client.event
async def on_message(message):
    global userID
    userID = message.author.id
    userID = (f"<@{userID}>")
    if message.author == client.user:
        return
    if message.content == '%69':
        #UserID = '@229122227767869440'
        #await message.channel.send(f'Nice {UserID}')
        await message.channel.send(f'Nice job {userID}')
    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'{UserID} has coomed 
{counter} times!')
    if message.content == '%boner':
        global bonercounter
        bonercounter = bonercounter + 1
        UserID = '@229122227767869440'
        await message.channel.send(
            f'{UserID} has had a boner {bonercounter} times!')
    if message.content == '%bonercheck':
        global randomhardness
        randomhardness = random.randint(0, 100)
        await message.channel.send(f"{userID} is 
{randomhardness}% hard")
    if message.content == 'Hi':
        await message.channel.send(f"Hello there {userID}")
    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()

Upvotes: 1

Views: 1230

Answers (1)

swift user12321
swift user12321

Reputation: 68


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'{client.user.name} 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 {member.name}, welcome to my Discord server!')
@client.event
async def on_message(message):
    global userID
    userID = message.author.id
    userID = (f"<@{userID}>")
    if message.author == client.user:
        return
    if message.content == '%69':
        #UserID = '@229122227767869440'
        #await message.channel.send(f'Nice {UserID}')
        await message.channel.send(f'Nice job {userID}')
    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'{UserID} has coomed 
{counter} times!')
    if message.content == '%boner':
        global bonercounter
        bonercounter = bonercounter + 1
        UserID = '@229122227767869440'
        await message.channel.send(
            f'{UserID} has had a boner {bonercounter} times!')
    if message.content == '%bonercheck':
        global randomhardness
        randomhardness = random.randint(0, 100)
        await message.channel.send(f"{userID} is 
{randomhardness}% hard")
    if message.content == 'Hi':
        await message.channel.send(f"Hello there {userID}")
    if message.content == "%commands":
        await message.channel.send("hi,69,coom,boner,
bonercheck,meme,gif")
##--Voice Chat
@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()
@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()

So if you do the prefix join it will join the vc you are in and if you do the prefix leave it will disconnect For example >join, >leave

This wont detect if a user is in a vc though (that wasn’t your question) it will just join a leave

Hopefully this works

Upvotes: 1

Related Questions