-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguild_manager.py
More file actions
167 lines (146 loc) · 5.43 KB
/
guild_manager.py
File metadata and controls
167 lines (146 loc) · 5.43 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
guild_data = {}
is_loading = False
@nightyScript(
name="Guilds Manager v1.0",
author="simnJS",
description="Discord server management interface with visual guild listing and leave functionality.",
usage="UI Script - Use the Guild Manager tab to view and leave servers"
)
def GuildManagerScript():
"""
GUILDS MANAGER SCRIPT v1.0
--------------------------
Discord server management interface for viewing and leaving servers.
Displays all joined Discord servers in an organized two-column layout
with individual Leave buttons for each server.
FEATURES:
- Visual server listing with server names and icons
- Leave servers with one click
- Toast notifications for feedback
- Automatic interface updates
- Alphabetically sorted server list
USAGE:
Access the "Guilds Manager" tab in Nighty to:
• View all servers the bot is connected to
• Click "Leave" next to any server to disconnect
• Get instant feedback through notifications
NOTES:
- Interface automatically refreshes after leaving servers
- Leave operations are safe and include error handling
- Supports any number of servers with scroll functionality
CHANGELOG:
v1.0 - Initial release
- Complete guild management interface
- Leave functionality with notifications
- Two-column layout with server cards
"""
import asyncio
global guild_data, is_loading
def debug_log(message):
print(f"[Guild Manager Debug] {message}")
def log_message(message, level="INFO"):
print(f"[{level}] {message}")
def leave_guild_sync(guild_id):
"""Leave a guild synchronously"""
try:
guild = bot.get_guild(int(guild_id))
if not guild:
return False, "Guild not found"
name = guild.name
future = asyncio.run_coroutine_threadsafe(guild.leave(), bot.loop)
future.result(timeout=10)
guild_data.pop(guild_id, None)
return True, name
except Exception as e:
return False, str(e)
def leave_guild_handler(guild_id):
"""Handler to leave a specific guild"""
success, result = leave_guild_sync(guild_id)
if success:
gm_tab.toast(
title="Left Server",
description=f"You have left {result}",
type="SUCCESS"
)
load_guild_data()
else:
gm_tab.toast(
title="Error",
description=f"Failed to leave server: {result}",
type="ERROR"
)
def make_leave_handler(guild_id):
"""Create a unique handler with __name__ defined"""
def handler():
leave_guild_handler(guild_id)
handler.__name__ = f"leave_handler_{guild_id}"
return handler
def initialize_ui():
"""Initialize or reset the Tab and main container"""
nonlocal gm_tab, main_container
gm_tab = Tab(name="Guilds Manager", icon="preferences", title="Guilds Manager")
main_container = gm_tab.create_container(
type="rows",
gap=4,
height="auto",
width="full",
vertical_align="start",
overflow="auto"
)
def load_guild_data():
"""Load guild data and create cards in pairs"""
global guild_data, is_loading
if is_loading:
return
is_loading = True
guild_data.clear()
try:
initialize_ui()
guilds = sorted(bot.guilds, key=lambda g: g.name.lower())
for i in range(0, len(guilds), 2):
pair = guilds[i:i+2]
row_container = main_container.create_container(
type="columns",
gap=4,
horizontal_align="start",
vertical_align="start"
)
for guild in pair:
current_guild_id = guild.id
current_guild_name = guild.name
guild_data[current_guild_id] = {
"name": current_guild_name,
"guild_object": guild
}
guild_card = row_container.create_card(gap=4)
header_group = guild_card.create_group(
type="columns",
gap=2,
vertical_align="center"
)
header_group.create_ui_element(
UI.Text,
content=f"🏛️ {current_guild_name}",
size="lg",
full_width=True
)
header_group.create_ui_element(
UI.Button,
label="Leave",
variant="solid",
color="danger",
onClick=make_leave_handler(current_guild_id)
)
except Exception as e:
log_message(f"Error loading guilds: {e}", "ERROR")
finally:
is_loading = False
gm_tab.render()
try:
main_container = None
gm_tab = None
load_guild_data()
log_message("✅ Guild Manager UI initialized successfully")
except Exception as e:
print(f"Initialization error: {e}", type_="ERROR")
GuildManagerScript()