modbot / app.py
lunarflu's picture
lunarflu HF Staff
[main] another indentation fix
f805a66
raw
history blame
4.17 kB
import discord
from discord import app_commands
from discord.ext import commands
import gradio_client
import gradio as gr
from gradio_client import Client
import os #
import threading
import requests
import json
import random
import time
from PIL import Image
import asyncio
import glob
import falcon
from falcon import try_falcon
from falcon import continue_falcon
import deepfloydif
from deepfloydif import deepfloydif_stage_1
from deepfloydif import deepfloydif_stage_2
from deepfloydif import deepfloydif_stage_2_react_check
#-------------------------------------------------------------------------------------------------------------------------------------
MY_GUILD = discord.Object(id=1077674588122648679) # HF = 879548962464493619, test = 1077674588122648679
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
#-------------------------------------------------------------------------------------------------------------------------------------
class MyClient(discord.Client):
"""This structure allows slash commands to work instantly (instead of needing to sync global commands for up to an hour)"""
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
# This copies the global commands over to our guild
self.tree.copy_global_to(guild=MY_GUILD)
await self.tree.sync(guild=MY_GUILD)
client = MyClient(intents=discord.Intents.all())
#-------------------------------------------------------------------------------------------------------------------------------------
@client.event
async def on_ready():
print(f'Logged in as {client.user} (ID: {client.user.id})')
print('------')
#-------------------------------------------------------------------------------------------------------------------------------------
@client.tree.command()
@app_commands.describe(
prompt='Enter some text to chat with the bot! Like this: /falcon Hello, how are you?')
async def falcon(interaction: discord.Interaction, prompt: str):
"""Command that begins a new conversation with Falcon"""
try:
await try_falcon(interaction, prompt)
except Exception as e:
print(f"Error: {e}")
#-------------------------------------------------------------------------------------------------------------------------------------
@client.event
async def on_message(message):
"""Checks channel and continues Falcon conversation if it's the right Discord Thread"""
try:
await continue_falcon(message)
await message.remove_reaction('πŸ”', client.user) # test=πŸ” hf=πŸ”
except Exception as e:
print(f"Error: {e}")
#-------------------------------------------------------------------------------------------------------------------------------------
@client.tree.command()
@app_commands.describe(
prompt='Enter a prompt to generate an image! Can generate realistic text, too!')
async def deepfloydif(interaction: discord.Interaction, prompt: str):
"""DeepfloydIF stage 1 generation"""
try:
await deepfloydif_stage_1(interaction, prompt, client)
except Exception as e:
print(f"Error: {e}")
#-------------------------------------------------------------------------------------------------------------------------------------
@client.event
async def on_reaction_add(reaction, user):
"""Checks for a reaction in order to call dfif2"""
try:
await deepfloydif_stage_2_react_check(reaction, user)
except Exception as e:
print(f"Error: {e} (known error, does not cause issues, low priority)")
#-------------------------------------------------------------------------------------------------------------------------------------
"""This allows us to run the Discord bot in a Python thread"""
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
def run_bot():
client.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.queue(concurrency_count=20)
demo.launch()