Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
import threading
|
| 4 |
-
import json
|
| 5 |
import random
|
| 6 |
from threading import Event
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
-
import datetime
|
| 10 |
import requests
|
| 11 |
import discord
|
| 12 |
import gradio as gr
|
|
@@ -17,277 +15,27 @@ from discord.utils import oauth_url
|
|
| 17 |
from gradio_client.utils import QueueError
|
| 18 |
|
| 19 |
event = Event()
|
| 20 |
-
|
| 21 |
-
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
| 22 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 23 |
-
|
| 24 |
-
def ask(input_text):
|
| 25 |
-
api_url = "https://somerandomapifor-disorc-cause-i-need-it.onrender.com/qa"
|
| 26 |
-
params = {'question': input_text}
|
| 27 |
-
|
| 28 |
-
response = requests.get(api_url, params=params)
|
| 29 |
-
|
| 30 |
-
if response.status_code == 200:
|
| 31 |
-
json_response = response.json()
|
| 32 |
-
answer = json_response.get('answer') # Assuming the JSON response has an 'answer' field
|
| 33 |
-
|
| 34 |
-
# Applying the operation to the answer
|
| 35 |
-
modified_answer = answer[:-4] if answer else "No answer found."
|
| 36 |
-
|
| 37 |
-
return modified_answer
|
| 38 |
-
else:
|
| 39 |
-
return "Error: Unable to fetch response"
|
| 40 |
|
| 41 |
async def wait(job):
|
| 42 |
while not job.done():
|
| 43 |
await asyncio.sleep(0.2)
|
| 44 |
|
| 45 |
-
intents = discord.Intents.all()
|
| 46 |
-
bot = commands.Bot(command_prefix="$", intents=intents, help_command=None)
|
| 47 |
-
|
| 48 |
-
@bot.command()
|
| 49 |
-
async def uptime(ctx):
|
| 50 |
-
"""Displays the uptime of the bot."""
|
| 51 |
-
delta = datetime.datetime.utcnow() - bot.start_time
|
| 52 |
-
hours, remainder = divmod(int(delta.total_seconds()), 3600)
|
| 53 |
-
minutes, seconds = divmod(remainder, 60)
|
| 54 |
-
days, hours = divmod(hours, 24)
|
| 55 |
-
|
| 56 |
-
# Create a fancy embed with emojis
|
| 57 |
-
embed = discord.Embed(title="Bot Uptime", color=discord.Color.green())
|
| 58 |
-
embed.add_field(name="Uptime", value=f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds", inline=False)
|
| 59 |
-
embed.set_footer(text="Created by Cosmos")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#@bot.command()
|
| 64 |
-
#async def ai(ctx, *, input_text: str):
|
| 65 |
-
# """Ask our AI model a question."""
|
| 66 |
-
# async with ctx.typing():
|
| 67 |
-
#result = ask(input_text)
|
| 68 |
-
# result = "the ai is disabled currently"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
# await ctx.reply(result)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
@bot.command()
|
| 75 |
-
async def verse(ctx):
|
| 76 |
-
"""Get a random Bible verse."""
|
| 77 |
-
# Fetch a random Bible verse
|
| 78 |
-
bible_api_url = "https://labs.bible.org/api/?passage=random&type=json"
|
| 79 |
-
response = requests.get(bible_api_url)
|
| 80 |
-
if response.status_code == 200:
|
| 81 |
-
verse = response.json()[0]
|
| 82 |
-
passage = f"**{verse['bookname']} {verse['chapter']}:{verse['verse']}** \n{verse['text']}"
|
| 83 |
-
else:
|
| 84 |
-
passage = "Unable to fetch Bible verse"
|
| 85 |
-
|
| 86 |
-
# Create an embed
|
| 87 |
-
embed = discord.Embed(title="Random Bible Verse", description=passage, color=discord.Color.blue())
|
| 88 |
-
embed.set_footer(text="Created by Cosmos")
|
| 89 |
-
await ctx.reply(embed=embed)
|
| 90 |
-
|
| 91 |
-
@bot.command()
|
| 92 |
-
@commands.has_permissions(kick_members=True)
|
| 93 |
-
async def kick(ctx, member: discord.Member, *, reason: Optional[str] = "No reason provided"):
|
| 94 |
-
"""Kicks a member."""
|
| 95 |
-
try:
|
| 96 |
-
await member.kick(reason=reason)
|
| 97 |
-
await ctx.reply(f"{member.mention} has been kicked. Reason: {reason}")
|
| 98 |
-
except discord.Forbidden:
|
| 99 |
-
await ctx.reply("I don't have permission to kick members.")
|
| 100 |
-
except discord.HTTPException:
|
| 101 |
-
await ctx.reply("An error occurred while trying to kick the member. Please try again later.")
|
| 102 |
-
|
| 103 |
-
@bot.command()
|
| 104 |
-
@commands.has_permissions(ban_members=True)
|
| 105 |
-
async def ban(ctx, member: discord.Member, *, reason: Optional[str] = "No reason provided"):
|
| 106 |
-
"""Bans a member."""
|
| 107 |
-
try:
|
| 108 |
-
await member.ban(reason=reason)
|
| 109 |
-
await ctx.reply(f"{member.mention} has been banned. Reason: {reason}")
|
| 110 |
-
except discord.Forbidden:
|
| 111 |
-
await ctx.reply("I don't have permission to ban members.")
|
| 112 |
-
except discord.HTTPException:
|
| 113 |
-
await ctx.reply("An error occurred while trying to ban the member. Please try again later.")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
@bot.command()
|
| 117 |
-
async def cmds(ctx):
|
| 118 |
-
"""Returns a list of commands and bot information."""
|
| 119 |
-
# Get list of commands
|
| 120 |
-
command_list = [f"{command.name}: {command.help}" for command in bot.commands]
|
| 121 |
-
|
| 122 |
-
# Get bot information
|
| 123 |
-
bot_info = f"Bot Name: {bot.user.name}\nBot ID: {bot.user.id}"
|
| 124 |
-
|
| 125 |
-
# Create an embed
|
| 126 |
-
embed = discord.Embed(title="Bot prefix: $", color=discord.Color.blue())
|
| 127 |
-
embed.add_field(name="Commands", value="\n".join(command_list), inline=False)
|
| 128 |
-
embed.add_field(name="Bot Information", value=bot_info, inline=False)
|
| 129 |
-
embed.set_footer(text="Created by Cosmos")
|
| 130 |
-
|
| 131 |
-
await ctx.reply(embed=embed)
|
| 132 |
-
|
| 133 |
-
async def update_status():
|
| 134 |
-
await bot.wait_until_ready() # Wait until the bot is fully ready
|
| 135 |
-
while True:
|
| 136 |
-
# Fetch the number of members in all guilds the bot is connected to
|
| 137 |
-
member_count = sum(guild.member_count for guild in bot.guilds)
|
| 138 |
-
# Update the bot's status
|
| 139 |
-
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{member_count} users"))
|
| 140 |
-
# Wait for 60 seconds before updating again
|
| 141 |
-
await asyncio.sleep(60)
|
| 142 |
-
|
| 143 |
-
@bot.event
|
| 144 |
-
async def on_ready():
|
| 145 |
-
bot.start_time = datetime.datetime.utcnow()
|
| 146 |
-
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
| 147 |
-
event.set()
|
| 148 |
-
print("------")
|
| 149 |
-
bot.loop.create_task(update_status()) # Create the task within the on_ready event
|
| 150 |
-
|
| 151 |
-
@bot.event
|
| 152 |
-
async def on_member_join(member):
|
| 153 |
-
# Fetch the channels by their IDs
|
| 154 |
-
channel_ids = [1210566384267034644, 1210987054658494477, 1210578060164866128]
|
| 155 |
-
channels_info = []
|
| 156 |
-
for channel_id in channel_ids:
|
| 157 |
-
channel = member.guild.get_channel(channel_id)
|
| 158 |
-
if channel:
|
| 159 |
-
channels_info.append(f"• {channel.name}: {channel.mention}")
|
| 160 |
-
|
| 161 |
-
# Prepare the message content
|
| 162 |
-
channels_message = "\n".join(channels_info)
|
| 163 |
-
message_content = (
|
| 164 |
-
f"Welcome to the server, {member.name}!\n\n"
|
| 165 |
-
f"Hope you have a great stay here, {member.mention}!\n\n"
|
| 166 |
-
f"Here are some channels you might find interesting:\n{channels_message}\n\n"
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
# Load Bible verses JSON data
|
| 170 |
-
verses_json = '''
|
| 171 |
-
{
|
| 172 |
-
"verses": [
|
| 173 |
-
{
|
| 174 |
-
"reference": "**Romans 12:10**",
|
| 175 |
-
"text": "“Be devoted to one another in love. Honor one another above yourselves.”"
|
| 176 |
-
},
|
| 177 |
-
{
|
| 178 |
-
"reference": "**John 4:20**",
|
| 179 |
-
"text": "“If someone says, ‘I love God,’ and hates his brother, he is a liar; for he who does not love his brother whom he has seen, how can he love God whom he has not seen?”"
|
| 180 |
-
},
|
| 181 |
-
{
|
| 182 |
-
"reference": "**Ephesians 4:32**",
|
| 183 |
-
"text": "“And be kind to one another, tenderhearted, forgiving one another, even as God in Christ forgave you.”"
|
| 184 |
-
},
|
| 185 |
-
{
|
| 186 |
-
"reference": "**John 13:34-35**",
|
| 187 |
-
"text": "“A new commandment I give to you, that you love one another; as I have loved you, that you also love one another. By this, all will know that you are My disciples if you have love for one another.”"
|
| 188 |
-
},
|
| 189 |
-
{
|
| 190 |
-
"reference": "**Corinthians 12:26**",
|
| 191 |
-
"text": "“And if one member suffers, all the members suffer with it; or if one member is honored, all the members rejoice with it.”"
|
| 192 |
-
}
|
| 193 |
-
]
|
| 194 |
-
}
|
| 195 |
-
'''
|
| 196 |
-
|
| 197 |
-
verses_data = json.loads(verses_json)
|
| 198 |
-
random_verse = random.choice(verses_data["verses"]) # Selecting the first verse for demonstration
|
| 199 |
-
|
| 200 |
-
# Include the Bible verse in the message content
|
| 201 |
-
message_content += f"\n\nRandom Bible Verse:\n{random_verse['reference']} - {random_verse['text']}"
|
| 202 |
-
|
| 203 |
-
# Send a direct message to the user with the message content including the Bible verse
|
| 204 |
-
try:
|
| 205 |
-
await member.send(message_content)
|
| 206 |
-
except discord.HTTPException:
|
| 207 |
-
print(f"Failed to send a DM to {member.name}")
|
| 208 |
-
|
| 209 |
-
# Create an embed for the welcome channel
|
| 210 |
-
welcome_channel = discord.utils.get(member.guild.channels, name="👋wellcome-goodbye")
|
| 211 |
-
if welcome_channel:
|
| 212 |
-
embed = discord.Embed(
|
| 213 |
-
title=f"Welcome to the server, {member.name}!",
|
| 214 |
-
description=f"Hope you have a great stay here, {member.mention}!",
|
| 215 |
-
color=discord.Color.blue()
|
| 216 |
-
)
|
| 217 |
-
embed.add_field(name="Random Bible Verse", value=f"{random_verse['reference']} - {random_verse['text']}", inline=False)
|
| 218 |
-
embed.set_footer(text="Created by Cosmos")
|
| 219 |
-
await welcome_channel.send(embed=embed)
|
| 220 |
-
|
| 221 |
-
@bot.command()
|
| 222 |
-
async def search(ctx, *, query: str):
|
| 223 |
-
"""Search for a bible verse."""
|
| 224 |
-
bible_api_url = f"http://labs.bible.org/api/?passage={query}&formatting=plain&type=json"
|
| 225 |
-
try:
|
| 226 |
-
response = requests.get(bible_api_url)
|
| 227 |
-
response.raise_for_status()
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
# If verses are found, concatenate them into a single message
|
| 235 |
-
passage = "\n".join(verse_texts)
|
| 236 |
-
#passage = truncate_response(passage)
|
| 237 |
-
embed = discord.Embed(title=f"Search Results for '{query}'", description=passage, color=discord.Color.blue())
|
| 238 |
-
else:
|
| 239 |
-
embed = discord.Embed(title="Search Results", description="No results found.", color=discord.Color.red())
|
| 240 |
-
except requests.RequestException as e:
|
| 241 |
-
embed = discord.Embed(title="Search Results", description=f"Error: {e}", color=discord.Color.red())
|
| 242 |
|
| 243 |
-
embed.set_footer(text="Created by Cosmos")
|
| 244 |
-
await ctx.reply(embed=embed)
|
| 245 |
|
| 246 |
-
@bot.command()
|
| 247 |
-
@commands.has_permissions(manage_messages=True)
|
| 248 |
-
async def purge(ctx, limit: int):
|
| 249 |
-
"""Removes N amount of messages."""
|
| 250 |
-
try:
|
| 251 |
-
await ctx.channel.purge(limit=limit + 1) # Add 1 to include the command message itself
|
| 252 |
-
await ctx.reply(f"{limit} messages have been purged.")
|
| 253 |
-
except discord.Forbidden:
|
| 254 |
-
await ctx.reply("I don't have permission to manage messages.")
|
| 255 |
-
except discord.HTTPException:
|
| 256 |
-
await ctx.reply("An error occurred while trying to purge messages. Please try again later.")
|
| 257 |
|
| 258 |
|
| 259 |
-
@bot.event
|
| 260 |
-
async def on_command_error(ctx, error):
|
| 261 |
-
if isinstance(error, commands.CommandNotFound):
|
| 262 |
-
await ctx.reply("Sorry, I couldn't find that command. Use `$cmds` to see the list of available commands.")
|
| 263 |
-
elif isinstance(error, commands.MissingRequiredArgument):
|
| 264 |
-
await ctx.reply("Oops! Looks like you're missing some required arguments.")
|
| 265 |
-
elif isinstance(error, commands.CheckFailure):
|
| 266 |
-
await ctx.reply("You do not have the permissions to execute this command.")
|
| 267 |
-
else:
|
| 268 |
-
# Log the error to console or your logging system
|
| 269 |
-
print(f"An error occurred: {error}")
|
| 270 |
|
| 271 |
-
#
|
| 272 |
-
@bot.event
|
| 273 |
-
async def on_error(event_method, *args, **kwargs):
|
| 274 |
-
# Log the error to console or your logging system
|
| 275 |
-
print(f"An error occurred in {event_method}: {sys.exc_info()}")
|
| 276 |
|
| 277 |
-
# Error handling for unhandled exceptions
|
| 278 |
-
@bot.event
|
| 279 |
-
async def on_command_error(ctx, error):
|
| 280 |
-
if isinstance(error, commands.CommandInvokeError):
|
| 281 |
-
original_error = error.original
|
| 282 |
-
if isinstance(original_error, discord.Forbidden):
|
| 283 |
-
await ctx.send("I don't have permissions to do that.")
|
| 284 |
-
elif isinstance(original_error, discord.HTTPException):
|
| 285 |
-
await ctx.send("An error occurred while processing the command. Please try again later.")
|
| 286 |
-
else:
|
| 287 |
-
# Log the error to console or your logging system
|
| 288 |
-
print(f"Error: {original_error}")
|
| 289 |
-
await ctx.send("An unexpected error occurred.")
|
| 290 |
-
|
| 291 |
# running in thread
|
| 292 |
def run_bot():
|
| 293 |
if not DISCORD_TOKEN:
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
import threading
|
|
|
|
| 4 |
import random
|
| 5 |
from threading import Event
|
| 6 |
from typing import Optional
|
| 7 |
|
|
|
|
| 8 |
import requests
|
| 9 |
import discord
|
| 10 |
import gradio as gr
|
|
|
|
| 15 |
from gradio_client.utils import QueueError
|
| 16 |
|
| 17 |
event = Event()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
async def wait(job):
|
| 20 |
while not job.done():
|
| 21 |
await asyncio.sleep(0.2)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# settings v ####################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
intents = discord.Intents.all()
|
| 29 |
+
bot = commands.Bot(command_prefix="$", intents=intents, help_command=None)
|
| 30 |
+
|
| 31 |
+
# bot stuff v ####################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# end of stuff v ####################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# running in thread
|
| 40 |
def run_bot():
|
| 41 |
if not DISCORD_TOKEN:
|