首页 > 解决方案 > 可以为 Discord 音乐机器人编程自动语音吗?

问题描述

因此,标题基本上以简短的形式解释了我对 Discord 机器人的问题。更具体地说,如果音乐曲目排在第一位,并且不遵循公会配置的准则,例如包含亵渎内容,则该音频具有由原始发布者通过机器人网站提交给我们的已批准的版权侵权投诉,禁止收听到音频或它是一个尖叫者,它会自动说出如下信息:

计划在此时播放的当前音乐音量过大或超出音高频率限制。跳到队列中的下一首音乐。

目前计划播放的当前音乐已被标记为包含亵渎语言,并且不符合公会配置的指南。跳到队列中的下一首音乐。

计划在此时播放的当前音乐有版权侵权投诉,表明原始发布者限制收听音频。跳到队列中的下一首音乐。

是否可以为 Discord 机器人编程和自动语音?

提前致谢。

标签: pythonbotsdiscorddiscord.py

解决方案


我将帮助您将用户 Amadan 的想法实施到 discord.py 中,首先您需要安装 google text to Speech python api:

pip install gtts

这个答案将假设您没有使用 discord.py 的 rewrite 分支。

import os
import discord
from gtts import gTTS

if not discord.opus.is_loaded():
    # or libopus.so on linux in the current directory
    # you should replace this with the location the
    # opus library is located in and with the proper filename.
    discord.opus.load_opus('opus')

if not os.path.exists('message1.mp3'):

    tts = gTTS("The current music scheduled to play at this time is too intense in volume or exceeds "
               "the frequency limits for pitch. Skipping to next music in queue.")
    with open('message1.mp3', 'wb') as f:
        tts.write_to_fp(f)

if not os.path.exists('message2.mp3'):
    tts = gTTS("The current music scheduled to play at this time has been marked for containing profane"
               " language and does not comply with the guild-configured guidelines. "
               "Skipping to next music in queue.")
    with open('message2.mp3', 'wb') as f:
        tts.write_to_fp(f)

if not os.path.exists('message3.mp3'):
    tts = gTTS("The current music scheduled to play at this time has a copyright infringement complaint "
               "which indicates that the original publisher restricts listening to the audio. "
               "Skipping to next music in queue.")
    with open('message3.mp3', 'wb') as f:
        tts.write_to_fp(f)

@bot.command(pass_context=True)
async def play(ctx, num):
    vc = ctx.author.voice.voice_channel

    voice = await bot.join_voice_channel(vc)

    player = voice.create_ffmpeg_player('message{}.mp3'.format(num))
    player.start()

现在不和谐,当您在语音频道中并键入<prefix>play <num>时,其中 prefix 是机器人前缀,num (1-3) 是您要播放的消息的索引,机器人会说出该消息。


推荐阅读