Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,36 +2,25 @@ import os
|
|
2 |
import discord
|
3 |
from discord.ext import commands
|
4 |
from discord_slash import SlashCommand, SlashContext
|
5 |
-
from discord_slash.utils.manage_commands import create_option
|
6 |
import datetime
|
7 |
import requests
|
8 |
-
import gradio as gr
|
9 |
-
import gradio_client as grc
|
10 |
import asyncio
|
11 |
import threading
|
12 |
from threading import Event
|
13 |
-
from typing import Optional
|
14 |
|
15 |
event = Event()
|
16 |
|
17 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
18 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
19 |
|
20 |
-
# Initialize bot and slash commands
|
21 |
intents = discord.Intents.all()
|
22 |
bot = commands.Bot(command_prefix="$", intents=intents)
|
23 |
-
slash = SlashCommand(bot, sync_commands=True)
|
24 |
|
25 |
async def wait(job):
|
26 |
while not job.done():
|
27 |
await asyncio.sleep(0.2)
|
28 |
|
29 |
-
def get_client(session: Optional[str] = None) -> grc.Client:
|
30 |
-
client = grc.Client("https://wop-xxx-opengpt.hf.space/", hf_token=HF_TOKEN)
|
31 |
-
if session:
|
32 |
-
client.session_hash = session
|
33 |
-
return client
|
34 |
-
|
35 |
def truncate_response(response: str) -> str:
|
36 |
ending = "...\nTruncating response to 2000 characters due to discord api limits."
|
37 |
if len(response) > 2000:
|
@@ -41,7 +30,7 @@ def truncate_response(response: str) -> str:
|
|
41 |
|
42 |
@slash.slash(
|
43 |
name="uptime",
|
44 |
-
description="Displays the uptime of the bot."
|
45 |
)
|
46 |
async def uptime(ctx: SlashContext):
|
47 |
"""Displays the uptime of the bot."""
|
@@ -50,7 +39,6 @@ async def uptime(ctx: SlashContext):
|
|
50 |
minutes, seconds = divmod(remainder, 60)
|
51 |
days, hours = divmod(hours, 24)
|
52 |
|
53 |
-
# Create a fancy embed with emojis
|
54 |
embed = discord.Embed(title="Bot Uptime", color=discord.Color.green())
|
55 |
embed.add_field(name="Uptime", value=f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds", inline=False)
|
56 |
embed.set_footer(text="Created by Cosmos")
|
@@ -63,7 +51,6 @@ async def uptime(ctx: SlashContext):
|
|
63 |
)
|
64 |
async def verse(ctx: SlashContext):
|
65 |
"""Returns a random Bible verse."""
|
66 |
-
# Fetch a random Bible verse
|
67 |
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json"
|
68 |
response = requests.get(bible_api_url)
|
69 |
if response.status_code == 200:
|
@@ -72,7 +59,6 @@ async def verse(ctx: SlashContext):
|
|
72 |
else:
|
73 |
passage = "Unable to fetch Bible verse"
|
74 |
|
75 |
-
# Create an embed
|
76 |
embed = discord.Embed(title="Random Bible Verse", description=passage, color=discord.Color.blue())
|
77 |
embed.set_footer(text="Created by Cosmos")
|
78 |
await ctx.send(embed=embed)
|
@@ -83,13 +69,9 @@ async def verse(ctx: SlashContext):
|
|
83 |
)
|
84 |
async def cmds(ctx: SlashContext):
|
85 |
"""Returns a list of commands and bot information."""
|
86 |
-
# Get list of commands
|
87 |
command_list = [f"{command.name}: {command.help}" for command in bot.commands]
|
88 |
-
|
89 |
-
# Get bot information
|
90 |
bot_info = f"Bot Name: {bot.user.name}\nBot ID: {bot.user.id}"
|
91 |
|
92 |
-
# Create an embed
|
93 |
embed = discord.Embed(title="Bot prefix: $", color=discord.Color.blue())
|
94 |
embed.add_field(name="Commands", value="\n".join(command_list), inline=False)
|
95 |
embed.add_field(name="Bot Information", value=bot_info, inline=False)
|
@@ -98,13 +80,10 @@ async def cmds(ctx: SlashContext):
|
|
98 |
await ctx.send(embed=embed)
|
99 |
|
100 |
async def update_status():
|
101 |
-
await bot.wait_until_ready()
|
102 |
while True:
|
103 |
-
# Fetch the number of members in all guilds the bot is connected to
|
104 |
member_count = sum(guild.member_count for guild in bot.guilds)
|
105 |
-
# Update the bot's status
|
106 |
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{member_count} users"))
|
107 |
-
# Wait for 60 seconds before updating again
|
108 |
await asyncio.sleep(60)
|
109 |
|
110 |
@bot.event
|
@@ -113,13 +92,12 @@ async def on_ready():
|
|
113 |
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
114 |
event.set()
|
115 |
print("------")
|
116 |
-
bot.loop.create_task(update_status())
|
117 |
|
118 |
@bot.event
|
119 |
async def on_member_join(member):
|
120 |
channel = discord.utils.get(member.guild.channels, name="👋wellcome-goodbye")
|
121 |
|
122 |
-
# Fetch a random Bible verse
|
123 |
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json"
|
124 |
response = requests.get(bible_api_url)
|
125 |
if response.status_code == 200:
|
@@ -128,14 +106,12 @@ async def on_member_join(member):
|
|
128 |
else:
|
129 |
passage = "Unable to fetch Bible verse"
|
130 |
|
131 |
-
# Create an embed
|
132 |
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())
|
133 |
embed.add_field(name="Random Bible Verse", value=passage, inline=False)
|
134 |
embed.set_footer(text="Created by Cosmos")
|
135 |
|
136 |
await channel.send(embed=embed)
|
137 |
|
138 |
-
# running in thread
|
139 |
def run_bot():
|
140 |
if not DISCORD_TOKEN:
|
141 |
print("DISCORD_TOKEN NOT SET")
|
|
|
2 |
import discord
|
3 |
from discord.ext import commands
|
4 |
from discord_slash import SlashCommand, SlashContext
|
|
|
5 |
import datetime
|
6 |
import requests
|
|
|
|
|
7 |
import asyncio
|
8 |
import threading
|
9 |
from threading import Event
|
|
|
10 |
|
11 |
event = Event()
|
12 |
|
13 |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
|
|
|
16 |
intents = discord.Intents.all()
|
17 |
bot = commands.Bot(command_prefix="$", intents=intents)
|
18 |
+
slash = SlashCommand(bot, sync_commands=True)
|
19 |
|
20 |
async def wait(job):
|
21 |
while not job.done():
|
22 |
await asyncio.sleep(0.2)
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def truncate_response(response: str) -> str:
|
25 |
ending = "...\nTruncating response to 2000 characters due to discord api limits."
|
26 |
if len(response) > 2000:
|
|
|
30 |
|
31 |
@slash.slash(
|
32 |
name="uptime",
|
33 |
+
description="Displays the uptime of the bot."
|
34 |
)
|
35 |
async def uptime(ctx: SlashContext):
|
36 |
"""Displays the uptime of the bot."""
|
|
|
39 |
minutes, seconds = divmod(remainder, 60)
|
40 |
days, hours = divmod(hours, 24)
|
41 |
|
|
|
42 |
embed = discord.Embed(title="Bot Uptime", color=discord.Color.green())
|
43 |
embed.add_field(name="Uptime", value=f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds", inline=False)
|
44 |
embed.set_footer(text="Created by Cosmos")
|
|
|
51 |
)
|
52 |
async def verse(ctx: SlashContext):
|
53 |
"""Returns a random Bible verse."""
|
|
|
54 |
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json"
|
55 |
response = requests.get(bible_api_url)
|
56 |
if response.status_code == 200:
|
|
|
59 |
else:
|
60 |
passage = "Unable to fetch Bible verse"
|
61 |
|
|
|
62 |
embed = discord.Embed(title="Random Bible Verse", description=passage, color=discord.Color.blue())
|
63 |
embed.set_footer(text="Created by Cosmos")
|
64 |
await ctx.send(embed=embed)
|
|
|
69 |
)
|
70 |
async def cmds(ctx: SlashContext):
|
71 |
"""Returns a list of commands and bot information."""
|
|
|
72 |
command_list = [f"{command.name}: {command.help}" for command in bot.commands]
|
|
|
|
|
73 |
bot_info = f"Bot Name: {bot.user.name}\nBot ID: {bot.user.id}"
|
74 |
|
|
|
75 |
embed = discord.Embed(title="Bot prefix: $", color=discord.Color.blue())
|
76 |
embed.add_field(name="Commands", value="\n".join(command_list), inline=False)
|
77 |
embed.add_field(name="Bot Information", value=bot_info, inline=False)
|
|
|
80 |
await ctx.send(embed=embed)
|
81 |
|
82 |
async def update_status():
|
83 |
+
await bot.wait_until_ready()
|
84 |
while True:
|
|
|
85 |
member_count = sum(guild.member_count for guild in bot.guilds)
|
|
|
86 |
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{member_count} users"))
|
|
|
87 |
await asyncio.sleep(60)
|
88 |
|
89 |
@bot.event
|
|
|
92 |
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
93 |
event.set()
|
94 |
print("------")
|
95 |
+
bot.loop.create_task(update_status())
|
96 |
|
97 |
@bot.event
|
98 |
async def on_member_join(member):
|
99 |
channel = discord.utils.get(member.guild.channels, name="👋wellcome-goodbye")
|
100 |
|
|
|
101 |
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json"
|
102 |
response = requests.get(bible_api_url)
|
103 |
if response.status_code == 200:
|
|
|
106 |
else:
|
107 |
passage = "Unable to fetch Bible verse"
|
108 |
|
|
|
109 |
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())
|
110 |
embed.add_field(name="Random Bible Verse", value=passage, inline=False)
|
111 |
embed.set_footer(text="Created by Cosmos")
|
112 |
|
113 |
await channel.send(embed=embed)
|
114 |
|
|
|
115 |
def run_bot():
|
116 |
if not DISCORD_TOKEN:
|
117 |
print("DISCORD_TOKEN NOT SET")
|