-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
156 lines (121 loc) · 6.09 KB
/
app.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
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
# Sync Bot by jjkay03
import discord
from discord.ext import commands
intents = discord.Intents.default(); intents.message_content=True; intents.members=True
client = commands.Bot(command_prefix = "s.", intents=intents)
TOKEN = open("TOKEN.txt", "r").read()
version = "1.0.4"
# The sync server will copy the roles from the main server
main_server_id = 302087475385539007 # The ID of the main server
sync_server_id = 302087475385539007 # The ID of the sync server
# This is the role map, ID of a role on the main server and its equivalent on the sync server
# Main server role ID : Sync server role ID
role_id_map = {
302087475385539007 : 302087475385539007, # Role 1
302087475385539007 : 302087475385539007, # Role 2
302087475385539007 : 302087475385539007 # Role 3
}
# -- EVENTS ---------------------------------------------------------------------
# On ready
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.online)
print(">> BOT IS RUNNING")
print(">> Name: {}".format(client.user.name))
print(">> ID: {}".format(client.user.id))
print(f">> Version: {version}")
# Member update
@client.event
async def on_member_update(before, after):
if before.roles != after.roles and before.guild.id == main_server_id:
print(f"[>] Synchronized roles for {before.name} as they updated in the Main Server.")
await sync_roles(before.guild, client.get_guild(sync_server_id), before.id)
# On member join
@client.event
async def on_member_join(member):
if member.guild.id == sync_server_id:
print(f"[>] Synchronized roles for {member.name} as they joined the Sync Server.")
await sync_roles(client.get_guild(main_server_id), member.guild, member.id)
# -- FUNCTIONS ------------------------------------------------------------------
async def sync_roles(main_server, sync_server, user_id):
main_member = main_server.get_member(user_id)
sync_member = sync_server.get_member(user_id)
if main_member is None:
print(f"[D] User with ID {user_id} not found in Main Server.")
if sync_member is None:
print(f"[D] User with ID {user_id} not found in Sync Server."); return
sync_roles_to_remove = [role for role in sync_member.roles if role.id in role_id_map.values()]
# Remove sync roles from the user on the sync server
if sync_roles_to_remove:
await sync_member.remove_roles(*sync_roles_to_remove, reason="Removing sync roles.")
print(f"[-] Cleared sync roles the role for {sync_member.name} in the Sync Server.")
# Return after role clear if not on main server
if main_member is None:
return
main_user_roles = [role.id for role in main_member.roles]
sync_user_roles = [role.id for role in sync_member.roles]
# Add roles on the sync server that the user has on the main server
for role_id in main_user_roles:
sync_role_id = role_id_map.get(role_id)
if sync_role_id:
sync_role = sync_server.get_role(sync_role_id)
if sync_role:
await sync_member.add_roles(sync_role, reason="Syncing roles from the main server.")
print(f"[+] Gave {sync_member.name} the role {sync_role.name} in the Sync Server.")
# -- COMMANDS -------------------------------------------------------------------
# COMMAND: ping
@client.command(aliases=["Ping","PING", "p", "P"], help="Shows you the ping of the Bot")
async def ping(ctx):
ping = round(client.latency * 1000)
await ctx.send(f"**{ping}ms**")
# COMMAND: sync
@client.command()
async def sync(ctx, user_id: int = None):
if ctx.author.guild_permissions.manage_roles:
main_server = client.get_guild(main_server_id)
sync_server = client.get_guild(sync_server_id)
if main_server is None or sync_server is None:
await ctx.send("One or both of the servers could not be found.")
return
await sync_roles(main_server, sync_server, user_id)
await ctx.send(":ballot_box_with_check: Roles have been synced successfully!")
else:
await ctx.message.delete()
await ctx.send(":x: You do not have the required permissions to use this command.", delete_after=5)
# COMMAND: syncall
@client.command()
async def syncall(ctx):
if ctx.author.guild_permissions.manage_roles:
main_server = client.get_guild(main_server_id)
sync_server = client.get_guild(sync_server_id)
if sync_server is None:
await ctx.send("Sync server not found.")
return
for member in sync_server.members:
await sync_roles(main_server, sync_server, member.id)
print(f"[!] Synchronized roles for {member.name} in the Sync Server.")
await ctx.send(":ballot_box_with_check: Roles have been synced for all members on the sync server!")
else:
await ctx.message.delete()
await ctx.send(":x: You do not have the required permissions to use this command.", delete_after=5)
# COMMAND: removeall
@client.command()
async def removeall(ctx):
if ctx.author.guild_permissions.manage_roles:
sync_server = client.get_guild(sync_server_id)
if sync_server is None:
await ctx.send("Sync server not found.")
return
# Iterate through all members on the sync server
for member in sync_server.members:
sync_roles = [role for role in member.roles if role.id in role_id_map.values()]
if sync_roles:
await member.remove_roles(*sync_roles, reason="Removing all sync roles.")
print(f"[-] Removed sync roles from {member.name} in the Sync Server.")
await ctx.send(":ballot_box_with_check: Removed all sync roles from everyone on the sync server!")
else:
await ctx.message.delete()
await ctx.send(":x: You do not have the required permissions to use this command.", delete_after=5)
# -- CLIENT RUN -----------------------------------------------------------------
# Client Run
client.run(TOKEN)