Skip to content

Commit

Permalink
Add voice command
Browse files Browse the repository at this point in the history
  • Loading branch information
MycroftKang committed Feb 11, 2024
1 parent 19fc2d0 commit 79449f8
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 153 deletions.
4 changes: 4 additions & 0 deletions changelogs/unreleased/add-voice-command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Add voice command
pull_request:
type: features
2 changes: 1 addition & 1 deletion locales/ko_KR/LC_MESSAGES/mkbot.po
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ msgid "Your enemy is not ready yet, please wait until they are"
msgstr "적이 아직 준비되지 않았습니다, 잠시 기다려주세요"

msgid "[MK Bot]({url}) said on behalf of {author}"
msgstr "[MK Bot]({url}) {author}을(를) 대신하여 말했습니다"
msgstr "[MK Bot]({url}) {author}을(를) 대신하여 말했습니다."

msgid "chose... %s!"
msgstr "선택... %s!"
Expand Down
9 changes: 2 additions & 7 deletions locales/mkbot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ msgstr ""
msgid "Invalid language"
msgstr ""

msgid "Invalid parameter. For more information, type `{commandPrefix}help tts`."
msgstr ""

msgid "Invalid time format. The required format is `<hour>:<minute>`."
msgstr ""

Expand Down Expand Up @@ -695,12 +692,10 @@ msgstr ""
#, docstring
msgid ""
"TTS voice available\n"
"{commandPrefix}tts [option] \"Content\" : Says the content in \"content\". You do not have to use Quotation marks even if there are spaces included in content.\n"
"{commandPrefix}tts \"Content\" : Says the content in \"content\". You do not have to use Quotation marks even if there are spaces included in content.\n"
"\n"
"*Example*\n"
"{commandPrefix}tts \"Content\"\n"
"{commandPrefix}tts -m \"Content\"\n"
"{commandPrefix}tts -w \"Content\": -m speaks in male voice and -w speaks in female voice. Default voice is male."
"{commandPrefix}tts \"Content\""
msgstr ""

#, docstring
Expand Down
21 changes: 20 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ requests = "2.31.0"
sqlalchemy = "1.4.46"
yt-dlp = "2023.12.30"
mulgyeol-telemetry = {git = "https://github.com/MycroftKang/application-insights-python.git", rev = "c03c4d415b15648134717bb402d895d9b7ebea57", optional = true}
gtts = "^2.5.1"

[tool.poetry.group.dev.dependencies]
black = "22.3.0"
Expand Down
143 changes: 0 additions & 143 deletions src/bot/core/controllers/discord/tts.py

This file was deleted.

64 changes: 64 additions & 0 deletions src/bot/core/controllers/discord/voice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import io

import discord
from discord.ext import commands
from gtts import gTTS

from mgylabs.i18n import __
from mgylabs.utils import logger

from .utils.exceptions import UsageError
from .utils.feature import Feature
from .utils.MGCert import Level, MGCertificate
from .utils.MsgFormat import MsgFormatter
from .utils.voice import validate_voice_client

log = logger.get_logger(__name__)


@commands.hybrid_command()
@MGCertificate.verify(level=Level.TRUSTED_USERS)
@Feature.Experiment()
async def voice(ctx: commands.Context, *, content: str):
"""
TTS voice available
{commandPrefix}tts "Content" : Says the content in "content". You do not have to use Quotation marks even if there are spaces included in content.
*Example*
{commandPrefix}tts "Content"
"""

if not await validate_voice_client(ctx):
raise UsageError(
__(
"You are not in any voice channel. Please join a voice channel to use TTS."
)
)

mp3 = io.BytesIO()
gtts = gTTS(content, lang="ko")
gtts.write_to_fp(mp3)
mp3.seek(0)

if not ctx.interaction:
await ctx.message.delete()

embed = MsgFormatter.get(
ctx,
content,
__("[MK Bot]({url}) said on behalf of {author}").format(
url="https://github.com/mgylabs/mkbot", author=ctx.author.mention
),
show_req_user=False,
)

embed.set_author(
name=ctx.message.author.display_name, icon_url=ctx.message.author.avatar.url
)
await ctx.send(embed=embed)

ctx.voice_client.play(discord.FFmpegPCMAudio(mp3, pipe=True))


async def setup(bot: commands.Bot):
bot.add_command(voice)
2 changes: 1 addition & 1 deletion src/bot/discord_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"core.controllers.discord.leave",
"core.controllers.discord.logout",
"core.controllers.discord.ping",
# "core.controllers.discord.tts",
"core.controllers.discord.voice",
"core.controllers.discord.poll",
"core.controllers.discord.roulette",
"core.controllers.discord.dice",
Expand Down

0 comments on commit 79449f8

Please sign in to comment.