【问题标题】:How to Make a Discord Music Bot Using Discord.py Cogs如何使用 Discord.py Cogs 制作 Discord 音乐机器人
【发布时间】:2020-09-27 23:58:48
【问题描述】:

我目前的项目

我正在尝试将我从 YouTube 视频中制作的 Python 音乐机器人转换为 cog,因此我的主要 Python Discord 机器人文件不会那么混乱。

我的问题

我写的下面的代码不起作用。我阅读了有关 cogs 的文档,但似乎找不到我做错了什么。

这是我尝试制作的代码,但它

import discord
from discord.ext import commands
from discord.utils import get
import youtube_dl
import os
from time import sleep
 
rpgmusicpath = r"path\to\music.mp3"
 
class Music(commands.Cog):
    def __init__(self, client):
        self.bot = client
 
    @commands.Cog.listener()
    async def on_ready(self):
        print('Music cog successfully loaded.')
       
       
    @commands.command(pass_context=True)
    async def rpgmusic(ctx, self):
        global voice
        channel = ctx.message.author.voice.channel
        voice = get(bot.voice_clients, guild=ctx.guild)
 
        if voice and voice.is_connected():
            await voice.move_to(channel)
 
        else:
            voice = await channel.connect()
            print(f'Bot connected to voice channel {channel}\n')
   
        await ctx.send(f'Playing some RPG music in {channel}.')
        sleep(3)
        voice.play(discord.FFmpegPCMAudio('rpgmusic.mp3'), after=lambda e: print(f'RPG music in {channel} has finished playing.'))
        voice.source = discord.PCMVolumeTransformer(voice.source)
        voice.source.volume = 0.05
 
   
    @commands.command(pass_context=True)
    async def join(ctx, self):
        global voice
        channel = ctx.message.author.voice.channel
        voice = get(bot.voice_clients, guild=ctx.guild)
 
        if voice and voice.is_connected():
            await voice.move_to(channel)
 
        else:
            voice = await channel.connect()
            print(f'Bot connected to voice channel {channel}\n')
 
        await ctx.send(f'I joined {channel}.')
   
 
    @commands.command(pass_context=True)
    async def leave(ctx, self):
        channel = ctx.message.author.voice.channel
        voice = get(bot.voice_clients, guild=ctx.guild)
   
        if voice and voice.is_connected():
            await voice.disconnect()
            print(f'Bot disconnected from channel {channel}.')
       
        else:
            print('Not able to disconnect to a voice channel because bot wasn\'t in one.')
       
    @commands.command(pass_context=True)
    async def play(ctx, url: str, self):
        song_there = os.path.isfile('song.mp3')
        try:
            if song_there:
                os.remove('song.mp3')
                print('Removed current song.')
        except PermissionError:
            print('Error in deleting song file. (Song in use.)')
            await ctx.send('Unable to request song. (Song already in use.)')
            return
   
        await ctx.send('Preparing song. Please wait.')
        voice = get(bot.voice_clients, guild=ctx.guild)
 
        ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
            }],
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            print('Downloading audio now.\n')
            ydl.download([url])
 
        for file in os.listdir('./'):
            if file.endswith('.mp3'):
                name = file
                print(f'Renamed File: {file}.')
                os.rename(file, 'song.mp3')
 
        voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: print(f'{name} has finished playing.'))
        voice.source = discord.PCMVolumeTransformer(voice.source)
        voice.source.volume = 0.06
 
        nname = name.rsplit('-', 2)
        await ctx.send(f'Now playing {name}.')
        print('Now playing.\n')
 
 
def setup(bot):
    bot.add_cog(Music(bot))

【问题讨论】:

    标签: python-3.x audio discord.py-rewrite


    【解决方案1】:

    两个问题:

    • 您没有将bot 替换为self.bot

    rpgmusicjoinleaveplay 命令中,更改:

    voice = get(bot.voice_clients, guild=ctx.guild)
    

    收件人:

    voice = get(self.bot.voice_clients, guild=ctx.guild)
    
    • 您的第一个参数必须是self,而不是ctx
    @commands.command(pass_context=True)
    async def rpgmusic(self, ctx)
    
    @commands.command(pass_context=True)
    async def join(self, ctx)
    
    @commands.command(pass_context=True)
    async def play(self, ctx)
    

    另外,由于你有一个join 函数,你可以在rpgmusic 中等待它(你也不需要global voice):

    @commands.command(pass_context=True)
        async def rpgmusic(ctx, self):
            await self.join(ctx)
            await ctx.send(f'Playing some RPG music in {channel}.')
            sleep(3)
            voice.play(discord.FFmpegPCMAudio('rpgmusic.mp3'), after=lambda e: print(f'RPG music in {channel} has finished playing.'))
            voice.source = discord.PCMVolumeTransformer(voice.source)
            voice.source.volume = 0.05
    

    【讨论】:

    • 它仍然没有加入。更糟糕的是,我在控制台中没有收到任何错误。还有其他想法吗?
    • 非常感谢。这让一切正常。而且,至于这个例子,我想我会坚持下载它。直接从 YT 播放会产生静态效果,而且质量不是很高。我宁愿等待一点,质量好,也不愿有更多的时间,质量差。不过还是谢谢。
    猜你喜欢
    • 2021-05-24
    • 2021-05-05
    • 1970-01-01
    • 2018-05-18
    • 2021-05-27
    • 2017-06-18
    • 2022-01-04
    • 2019-04-30
    • 2018-07-24
    相关资源
    最近更新 更多