wop commited on
Commit
12e0b84
1 Parent(s): 1a26fbe

Update app.py

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