Spaces:
Paused
Paused
File size: 4,423 Bytes
421cf0e f4645b0 c6b9f53 bbc8b82 f4645b0 421cf0e dbaa346 421cf0e f4645b0 ab725eb f4645b0 421cf0e f4645b0 ab725eb f4645b0 68da665 885d112 baa57de 885d112 68da665 f4645b0 99d64f1 03013ee 99d64f1 f4645b0 99d64f1 8ecb673 99d64f1 abe125b ab725eb abe125b a52bb9d 972b032 39669cf ab725eb a936ad9 39669cf 885d112 b9a119d 885d112 baa57de 885d112 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import os
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
import datetime
import requests
import asyncio
import threading
from threading import Event
event = Event()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
HF_TOKEN = os.getenv("HF_TOKEN")
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="$", intents=intents)
slash = SlashCommand(bot, sync_commands=True)
async def wait(job):
while not job.done():
await asyncio.sleep(0.2)
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
@slash.slash(
name="uptime",
description="Displays the uptime of the bot."
)
async def uptime(ctx: SlashContext):
"""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)
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)
@slash.slash(
name="verse",
description="Returns a random Bible verse."
)
async def verse(ctx: SlashContext):
"""Returns 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']}** - \n{verse['text']}"
else:
passage = "Unable to fetch Bible verse"
embed = discord.Embed(title="Random Bible Verse", description=passage, color=discord.Color.blue())
embed.set_footer(text="Created by Cosmos")
await ctx.send(embed=embed)
@slash.slash(
name="cmds",
description="Returns a list of commands and bot information."
)
async def cmds(ctx: SlashContext):
"""Returns a list of commands and bot information."""
command_list = [f"{command.name}: {command.help}" for command in bot.commands]
bot_info = f"Bot Name: {bot.user.name}\nBot ID: {bot.user.id}"
embed = discord.Embed(title="Bot prefix: $", color=discord.Color.blue())
embed.add_field(name="Commands", value="\n".join(command_list), inline=False)
embed.add_field(name="Bot Information", value=bot_info, inline=False)
embed.set_footer(text="Created by Cosmos")
await ctx.send(embed=embed)
async def update_status():
await bot.wait_until_ready()
while True:
member_count = sum(guild.member_count for guild in bot.guilds)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{member_count} users"))
await asyncio.sleep(60)
@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("------")
bot.loop.create_task(update_status())
@bot.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name="👋wellcome-goodbye")
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"
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)
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()
|