File size: 3,198 Bytes
56df8ed
 
 
 
39fd8bf
ea1af42
 
56df8ed
39fd8bf
56df8ed
 
39fd8bf
 
56df8ed
 
 
35c957f
56df8ed
35c957f
 
 
 
 
 
 
 
 
56df8ed
 
 
 
 
 
 
39fd8bf
35c957f
39fd8bf
56df8ed
 
39fd8bf
56df8ed
 
39fd8bf
 
35c957f
39fd8bf
35c957f
39fd8bf
 
 
 
 
 
 
 
 
 
ea1af42
 
35c957f
 
 
 
 
ea1af42
 
35c957f
 
ea1af42
35c957f
 
ea1af42
 
35c957f
 
 
ea1af42
 
 
 
f45da3f
 
56df8ed
 
f45da3f
56df8ed
35c957f
56df8ed
 
ea1af42
56df8ed
 
 
 
 
 
 
 
39fd8bf
56df8ed
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import discord
from discord.ext import commands
import threading
import gradio as gr
import asyncio
import requests
import urllib.parse

# Set up intents - message_content is required to fetch message text
intents = discord.Intents.default()
intents.messages = True
intents.reactions = True
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

# Mapping flag emojis to their language names
flag_to_language = {
    "๐Ÿ‡ฌ๐Ÿ‡ง": "English",
    "๐Ÿ‡ซ๐Ÿ‡ท": "French",
    "๐Ÿ‡ฉ๐Ÿ‡ช": "German",
    "๐Ÿ‡ช๐Ÿ‡ธ": "Spanish",
    "๐Ÿ‡ฎ๐Ÿ‡น": "Italian",
    "๐Ÿ‡ท๐Ÿ‡บ": "Russian",
    "๐Ÿ‡ฏ๐Ÿ‡ต": "Japanese",
    "๐Ÿ‡จ๐Ÿ‡ณ": "Chinese",
    # Add more flags and language names as needed
}

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")

@bot.event
async def on_raw_reaction_add(payload):
    # Ignore reactions by the bot itself
    if payload.user_id == bot.user.id:
        return

    emoji = str(payload.emoji)
    if emoji in flag_to_language:
        target_lang = flag_to_language[emoji]
        channel = bot.get_channel(payload.channel_id)
        if channel is None:
            print("Channel not found")
            return

        try:
            # Fetch the full message to access its content
            message = await channel.fetch_message(payload.message_id)
        except Exception as e:
            print(f"Error fetching message: {e}")
            return

        original_text = message.content
        if not original_text:
            print("Message content is empty!")
            return

        # Build the translation prompt using the target language
        prompt = (
            f'Translate "{original_text}" to {target_lang}. '
            "Write only and only the translated text, no need for explanation or anything."
        )
        encoded_prompt = urllib.parse.quote(prompt)
        url = f"https://text.pollinations.ai/{encoded_prompt}?model=gemini"
        print(f"GET {url}")

        try:
            # Run the blocking GET request in a thread so as not to block the event loop.
            response = await asyncio.to_thread(requests.get, url, timeout=10)
            response.raise_for_status()
            translated_text = response.text.strip()
            print(f"API response: {translated_text}")
            if not translated_text:
                translated_text = "[No translation returned]"
        except Exception as e:
            print(f"Error during GET request: {e}")
            translated_text = "[Error translating text]"

        # Reply to the original message so that it is tagged
        await message.reply(f"`{translated_text}`")

def run_discord_bot():
    bot.run("MTM1MjI2OTQ4MTQwNTE4NjA3MA.GrdeHW.rYndSNvb9mepFdp_uTK4IOAmKwt31QER6hRgzg")  # Replace with your actual token

# Run the Discord bot in a separate daemon thread
threading.Thread(target=run_discord_bot, daemon=True).start()

# Gradio interface function (simple echo interface)
def process_input(text):
    if not text.strip():
        return "OK"
    return text

iface = gr.Interface(
    fn=process_input,
    inputs=gr.Textbox(label="Enter something"),
    outputs=gr.Textbox(label="Output")
)

iface.launch()