translatomat/main.py
2025-05-03 17:17:32 +02:00

78 lines
2.1 KiB
Python

import os
import googletrans
import discord
from discord import app_commands
discord_bot_token = os.environ['TOKEN_DISCORD']
intents = discord.Intents.all()
bot_client = discord.Client(intents=intents)
tree = app_commands.CommandTree(bot_client)
"""
My Bots Overview:
https://discordapp.com/developers/applications
Discord Bot API Documentation:
https://discordpy.readthedocs.io/en/latest/api.html
Changelog:
https://discordpy.readthedocs.io/en/latest/whats_new.html
"""
###
# Discord Events
###
@bot_client.event
async def on_ready():
print("Discord.py version {}".format(discord.__version__))
print("googletrans version {}".format(googletrans.__version__))
await tree.sync()
await bot_client.change_presence(activity=discord.Game(name="Translating only for you!"))
@bot_client.event
async def on_message(ctx: discord.Message):
if ctx.author.bot:
return
translator = googletrans.Translator()
detected = await translator.detect(ctx.content)
detected_lang = detected.lang
if detected.confidence < .95:
return
translator_category = discord.utils.find(lambda c: c.name == "Translatomat", ctx.guild.categories)
for channel in translator_category.channels:
if detected_lang != channel.name:
translated = await translator.translate(str(ctx.clean_content), channel.name)
translated_message = translated.text
if str(ctx.clean_content).lower() == str(translated_message).lower():
return
embed = discord.Embed(color=0x04a1ff)
embed.add_field(name="Translated Message", value=translated_message, inline=False)
embed.add_field(name="Original Message", value=ctx.content, inline=False)
embed.add_field(name="Author", value=ctx.author.mention, inline=True)
embed.add_field(name="Channel", value=ctx.channel.mention, inline=True)
embed.add_field(name="Message Link", value=ctx.jump_url, inline=True)
await channel.send(embed=embed)
###
# Commands
###
if __name__ == "__main__":
bot_client.run(discord_bot_token)