Spaces:
Paused
Paused
import asyncio | |
import os | |
import threading | |
from threading import Event | |
from typing import Optional | |
import datetime | |
import requests | |
import discord | |
import gradio as gr | |
import gradio_client as grc | |
from discord import Permissions | |
from discord.ext import commands | |
from discord.utils import oauth_url | |
from gradio_client.utils import QueueError | |
event = Event() | |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
async def wait(job): | |
while not job.done(): | |
await asyncio.sleep(0.2) | |
def get_client(session: Optional[str] = None) -> grc.Client: | |
client = grc.Client("https://wop-xxx-opengpt.hf.space/", hf_token=HF_TOKEN) | |
if session: | |
client.session_hash = session | |
return client | |
def truncate_response(response: str) -> str: | |
ending = "...\nTruncating response to 2000 characters due to discord api limits." | |
if len(response) > 2000: | |
return response[: 2000 - len(ending)] + ending | |
else: | |
return response | |
intents = discord.Intents.all() | |
bot = commands.Bot(command_prefix="$", intents=intents) | |
async def uptime(ctx): | |
"""Displays the uptime of the bot.""" | |
delta = datetime.datetime.utcnow() - bot.start_time | |
hours, remainder = divmod(int(delta.total_seconds()), 3600) | |
minutes, seconds = divmod(remainder, 60) | |
days, hours = divmod(hours, 24) | |
# Create a fancy embed with emojis | |
embed = discord.Embed(title="Bot Uptime", color=discord.Color.green()) | |
embed.add_field(name="Uptime", value=f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds", inline=False) | |
embed.set_footer(text="Created by Cosmos") | |
await ctx.send(embed=embed) | |
async def update_status(): | |
await bot.wait_until_ready() # Wait until the bot is fully ready | |
while True: | |
# Fetch the number of members in all guilds the bot is connected to | |
member_count = sum(guild.member_count for guild in bot.guilds) | |
# Update the bot's status | |
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{member_count} users")) | |
# Wait for 60 seconds before updating again | |
await asyncio.sleep(60) | |
async def on_ready(): | |
bot.start_time = datetime.datetime.utcnow() | |
print(f"Logged in as {bot.user} (ID: {bot.user.id})") | |
event.set() | |
print("------") | |
bot.loop.create_task(update_status()) # Create the task within the on_ready event | |
async def on_member_join(member): | |
channel = discord.utils.get(member.guild.channels, name="👋wellcome-goodbye") | |
# Fetch a random Bible verse | |
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json" | |
response = requests.get(bible_api_url) | |
if response.status_code == 200: | |
verse = response.json()[0] | |
passage = f"{verse['bookname']} {verse['chapter']}:{verse['verse']} - {verse['text']}" | |
else: | |
passage = "Unable to fetch Bible verse" | |
# Create an embed | |
embed = discord.Embed(title=f"Welcome to the server, {member.name}!", description=f"Hope you have a great stay here, <@{member.id}>!", color=discord.Color.blue()) | |
embed.add_field(name="Random Bible Verse", value=passage, inline=False) | |
embed.set_footer(text="Created by Cosmos") | |
await channel.send(embed=embed) | |
# running in thread | |
def run_bot(): | |
if not DISCORD_TOKEN: | |
print("DISCORD_TOKEN NOT SET") | |
event.set() | |
else: | |
bot.run(DISCORD_TOKEN) | |
threading.Thread(target=run_bot).start() | |
event.wait() | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
f""" | |
# Discord bot is online! | |
""" | |
) | |
demo.launch() | |