|
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') |
|
|
|
TRIGGERS = { |
|
"discord bot": "<@U051DB2754M>", |
|
"text to speech": "<@U039C2GANMV>", |
|
"tts": "<@U039C2GANMV>", |
|
"asr": "<@U039C2GANMV>", |
|
"musicgen": "<@U039C2GANMV>", |
|
"whisper": "<@U039C2GANMV>", |
|
"speech recognition": "<@U039C2GANMV>", |
|
"bark": "<@U039C2GANMV>", |
|
"autotrain": "<@U01E3LEC2N7>", |
|
"sentence-transformers": "<@U04E4DNPWG7>", |
|
"sentence_transformers": "<@U04E4DNPWG7>", |
|
"setfit": "<@U04E4DNPWG7>", |
|
"sentence transformers": "<@U04E4DNPWG7>", |
|
} |
|
|
|
intents = discord.Intents.all() |
|
intents.messages = True |
|
bot = commands.Bot(command_prefix='!', intents=intents) |
|
|
|
slack_client = WebClient(token=SLACK_BOT_TOKEN) |
|
|
|
@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) |
|
if huggingfolks_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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ask_for_help = await bot.fetch_channel(1019883044724822016) |
|
if ask_for_help.last_message_id == message.channel.id: |
|
content = message.content |
|
try: |
|
|
|
response = slack_client.chat_postMessage( |
|
channel=SLACK_CHANNEL_ID, |
|
text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}" |
|
) |
|
|
|
thread_ts = response['ts'] |
|
slack_client.chat_postMessage( |
|
channel=SLACK_CHANNEL_ID, |
|
text=content, |
|
thread_ts=thread_ts |
|
) |
|
except SlackApiError as e: |
|
print(f"Error posting to Slack: {e.response['error']}") |
|
|
|
|
|
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() |
|
|