forked from gvvq/script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mshop.py
60 lines (54 loc) · 2.37 KB
/
mshop.py
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
@bot.command()
async def mshop(ctx, folder_or_script: str = '1', page_number: int = 1):
import requests
def get_contents(url):
try:
response = requests.get(url)
response.raise_for_status()
contents = response.json()
return contents
except requests.RequestException:
return None
def list_folder_contents(folder_name):
GITHUB_REPO_URL = "https://api.github.com/repos/MayhemBill/script/contents/"
folder_url = f"{GITHUB_REPO_URL}{folder_name}"
contents = get_contents(folder_url)
if contents is not None:
subfolders = [item["name"] for item in contents if item["type"] == "dir"]
return subfolders
else:
return None
def list_scripts_in_folder(folder_name):
GITHUB_REPO_URL = "https://api.github.com/repos/MayhemBill/script/contents/"
folder_url = f"{GITHUB_REPO_URL}{folder_name}"
contents = get_contents(folder_url)
if contents is not None:
scripts = [item["name"] for item in contents if item["type"] == "file" and item["name"].endswith(".py")]
return scripts
else:
return None
try:
page_number = int(folder_or_script)
folder_name = None
except ValueError:
page_number = 1
folder_name = folder_or_script
if folder_name:
folder_contents = list_scripts_in_folder(folder_name)
if folder_contents is not None:
total_scripts = len(folder_contents)
total_pages = (total_scripts + 14) // 15
start_index = (page_number - 1) * 15
end_index = start_index + 15
scripts_on_page = folder_contents[start_index:end_index]
formatted_scripts = "\n".join([f"[ {script} ]" for script in scripts_on_page])
await ctx.send(f"```ini\nScripts in folder '{folder_name}' (Page {page_number}/{total_pages}):\n{formatted_scripts}\n```")
else:
await ctx.send(f"No scripts found in folder '{folder_name}'.")
else:
folders = list_folder_contents("")
if folders is not None:
formatted_folders = "\n".join([f"[ {folder} ]" for folder in folders])
await ctx.send(f"```ini\nAvailable folders:\n{formatted_folders}\n```")
else:
await ctx.send("Unable to fetch folder list.")