Python Discord Bot 未加入语音频道
Posted
技术标签:
【中文标题】Python Discord Bot 未加入语音频道【英文标题】:Python Discord Bot isn't joinig voice channel 【发布时间】:2022-01-23 23:45:11 【问题描述】:import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
@client.event
async def on_ready():
print("I'm logged in as 0.user.".format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
机器人不加入我不知道我做错了什么。它应该只加入我的语音频道。我看了this 教程,但正如我所说,它没有用。
【问题讨论】:
【参考方案1】:你有点混淆了隐喻:你的 on_message
是一个简单的函数:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
而您的 join
和 disconnect
函数看起来好像属于一个类(注意 self
参数):
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
如果您从这些函数中删除 self
参数,您应该能够使其正常工作:
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
@commands.command()
async def disconnect(ctx):
await ctx.voice_client.disconnect()
【讨论】:
以上是关于Python Discord Bot 未加入语音频道的主要内容,如果未能解决你的问题,请参考以下文章
使用 python Discord bot 播放 Youtube 音频
如何让基于 Python 的 Discord Bot 随机播放不同的声音?