lunarflu HF staff commited on
Commit
d3ba82c
1 Parent(s): 21c7d26
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -8,8 +8,11 @@ from slack_sdk.errors import SlackApiError
8
 
9
  DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
10
  SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
11
- SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
12
- SLACK_CHANNEL_ID_TEST = 'C07B4KNU5BQ'
 
 
 
13
 
14
  TRIGGERS = {
15
  "discord bot": "<@U051DB2754M>", # Adam
@@ -44,8 +47,11 @@ bot = commands.Bot(command_prefix='!', intents=intents)
44
  slack_client = WebClient(token=SLACK_BOT_TOKEN)
45
 
46
  thread_mapping = {}
 
 
47
  FORUM_CHANNEL_ID = 1259415803879751700
48
 
 
49
  @bot.event
50
  async def on_ready():
51
  print(f'Logged in as {bot.user}')
@@ -66,31 +72,28 @@ async def on_message(message):
66
  await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
67
  break
68
 
69
-
70
-
71
-
72
  # Check if the message is in a thread
73
  if isinstance(message.channel, discord.Thread):
74
  discord_thread_id = message.channel.id
75
 
76
  # Check if there's an existing Slack thread for this Discord thread
 
77
  if discord_thread_id in thread_mapping:
78
  slack_thread_ts = thread_mapping[discord_thread_id]
79
- else:
80
- # If not, create a new Slack thread and update the mapping
81
- slack_thread_ts = post_to_slack_forum_version('#discord-slackbot-test', f"Thread started: {message.channel.name}")
82
- if slack_thread_ts:
83
- thread_mapping[discord_thread_id] = slack_thread_ts
84
-
85
- # Post the message to the existing Slack thread
86
- post_to_slack_forum_version('#discord-slackbot-test', message.content, thread_ts=slack_thread_ts)
87
 
88
 
89
  @bot.event
90
  async def on_thread_create(thread):
 
91
  if isinstance(thread.parent, discord.ForumChannel) and thread.parent.id == FORUM_CHANNEL_ID:
92
  discord_thread_id = thread.id
93
- slack_thread_ts = post_to_slack_forum_version('#discord-slackbot-test', f"New forum thread started: {thread.name}")
 
 
 
 
94
  if slack_thread_ts:
95
  thread_mapping[discord_thread_id] = slack_thread_ts
96
 
@@ -106,10 +109,6 @@ def post_to_slack_forum_version(channel, text, thread_ts=None):
106
  except SlackApiError as e:
107
  print(f"Error posting to Slack: {e.response['error']}")
108
  return None
109
-
110
-
111
-
112
-
113
  #----------------------------------------------------------------------------------------------
114
 
115
 
 
8
 
9
  DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
10
  SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
11
+
12
+ # real = os.getenv('SLACK_CHANNEL_ID_HF')
13
+ # test = 'C07B4KNU5BQ'
14
+ SLACK_CHANNEL_ID = 'C07B4KNU5BQ'
15
+
16
 
17
  TRIGGERS = {
18
  "discord bot": "<@U051DB2754M>", # Adam
 
47
  slack_client = WebClient(token=SLACK_BOT_TOKEN)
48
 
49
  thread_mapping = {}
50
+ # 1259415803879751700 = test forum
51
+ # 1019883044724822016 = ask for help
52
  FORUM_CHANNEL_ID = 1259415803879751700
53
 
54
+
55
  @bot.event
56
  async def on_ready():
57
  print(f'Logged in as {bot.user}')
 
72
  await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
73
  break
74
 
 
 
 
75
  # Check if the message is in a thread
76
  if isinstance(message.channel, discord.Thread):
77
  discord_thread_id = message.channel.id
78
 
79
  # Check if there's an existing Slack thread for this Discord thread
80
+ # (the only Slack threads created should be for forum channel threads, not just any thread)
81
  if discord_thread_id in thread_mapping:
82
  slack_thread_ts = thread_mapping[discord_thread_id]
83
+ # post to slack only if thread already exists
84
+ post_to_slack_forum_version(SLACK_CHANNEL_ID, message.content, thread_ts=slack_thread_ts)
 
 
 
 
 
 
85
 
86
 
87
  @bot.event
88
  async def on_thread_create(thread):
89
+ # (discord) must be the child thread of the CORRECT forum channel(s) (not just any thread, or any forum channel)
90
  if isinstance(thread.parent, discord.ForumChannel) and thread.parent.id == FORUM_CHANNEL_ID:
91
  discord_thread_id = thread.id
92
+ slack_thread_ts = post_to_slack_forum_version(
93
+ SLACK_CHANNEL_ID,
94
+ f"New forum thread started in #ask-for-help by {thread.owner}: *{thread.name}*\n"
95
+ f"{thread.jump_url}"
96
+ )
97
  if slack_thread_ts:
98
  thread_mapping[discord_thread_id] = slack_thread_ts
99
 
 
109
  except SlackApiError as e:
110
  print(f"Error posting to Slack: {e.response['error']}")
111
  return None
 
 
 
 
112
  #----------------------------------------------------------------------------------------------
113
 
114