|
import discord |
|
import threading |
|
import os |
|
import gradio as gr |
|
from discord.ext import commands |
|
from slack_sdk import WebClient |
|
from slack_sdk.errors import SlackApiError |
|
|
|
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') |
|
SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF') |
|
|
|
|
|
|
|
SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF') |
|
|
|
|
|
ASK_FOR_HELP_CHANNEL_ID = 1019883044724822016 |
|
GRADIO_CHANNEL_ID = 1025174734427656283 |
|
|
|
|
|
TRIGGERS = { |
|
("discord bot",): "<@U051DB2754M>", |
|
("text to speech",): "<@U039C2GANMV>", |
|
("tts",): "<@U039C2GANMV>", |
|
("asr",): "<@U039C2GANMV>", |
|
("musicgen",): "<@U039C2GANMV>", |
|
("whisper",): "<@U039C2GANMV>", |
|
("speech recognition",): "<@U039C2GANMV>", |
|
("bark",): "<@U039C2GANMV>", |
|
("autotrain",): "<@U01E3LEC2N7>", |
|
("auto train",): "<@U01E3LEC2N7>", |
|
("competition",): "<@U01E3LEC2N7>", |
|
("competitions",): "<@U01E3LEC2N7>", |
|
("sentence-transformers",): "<@U04E4DNPWG7>", |
|
("sentence_transformers",): "<@U04E4DNPWG7>", |
|
("setfit",): "<@U04E4DNPWG7>", |
|
("sentence transformers",): "<@U04E4DNPWG7>", |
|
("argilla",): "<@U076B8C7G3E>", |
|
("distilabel",): "<@U076B8C7G3E>", |
|
("argilla",): "<@U0766H30T7F>", |
|
("dataset",): "<@U0766H30T7F>", |
|
("docs",): "<@U02DATT4C5B>", |
|
("documentation",): "<@U02DATT4C5B>", |
|
("gradio",): "<@U02NMK75F1V>", |
|
("argilla",): "<@U076MF65WEM>", |
|
("distilabel",): "<@U076MF65WEM>", |
|
("argilla",): "<@U0765RENPNZ>", |
|
("distilabel",): "<@U0765RENPNZ>", |
|
("dataset", "feedback"): "<@U0768RCHCRY>", |
|
} |
|
|
|
intents = discord.Intents.all() |
|
intents.messages = True |
|
bot = commands.Bot(command_prefix='!', intents=intents) |
|
|
|
slack_client = WebClient(token=SLACK_BOT_TOKEN) |
|
|
|
thread_mapping = {} |
|
|
|
|
|
|
|
@bot.event |
|
async def on_ready(): |
|
print(f'Logged in as {bot.user}') |
|
|
|
@bot.event |
|
async def on_message(message): |
|
if message.author == bot.user: |
|
return |
|
|
|
|
|
huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265) |
|
bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341) |
|
if huggingfolks_role not in message.author.roles: |
|
if bots_role not in message.author.roles: |
|
content = message.content.lower() |
|
for trigger, slack_mention in TRIGGERS.items(): |
|
if trigger in content: |
|
await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger) |
|
break |
|
|
|
|
|
if isinstance(message.channel, discord.Thread): |
|
print("1") |
|
discord_thread_id = message.channel.id |
|
print("2") |
|
|
|
|
|
if discord_thread_id in thread_mapping: |
|
print("3") |
|
slack_thread_ts = thread_mapping[discord_thread_id] |
|
|
|
post_to_slack_forum_version(SLACK_CHANNEL_ID, message.content, message.author, thread_ts=slack_thread_ts) |
|
|
|
|
|
@bot.event |
|
async def on_thread_create(thread): |
|
|
|
if isinstance(thread.parent, discord.ForumChannel) and thread.parent.id in {ASK_FOR_HELP_CHANNEL_ID, GRADIO_CHANNEL_ID}: |
|
print("4") |
|
discord_thread_id = thread.id |
|
slack_thread_ts = post_to_slack_create_thread( |
|
SLACK_CHANNEL_ID, |
|
f"New forum thread started in {thread.parent.name} by {thread.owner}: *{thread.name}*\n" |
|
f"{thread.jump_url}" |
|
) |
|
print("5") |
|
if slack_thread_ts: |
|
thread_mapping[discord_thread_id] = slack_thread_ts |
|
|
|
|
|
def post_to_slack_forum_version(channel, text, author, thread_ts=None): |
|
print("6") |
|
text = f"{author}" + ": " + text |
|
try: |
|
response = slack_client.chat_postMessage( |
|
channel=channel, |
|
text=text, |
|
thread_ts=thread_ts |
|
) |
|
return response['ts'] |
|
except SlackApiError as e: |
|
print(f"Error posting to Slack: {e.response['error']}") |
|
return None |
|
|
|
|
|
def post_to_slack_create_thread(channel, text, thread_ts=None): |
|
print("6") |
|
try: |
|
response = slack_client.chat_postMessage( |
|
channel=channel, |
|
text=text, |
|
thread_ts=thread_ts |
|
) |
|
return response['ts'] |
|
except SlackApiError as e: |
|
print(f"Error posting to Slack: {e.response['error']}") |
|
return None |
|
|
|
|
|
|
|
|
|
async def post_to_slack(author, content, channel, url, slack_mention, trigger): |
|
try: |
|
response = slack_client.chat_postMessage( |
|
channel=SLACK_CHANNEL_ID, |
|
text=f"{slack_mention} (for the keyword -> '{trigger}')\nFrom {author} in channel #{channel}: {content}\n{url}" |
|
) |
|
except SlackApiError as e: |
|
print(f"Error posting to Slack: {e.response['error']}") |
|
|
|
|
|
|
|
def run_bot(): |
|
bot.run(DISCORD_TOKEN) |
|
threading.Thread(target=run_bot).start() |
|
def greet(name): |
|
return "Hello " + name + "!" |
|
demo = gr.Interface(fn=greet, inputs="text", outputs="text") |
|
demo.launch() |
|
|