NihalGazi commited on
Commit
39fd8bf
Β·
verified Β·
1 Parent(s): 56df8ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -2,11 +2,13 @@ import discord
2
  from discord.ext import commands
3
  import threading
4
  import gradio as gr
 
5
 
6
- # ==== Discord Bot Setup ====
7
  intents = discord.Intents.default()
8
- intents.reactions = True
9
  intents.messages = True
 
 
10
 
11
  bot = commands.Bot(command_prefix="!", intents=intents)
12
 
@@ -14,6 +16,7 @@ bot = commands.Bot(command_prefix="!", intents=intents)
14
  def translate(text, target_language):
15
  return f"[{target_language} translation of] {text}"
16
 
 
17
  flag_to_language = {
18
  "πŸ‡«πŸ‡·": "fr", # French
19
  "πŸ‡©πŸ‡ͺ": "de", # German
@@ -27,38 +30,50 @@ flag_to_language = {
27
  async def on_ready():
28
  print(f"Logged in as {bot.user}")
29
 
 
30
  @bot.event
31
- async def on_reaction_add(reaction, user):
32
- if user.bot:
33
- return
34
- if reaction.message.author.bot:
35
  return
36
 
37
- emoji = reaction.emoji
38
  if emoji in flag_to_language:
39
  target_lang = flag_to_language[emoji]
40
- original_text = reaction.message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  translated_text = translate(original_text, target_lang)
42
- await reaction.message.channel.send(f"Translated to {target_lang}: {translated_text}")
43
 
44
  def run_discord_bot():
45
- bot.run("MTM1MjI2OTQ4MTQwNTE4NjA3MA.GrdeHW.rYndSNvb9mepFdp_uTK4IOAmKwt31QER6hRgzg")
46
 
47
- # Start Discord bot in a separate thread
48
  threading.Thread(target=run_discord_bot, daemon=True).start()
49
 
50
- # ==== Gradio Interface ====
51
  def process_input(text):
52
  if not text.strip():
53
  return "OK"
54
  return text
55
 
56
- # Gradio UI
57
  iface = gr.Interface(
58
  fn=process_input,
59
  inputs=gr.Textbox(label="Enter something"),
60
- outputs=gr.Textbox(label="Output"),
61
- live=True,
62
  )
63
 
64
  iface.launch()
 
2
  from discord.ext import commands
3
  import threading
4
  import gradio as gr
5
+ import asyncio
6
 
7
+ # Set up intents - message_content is required to fetch message text
8
  intents = discord.Intents.default()
 
9
  intents.messages = True
10
+ intents.reactions = True
11
+ intents.message_content = True
12
 
13
  bot = commands.Bot(command_prefix="!", intents=intents)
14
 
 
16
  def translate(text, target_language):
17
  return f"[{target_language} translation of] {text}"
18
 
19
+ # Mapping flag emojis to language codes
20
  flag_to_language = {
21
  "πŸ‡«πŸ‡·": "fr", # French
22
  "πŸ‡©πŸ‡ͺ": "de", # German
 
30
  async def on_ready():
31
  print(f"Logged in as {bot.user}")
32
 
33
+ # Use on_raw_reaction_add to reliably catch reactions on uncached messages
34
  @bot.event
35
+ async def on_raw_reaction_add(payload):
36
+ # Ignore if the reaction was added by the bot itself
37
+ if payload.user_id == bot.user.id:
 
38
  return
39
 
40
+ emoji = str(payload.emoji)
41
  if emoji in flag_to_language:
42
  target_lang = flag_to_language[emoji]
43
+ # Get the channel from the payload
44
+ channel = bot.get_channel(payload.channel_id)
45
+ if channel is None:
46
+ return
47
+ try:
48
+ # Fetch the full message to access its content
49
+ message = await channel.fetch_message(payload.message_id)
50
+ except Exception as e:
51
+ print(f"Error fetching message: {e}")
52
+ return
53
+
54
+ original_text = message.content
55
+ if not original_text:
56
+ print("Message content is empty!")
57
  translated_text = translate(original_text, target_lang)
58
+ await channel.send(f"Translated to {target_lang}: {translated_text}")
59
 
60
  def run_discord_bot():
61
+ bot.run("YOUR_DISCORD_BOT_TOKEN") # Replace with your actual token
62
 
63
+ # Run Discord bot in a separate daemon thread
64
  threading.Thread(target=run_discord_bot, daemon=True).start()
65
 
66
+ # Gradio interface function
67
  def process_input(text):
68
  if not text.strip():
69
  return "OK"
70
  return text
71
 
72
+ # Create a simple Gradio UI
73
  iface = gr.Interface(
74
  fn=process_input,
75
  inputs=gr.Textbox(label="Enter something"),
76
+ outputs=gr.Textbox(label="Output")
 
77
  )
78
 
79
  iface.launch()