File size: 2,643 Bytes
421cf0e
 
 
 
 
 
c6b9f53
421cf0e
 
dbaa346
421cf0e
 
 
 
 
 
 
 
dbaa346
421cf0e
 
 
 
 
 
 
 
dbaa346
421cf0e
 
 
 
 
 
 
 
 
 
 
 
 
5dc344d
1b3d256
421cf0e
68da665
 
 
 
 
 
 
 
 
421cf0e
 
 
c6b9f53
421cf0e
 
 
abe125b
 
 
 
 
 
 
 
 
 
d6bf2a5
 
a52bb9d
972b032
8278126
 
306f828
a936ad9
421cf0e
 
 
 
 
 
 
 
 
1a45034
421cf0e
 
 
 
 
 
306f828
421cf0e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import asyncio
import os
import threading
from threading import Event
from typing import Optional

import datetime
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)

@bot.command()
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)
    await ctx.send(f"Uptime: {days} days, {hours} hours, {minutes} minutes, {seconds} seconds")


@bot.event
async def on_ready():
    bot.start_time = datetime.datetime.utcnow()
    print(f"Logged in as {bot.user} (ID: {bot.user.id})")
    event.set()
    print("------")
    await update_status()
    
async def update_status():
    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)
        
bot.loop.create_task(update_status())

@bot.event
async def on_member_join(mb):
    channelv0 = discord.utils.get(mb.guild.channels, name = "👋wellcome-goodbye")
    await channelv0.send(f"Welcome to server, <@{mb.id}>! ")


# 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()