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 c6bc55f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 145 deletions.
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.

59 changes: 59 additions & 0 deletions src/bot/core/controllers/discord/voice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.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)
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)

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
),
)

embed.set_author(
name=ctx.message.author.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 c6bc55f

Please sign in to comment.