【问题标题】:(Discord py) Music bot does not take any commands(Discord py) 音乐机器人不接受任何命令
【发布时间】:2021-10-03 06:14:04
【问题描述】:

这是我第一次在这里发帖,如果有什么遗漏,我很抱歉。到目前为止,我已经设置了三个命令(加入、播放、离开)以使其正常工作。我花了几个小时来解决这个机器人的问题,因为它没有接受任何命令。任何帮助,将不胜感激!已经谢谢了。

所以每当有人给机器人一个命令时,我都会收到这个错误:

忽略命令 None 中的异常: discord.ext.commands.errors.CommandNotFound:找不到命令“join”

import discord
from discord.ext import commands
import youtube_dl

class music(commands.Cog):
  def __init__(self, client):
    self.client = client

    @commands.command()
    async def join(self, ctx):
        if ctx.author.voice is None:
          await ctx.send("Not in channel")
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
          await voice_channel.connect()
        else:
          await ctx.voice_client.move_to(voice_channel)

    @commands.command()
    async def disconnect(self, ctx):
      await ctx.voice_client.disconnect()

    @commands.command()
    async def p (self,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)

def setup(client):
    client.add_cog(music(client))

这是我的主文件:

import discord
from discord.ext import commands
import music

cogs = [music]

client = commands.Bot(command_prefix = '!', intents = discord.Intents.all())

for i in range(len(cogs)):
   cogs[i].setup(client)



client.run("*my bot here*")

【问题讨论】:

  • 您是否在主文件中正确注册了您的 cog?
  • 我想是的。我会在帖子中添加我的主文件。
  • 您需要使用load_extension 加载齿轮。我已经发布了一个答案,其中包含一些应该适合您的代码。

标签: discord discord.py


【解决方案1】:

如上面评论中所说,要加载齿轮,您需要 load_extension

https://discordpy.readthedocs.io/en/master/ext/commands/api.html?highlight=bot%20load%20extension#discord.ext.commands.Bot.load_extension

这里应该可以工作:

cogs = ['music']

for i in cogs:
   client.load_extension(f'cogs.{i}')

这假设您将 music cog 保存在名为 cogs 的文件夹中。如果您使用不同的名称,请将client.load_extension(f'cogs.{i}') 行中的cogs 更改为您正在使用的任何名称,或者如果它们位于同一文件夹中,则将其完全省略。

【讨论】:

  • 谢谢!我实现了这一点。我从线上省略了齿轮,因为它们都在同一个文件夹中。但是,当我向机器人发出命令时,我仍然收到此错误:Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "join" is not found
  • @Petu 哦,帖子中的缩进与您文件中的缩进相同吗?如果是这样,您需要将命令缩进一次。它们需要与def __init__(self, client): 处于同一缩进级别,并且现在它们与self.client = client 处于同一级别。抱歉我没有早点发现。一般来说,缩进看起来有点像,有时你使用 2 个空格,有时是 4 个。我建议坚持一个选择。
  • 就是这样,我重做了缩进,它起作用了,非常感谢!
猜你喜欢
  • 2021-02-21
  • 2022-12-03
  • 2022-01-21
  • 2020-06-20
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 2021-05-27
  • 2019-04-30
相关资源
最近更新 更多