我是 Python 的初学者,最近我开始为一些朋友和我制作一个 discord 机器人。我的想法是输入 !startq 并让机器人加入频道,播放本地存储在同一文件夹中的 mp3 文件bot.py 也在。
import discord, chalk
from discord.ext import commands
import time
import asyncio
bot = commands.Bot(command_prefix = "!")
@bot.event
async def on_ready():
print("Bot is ready!")
@bot.command()
async def q5(ctx):
await ctx.send("@here QUEUE STARTING IN 5 MINUTES")
@bot.command()
async def q3(ctx):
await ctx.send("@here QUEUE STARTING IN 3 MINUTES")
@bot.command()
async def q1(ctx):
await ctx.send("@here QUEUE STARTING IN 1 MINUTES")
@bot.command()
async def ping(ctx):
ping_ = bot.latency
ping = round(ping_ * 1000)
await ctx.send(f"my ping is {ping}ms")
@bot.command()
async def startq(ctx):
voicechannel = discord.utils.get(ctx.guild.channels, name='queue')
vc = await voicechannel.connect()
vc.play(discord.FFmpegPCMAudio("countdown.mp3"), after=lambda e: print('done', e))
bot.run('TOKEN')
到目前为止,我的机器人可以正常加入频道,但它实际上并没有播放 mp3。我在“非官方 Discord API Discord”和其他一些编程 Discord 中问过无数人,但我还没有得到答案。
原文由 ropke 发布,翻译遵循 CC BY-SA 4.0 许可协议
我对我的 discord 机器人做了类似的事情,这里有一些您可以参考的示例代码。如果您正在播放 mp3 文件,请确保安装 ffmpeg,我在设置我的机器人时按照此处的说明进行操作 https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg