【问题标题】:Python Discord Bot isn't joinig voice channelPython Discord Bot 未加入语音频道
【发布时间】: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 教程,但正如我所说,它没有用。

【问题讨论】:

    标签: python discord discord.py


    【解决方案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!")
    

    而您的 joindisconnect 函数看起来好像属于一个类(注意 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()
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 1970-01-01
      • 2020-11-04
      • 2020-11-03
      • 2020-06-26
      • 2020-12-21
      • 2017-06-22
      • 1970-01-01
      • 2020-07-16
      相关资源
      最近更新 更多