Spaces:
Paused
Paused
test
Browse files
app.py
CHANGED
|
@@ -43,6 +43,9 @@ bot = commands.Bot(command_prefix='!', intents=intents)
|
|
| 43 |
|
| 44 |
slack_client = WebClient(token=SLACK_BOT_TOKEN)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
@bot.event
|
| 47 |
async def on_ready():
|
| 48 |
print(f'Logged in as {bot.user}')
|
|
@@ -63,64 +66,47 @@ async def on_message(message):
|
|
| 63 |
await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
|
| 64 |
break
|
| 65 |
|
| 66 |
-
ask_for_help = await bot.fetch_channel(1259415803879751700)
|
| 67 |
-
if ask_for_help.last_message_id == message.channel.id:
|
| 68 |
-
content = message.content
|
| 69 |
-
thread_id = message.channel.id
|
| 70 |
|
| 71 |
-
try:
|
| 72 |
|
| 73 |
-
thread_ts = find_slack_thread_ts(thread_id)
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
text=content,
|
| 80 |
-
thread_ts=thread_ts
|
| 81 |
-
)
|
| 82 |
-
else:
|
| 83 |
|
| 84 |
-
response = slack_client.chat_postMessage(
|
| 85 |
-
channel=SLACK_CHANNEL_ID_TEST,
|
| 86 |
-
text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}\n\nDiscord Thread ID: {thread_id}"
|
| 87 |
-
)
|
| 88 |
-
thread_ts = response['ts']
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
|
| 99 |
-
def
|
| 100 |
-
"""Search for a Slack thread with the given Discord thread ID and return the thread_ts."""
|
| 101 |
try:
|
| 102 |
-
response = slack_client.
|
| 103 |
-
channel=
|
| 104 |
-
|
|
|
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
while response['has_more']:
|
| 109 |
-
response = slack_client.conversations_history(
|
| 110 |
-
channel=SLACK_CHANNEL_ID_TEST,
|
| 111 |
-
limit=200,
|
| 112 |
-
cursor=response['response_metadata']['next_cursor']
|
| 113 |
-
)
|
| 114 |
-
messages.extend(response['messages'])
|
| 115 |
-
|
| 116 |
-
for message in messages:
|
| 117 |
-
if f"Discord Thread ID: {thread_id}" in message['text']:
|
| 118 |
-
return message['ts']
|
| 119 |
-
|
| 120 |
except SlackApiError as e:
|
| 121 |
-
print(f"Error
|
|
|
|
| 122 |
|
| 123 |
-
return None
|
| 124 |
|
| 125 |
|
| 126 |
|
|
|
|
| 43 |
|
| 44 |
slack_client = WebClient(token=SLACK_BOT_TOKEN)
|
| 45 |
|
| 46 |
+
thread_mapping = {}
|
| 47 |
+
FORUM_CHANNEL_ID = 123456789012345678
|
| 48 |
+
|
| 49 |
@bot.event
|
| 50 |
async def on_ready():
|
| 51 |
print(f'Logged in as {bot.user}')
|
|
|
|
| 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 |
|
| 97 |
|
| 98 |
+
def post_to_slack_forum_version(channel, text, thread_ts=None):
|
|
|
|
| 99 |
try:
|
| 100 |
+
response = slack_client.chat_postMessage(
|
| 101 |
+
channel=channel,
|
| 102 |
+
text=text,
|
| 103 |
+
thread_ts=thread_ts
|
| 104 |
)
|
| 105 |
+
return response['ts'] # Return the Slack message timestamp (thread ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except SlackApiError as e:
|
| 107 |
+
print(f"Error posting to Slack: {e.response['error']}")
|
| 108 |
+
return None
|
| 109 |
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
|