【发布时间】:2021-03-02 10:18:46
【问题描述】:
我目前的项目
我目前正在尝试在 Python 3 中为 Discord 机器人制作一个 cog,当有人加入特定的 Discord 语音频道时,它在运行时播放特定的音频文件。
我的问题
我已经有了我的项目的代码(信用:Tabulate),但我不知道如何
- 将其转换为 cog,然后
- 使其适用于特定语音频道,而不是 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