如何让机器人加入语音频道?

时间:2021-07-03 08:32:29

标签: python discord discord.py

当我说 !joinjoin 时,它根本不加入。我已经尝试在@bot.command 中添加名称,但它仍然不起作用。我已经试了一个多星期了。

在底部,我还添加了 client.run,但我没有将其放入,因为它会显示机器人。我想让它基本上做的是,当我说 %join 并在语音频道中时,我希望它加入我所在的频道,但如果我不在频道中,它会说加入一个语音通道。当我说 %leave 时,我希望它离开语音聊天。

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()

1 个答案:

答案 0 :(得分:1)


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()

因此,如果您执行前缀连接,它将加入您所在的 vc,如果您执行前缀离开,它将断开连接 例如>加入,>离开

这不会检测用户是否在 vc 中(这不是您的问题)它只会加入休假

希望这有效