如何让音乐机器人加入语音通话
How to make a music bot join the voice call
所以我看了很多 youtube vids 并得到了一个工作代码
但是我在我的代码中找不到错误
import discord
from discord import Embed
from itertools import cycle
import os
import asyncio
import random
import json
import youtube_dl
import praw
from datetime import datetime as dt
from typing import Optional
from discord.ext import tasks,commands
from discord.ext.commands import Bot
from discord.utils import get
from keep_alive import keep_alive
def getprefix(bot,msg):
return['d! ','d!','doge ','doge']
bot = commands.Bot(case_insensitive=True,command_prefix=getprefix)
bot.remove_command("help")
status = cycle(['Prefix=d!','Helping server','Prefix=doge'])
@bot.event
async def on_ready():
print('on')
change_status.start()
@tasks.loop(seconds=10)
async def change_status():
await bot.change_presence(status=discord.Status.idle,activity=discord.Streaming(name=(next(status)), url='https://www.youtube.com/watch?v=iik25wqIuFo'))
@bot.command(aliases=['j','enter','connect'])
async def join(ctx):
if ctx.author.voice is None:
em=discord.Embed(title="ERROR",description=f"{ctx.author.mention} You are not in a voice channel!")
await ctx.send(embed=em)
voice_channel=ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
em=discord.Embed(description=f"Connected")
await ctx.send(embed=em)
else:
await ctx.voice_client.move_to(voice_channel)
em=discord.Embed(description=f"Reconnected")
await ctx.send(embed=em)
@bot.command(aliases=['l','exit','leave'])
async def disconnect(ctx):
await ctx.voice_client.disconnect()
@bot.command(aliases=['stop','halt','pau'])
async def pause(ctx):
await ctx.voice_client.pause()
em=discord.Embed(description=f"Paused! {ctx.author.name}")
await ctx.send(embed=em)
@bot.command(aliases=['continue','res'])
async def resume(ctx):
await ctx.voice_client.resume()
em=discord.Embed(description=f"Resumed! {ctx.author.name}")
await ctx.send(embed=em)
@bot.command(aliases=['run','display'])
async def play(ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS={'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'-vn'}
YDL_OPTIONS={'format':'bestaudio'}
vc=ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info=ydl.extract_info(url,download=False)
url2=info['formats'][0]['url']
source=await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
vc.play(source)
因此,如果我不在语音频道中,机器人会显示错误,但当我在语音频道中时,它不会加入,也不会发送任何消息。
所以我无法弄清楚我的错误,我真的不知道其余的是否会起作用,因为第一步本身就出错了。
任何帮助都是有价值的
尝试替换
if ctx.voice_client is None:
与
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice == None:
await voice_channel.connect()
或者:
async def join(self, ctx):
member = utils.find(lambda m: m.id == ctx.author.id, ctx.guild.members)
if member is not None and member.voice is not None:
vc = member.voice.channel
await vc.connect()
print('Joined a Channel')
所以我看了很多 youtube vids 并得到了一个工作代码 但是我在我的代码中找不到错误
import discord
from discord import Embed
from itertools import cycle
import os
import asyncio
import random
import json
import youtube_dl
import praw
from datetime import datetime as dt
from typing import Optional
from discord.ext import tasks,commands
from discord.ext.commands import Bot
from discord.utils import get
from keep_alive import keep_alive
def getprefix(bot,msg):
return['d! ','d!','doge ','doge']
bot = commands.Bot(case_insensitive=True,command_prefix=getprefix)
bot.remove_command("help")
status = cycle(['Prefix=d!','Helping server','Prefix=doge'])
@bot.event
async def on_ready():
print('on')
change_status.start()
@tasks.loop(seconds=10)
async def change_status():
await bot.change_presence(status=discord.Status.idle,activity=discord.Streaming(name=(next(status)), url='https://www.youtube.com/watch?v=iik25wqIuFo'))
@bot.command(aliases=['j','enter','connect'])
async def join(ctx):
if ctx.author.voice is None:
em=discord.Embed(title="ERROR",description=f"{ctx.author.mention} You are not in a voice channel!")
await ctx.send(embed=em)
voice_channel=ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
em=discord.Embed(description=f"Connected")
await ctx.send(embed=em)
else:
await ctx.voice_client.move_to(voice_channel)
em=discord.Embed(description=f"Reconnected")
await ctx.send(embed=em)
@bot.command(aliases=['l','exit','leave'])
async def disconnect(ctx):
await ctx.voice_client.disconnect()
@bot.command(aliases=['stop','halt','pau'])
async def pause(ctx):
await ctx.voice_client.pause()
em=discord.Embed(description=f"Paused! {ctx.author.name}")
await ctx.send(embed=em)
@bot.command(aliases=['continue','res'])
async def resume(ctx):
await ctx.voice_client.resume()
em=discord.Embed(description=f"Resumed! {ctx.author.name}")
await ctx.send(embed=em)
@bot.command(aliases=['run','display'])
async def play(ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS={'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'-vn'}
YDL_OPTIONS={'format':'bestaudio'}
vc=ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info=ydl.extract_info(url,download=False)
url2=info['formats'][0]['url']
source=await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
vc.play(source)
因此,如果我不在语音频道中,机器人会显示错误,但当我在语音频道中时,它不会加入,也不会发送任何消息。 所以我无法弄清楚我的错误,我真的不知道其余的是否会起作用,因为第一步本身就出错了。 任何帮助都是有价值的
尝试替换
if ctx.voice_client is None:
与
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice == None:
await voice_channel.connect()
或者:
async def join(self, ctx):
member = utils.find(lambda m: m.id == ctx.author.id, ctx.guild.members)
if member is not None and member.voice is not None:
vc = member.voice.channel
await vc.connect()
print('Joined a Channel')