【问题标题】:How to Make a Discord Bot Join a Voice Channel and Play an Audio File When a Member Joins the Channel Using Discord.py Cogs如何使用 Discord.py Cogs 使 Discord Bot 加入语音频道并在成员加入频道时播放音频文件
【发布时间】:2021-03-02 10:18:46
【问题描述】:

我目前的项目

我目前正在尝试在 Python 3 中为 Discord 机器人制作一个 cog,当有人加入特定的 Discord 语音频道时,它在运行时播放特定的音频文件。

我的问题

我已经有了我的项目的代码(信用:Tabulate),但我不知道如何

  1. 将其转换为 cog,然后
  2. 使其适用于特定语音频道,而不是 Discord 中的每个频道 服务器。

这是我的代码:

import discord
from discord.ext import commands
from time import sleep

rpgmusicpath = r"C:\Users\lones\OneDrive\Desktop\Bot\music\rpgmusic.mp3"

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

    @commands.Cog.listener()
    async def on_ready(self):
        print('Channel Intro cog successfully loaded.')

    @commands.Cog.event
    async def on_voice_state_update(member: discord.Member, before, after):
    #replace this with the path to your audio file
        path = r"path\to\music.mp3"

        vc_before = before.channel
        vc_after = after.channel
    
        if vc_before == vc_after:
            return
    
        elif vc_before is None:
            channel = member.voice.channel
            vc = await channel.connect()
            vc.play(discord.FFmpegPCMAudio(path))
            with audioread.audio_open(path) as f:
                #Start Playing
                sleep(f.duration)
            await vc.disconnect()

        elif vc_after is None:
            return
    
        else:
            channel = member.voice.channel
            vc = await channel.connect()
            vc.play(discord.FFmpegPCMAudio(path))
            with audioread.audio_open(path) as f:
                #Start Playing
                sleep(f.duration)
            await vc.disconnect()
        

def setup(bot):
    bot.add_cog(VoiceChannelIntro(bot))

【问题讨论】:

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


    【解决方案1】:

    以下是你的一些错误:

    1. 使用asyncio.sleep() 而不是time.sleep()
    2. 您忘记将self 作为第一个参数传递

    以下是修改后的代码:

    import discord
    from discord.ext import commands
    from asyncio import sleep
    
    rpgmusicpath = r"C:\Users\lones\OneDrive\Desktop\Bot\music\rpgmusic.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.Cog.event
        async def on_voice_state_update(self, member, before, after):
        #replace this with the path to your audio file
            path = r"C:\Users\lones\OneDrive\Desktop\Bot\test-chamber-4-intro.mp3"
    
            vc_before = before.channel
            vc_after = after.channel
        
            if not vc_before and vc_after.id == YOUR VOICE CHANNEL ID:
                vc = await vc_after.connect()
                vc.play(discord.FFmpegPCMAudio(path))
                with audioread.audio_open(path) as f:
                    #Start Playing
                    sleep(f.duration)
                await vc.disconnect()
    
    def setup(bot):
        bot.add_cog(Music(bot))
    

    【讨论】:

    • 嘿,所以我去测试它,但我得到一个错误。当我运行代码时,它显示:“AttributeError: type object 'Cog' has no attribute 'event'”。知道可能是什么原因造成的吗?我查看了 Discord.py 中的文档,但它甚至没有提到任何关于 Cog.event 的内容。
    猜你喜欢
    • 2020-11-04
    • 2020-11-03
    • 1970-01-01
    • 2022-01-23
    • 2021-01-05
    • 2020-06-26
    • 2021-06-06
    • 2020-09-05
    • 2018-04-04
    相关资源
    最近更新 更多