wop commited on
Commit
1a26fbe
1 Parent(s): ab725eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -27
app.py CHANGED
@@ -1,26 +1,34 @@
1
- import os
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:
@@ -28,29 +36,28 @@ def truncate_response(response: str) -> str:
28
  else:
29
  return response
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."""
37
  delta = datetime.datetime.utcnow() - bot.start_time
38
  hours, remainder = divmod(int(delta.total_seconds()), 3600)
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")
45
 
46
  await ctx.send(embed=embed)
47
 
48
- @slash.slash(
49
- name="verse",
50
- description="Returns a random Bible verse."
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,19 +66,21 @@ async def verse(ctx: SlashContext):
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)
65
 
66
- @slash.slash(
67
- name="cmds",
68
- description="Returns a list of commands and bot information."
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,10 +89,13 @@ async def cmds(ctx: SlashContext):
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,12 +104,13 @@ async def on_ready():
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,12 +119,15 @@ async def on_member_join(member):
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")
 
 
 
 
 
 
 
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:
 
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:
 
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)
 
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
 
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:
 
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")