|
| 1 | +import networkx as nx |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import disnake |
| 4 | + |
| 5 | +# Function to fetch server data dynamically |
| 6 | +def fetch_discord_server_data(guild): |
| 7 | + graph_data = {} |
| 8 | + for channel in guild.channels: |
| 9 | + if isinstance(channel, disnake.TextChannel): |
| 10 | + threads = [thread.name for thread in channel.threads] |
| 11 | + graph_data[channel.name] = threads |
| 12 | + return graph_data |
| 13 | + |
| 14 | +# Function to shorten names to avoid overlaps |
| 15 | +def shorten_name(name, max_length=15): |
| 16 | + return name if len(name) <= max_length else name[:max_length] + "..." |
| 17 | + |
| 18 | +# Function to generate the Discord map |
| 19 | +def generate_discord_map(graph_data, output_file="server_map.png"): |
| 20 | + # Create a directed graph |
| 21 | + G = nx.DiGraph() |
| 22 | + |
| 23 | + # Add nodes and edges |
| 24 | + for channel, threads in graph_data.items(): |
| 25 | + G.add_node(channel, type='channel', color='lightblue', size=2000) |
| 26 | + for thread in threads: |
| 27 | + shortened_thread = shorten_name(thread) |
| 28 | + G.add_node(shortened_thread, type='thread', color='lightgreen', size=1000) |
| 29 | + G.add_edge(channel, shortened_thread) |
| 30 | + |
| 31 | + # Extract attributes for styling |
| 32 | + node_colors = [data['color'] for _, data in G.nodes(data=True)] |
| 33 | + node_sizes = [data['size'] for _, data in G.nodes(data=True)] |
| 34 | + |
| 35 | + # Draw the graph |
| 36 | + plt.figure(figsize=(16, 12)) |
| 37 | + |
| 38 | + # Use a shell layout for better spatial distribution |
| 39 | + pos = nx.shell_layout(G) |
| 40 | + |
| 41 | + # Draw nodes and edges |
| 42 | + nx.draw( |
| 43 | + G, pos, |
| 44 | + with_labels=True, |
| 45 | + labels={node: shorten_name(node) for node in G.nodes()}, |
| 46 | + node_size=node_sizes, |
| 47 | + node_color=node_colors, |
| 48 | + font_size=8, |
| 49 | + font_color='black', |
| 50 | + edge_color='gray', |
| 51 | + alpha=0.8 |
| 52 | + ) |
| 53 | + |
| 54 | + # Set title and save the map |
| 55 | + plt.title("Discord Server Map", fontsize=16) |
| 56 | + plt.savefig(output_file) |
| 57 | + plt.show() |
| 58 | + |
| 59 | +async def generate_map(inter: disnake.ApplicationCommandInteraction): |
| 60 | + await inter.response.defer() |
| 61 | + |
| 62 | + # Fetch graph data |
| 63 | + graph_data = fetch_discord_server_data(inter.guild) |
| 64 | + |
| 65 | + # Generate and save the map |
| 66 | + generate_discord_map(graph_data) |
| 67 | + |
| 68 | + await inter.followup.send(file=disnake.File("server_map.png")) |
0 commit comments