Skip to content

Commit

Permalink
feat: black formatting check workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Jul 12, 2023
1 parent ab1eaae commit 0c7630a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 56 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: black-action
on: [push, pull_request]

jobs:
linter_name:
name: Check Black Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: rickstaa/action-black@v1
with:
black_args: ". --check"
2 changes: 1 addition & 1 deletion src/cogs/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def language_codes(self, ctx):

for language in self.config["language_codes"]:
embed.add_field(
name=f"**{self.config['language_codes'][language]}**",
name=f"**{self.config['language_codes'][language]}**",
value=f"`{language}`",
inline=True,
)
Expand Down
115 changes: 61 additions & 54 deletions src/cogs/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def load_config(self):

# transcribe / translate command
@commands.command(aliases=["t", "translate", "tr", "trans"])
async def transcribe(self, ctx, *, translate_to=None): # takes an optional argument, translate_to, which is the language code to translate the transcribed text to.
async def transcribe(
self, ctx, *, translate_to=None
): # takes an optional argument, translate_to, which is the language code to translate the transcribed text to.
# check if translate_to was passed
if translate_to is not None:
translate_to = translate_to.upper()
Expand Down Expand Up @@ -81,29 +83,27 @@ async def transcribe(self, ctx, *, translate_to=None): # takes an optional argum
and translate_to in self.config["language_codes"]
):
# translate the text
deepl_translator = deepl.Translator(
auth_key=self.config["deepl_api_key"]
)
deepl_translator = deepl.Translator(auth_key=self.config["deepl_api_key"])
translated_text = deepl_translator.translate_text(
transcribed_text, target_lang=translate_to
)

embed = make_embed(transcribed_text, author, ctx.author, translate_to, translated_text)


embed_message = await replied_message.reply(
embed=embed, mention_author=False
embed = make_embed(
transcribed_text, author, ctx.author, translate_to, translated_text
)

embed_message = await replied_message.reply(embed=embed, mention_author=False)

@commands.Cog.listener("on_message")
async def auto_transcribe(self, msg: discord.Message):
if not msg_has_voice_note(msg): return
if not msg_has_voice_note(msg):
return

await msg.add_reaction("\N{HOURGLASS}")
try:
transcribed_text = await transcribe_msg(msg)
embed = make_embed(transcribed_text, msg.author)
await msg.reply(embed = embed)
await msg.reply(embed=embed)

except sr.UnknownValueError as e:
await msg.reply(
Expand All @@ -117,69 +117,76 @@ async def auto_transcribe(self, msg: discord.Message):
await msg.remove_reaction("\N{HOURGLASS}", self.bot.user)


def make_embed(transcribed_text, author, ctx_author = None, translate_to = None, translated_text = None):
embed = discord.Embed(
color=discord.Color.og_blurple(),
title=f"🔊 {author.name}'s Voice Message",
)
embed.add_field(
name=f"Transcription",
value= textwrap.dedent(
def make_embed(
transcribed_text, author, ctx_author=None, translate_to=None, translated_text=None
):
embed = discord.Embed(
color=discord.Color.og_blurple(),
title=f"🔊 {author.name}'s Voice Message",
)
embed.add_field(
name=f"Transcription",
value=textwrap.dedent(
f"""
```
{transcribed_text}
```
[vmt bot](https://github.com/dromzeh/vmt) by [@strazto](https://instagram.com/strazto) & [@dromzeh](https://github.com/dromzeh)
"""),
inline=False
)
"""
),
inline=False,
)

if translate_to and translated_text:
embed.add_field(
name=f"Translation (Into {translate_to.upper()})",
value=f"```{translated_text}```",
inline=False,
)
if translate_to and translated_text:
embed.add_field(
name=f"Translation (Into {translate_to.upper()})",
value=f"```{translated_text}```",
inline=False,
)

if ctx_author:
embed.set_footer(text=f"Transcribe requested by {ctx_author}")
if ctx_author:
embed.set_footer(text=f"Transcribe requested by {ctx_author}")

return embed
return embed


def msg_has_voice_note(msg: typing.Optional[discord.Message]) -> bool:
if not msg: return False
if not msg.attachments or not msg.flags.value >> 13: return False
if not msg:
return False
if not msg.attachments or not msg.flags.value >> 13:
return False
return True

async def transcribe_msg(msg: typing.Optional[discord.Message]) -> typing.Optional[typing.Union[typing.Any,list,tuple]]:
if not msg or not msg_has_voice_note(msg): return None

voice_msg_bytes = await msg.attachments[
0
].read() # read the voice message as bytes
voice_msg = io.BytesIO(voice_msg_bytes)
async def transcribe_msg(
msg: typing.Optional[discord.Message],
) -> typing.Optional[typing.Union[typing.Any, list, tuple]]:
if not msg or not msg_has_voice_note(msg):
return None

# convert the voice message to a .wav file
audio_segment = pydub.AudioSegment.from_file(voice_msg)
wav_bytes = io.BytesIO()
audio_segment.export(wav_bytes, format="wav")
voice_msg_bytes = await msg.attachments[0].read() # read the voice message as bytes
voice_msg = io.BytesIO(voice_msg_bytes)

# transcribe the audio using Google Speech Recognition API
recognizer = sr.Recognizer()
with sr.AudioFile(wav_bytes) as source:
audio_data = recognizer.record(source)
# convert the voice message to a .wav file
audio_segment = pydub.AudioSegment.from_file(voice_msg)
wav_bytes = io.BytesIO()
audio_segment.export(wav_bytes, format="wav")

try:
transcribed_text = recognizer.recognize_google(audio_data)
# transcribe the audio using Google Speech Recognition API
recognizer = sr.Recognizer()
with sr.AudioFile(wav_bytes) as source:
audio_data = recognizer.record(source)

except sr.UnknownValueError as e:
raise e
try:
transcribed_text = recognizer.recognize_google(audio_data)

except Exception as e:
raise e
except sr.UnknownValueError as e:
raise e

except Exception as e:
raise e

return transcribed_text
return transcribed_text


async def setup(bot):
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self):
# remove the default help command
self.remove_command("help")

async def setup_hook(self) -> None:
async def setup_hook(self) -> None:
cogsLoaded = 0
cogsCount = 0
for cog_file in os.listdir("cogs"):
Expand Down

0 comments on commit 0c7630a

Please sign in to comment.