Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ldap3
- import discord
- from discord.ext import commands
- # LDAP server configuration
- LDAP_SERVER = 'ldap://lldap.slothflix.xyz:25628'
- LDAP_ADMIN_DN = 'uid=admin,ou=people,dc=example,dc=com'
- LDAP_ADMIN_PASSWORD = 'Password'
- LDAP_USERS_OU = 'ou=people,dc=example,dc=com'
- LDAP_GROUPS_OU = 'ou=groups,dc=example,dc=com'
- # Discord bot configuration
- DISCORD_BOT_TOKEN = 'Token'
- SPECIFIED_ROLE = '900655128418349057'
- GROUP_TO_ADD = 'Subscriber'
- IGNORED_ROLES = ['900655128418349057', '900655128418349057']
- DISCORD_CHANNEL = '900643903525503058'
- # Initialize LDAP server connection
- server = ldap3.Server(LDAP_SERVER, get_info=ldap3.ALL)
- admin_connection = ldap3.Connection(server, user=LDAP_ADMIN_DN, password=LDAP_ADMIN_PASSWORD, auto_bind=True)
- # Initialize Discord bot
- intents = discord.Intents.all()
- intents.members = True
- bot = commands.Bot(command_prefix='/', intents=intents)
- print('The Bot is running')
- # Link LDAP user to Discord user
- @bot.command(name='link')
- @commands.has_role(900655128418349057)
- async def link(ctx, discord_username, ldap_username):
- try:
- # Check if LDAP user exists
- admin_connection.search(LDAP_USERS_OU, '(uid={})'.format(ldap_username), attributes=['uid'])
- if len(admin_connection.entries) == 0:
- await ctx.send('The specified LDAP user does not exist.')
- return
- # Link Discord user to LDAP user
- user = ctx.guild.get_member(int(discord_username))
- if user is None:
- await ctx.send('The specified Discord user does not exist.')
- return
- else:
- await ctx.send('Linking {} to LDAP user {}.'.format(user.name, ldap_username))
- user_link_attribute = 'linkedLdapUser'
- user_link_attribute_value = ldap_username
- user_roles = [role.name for role in user.roles]
- # Check if user already has the link attribute
- if user_link_attribute in user.public_flags:
- await ctx.send('This Discord user is already linked to an LDAP user.')
- return
- # Add link attribute to user
- user_edit_dict = {user_link_attribute: user_link_attribute_value}
- linked_ldap_user_flag = 1 # Choose a custom integer value for the flag
- linked_ldap_user_role = discord.utils.get(ctx.guild.roles, name='linkedLdapUser')
- if linked_ldap_user_role:
- await user.add_roles(linked_ldap_user_role)
- else:
- await ctx.send('The linkedLdapUser role does not exist. Please create the role in the server.')
- # Add user to specified group if necessary
- if SPECIFIED_ROLE not in user_roles:
- for ignored_role in IGNORED_ROLES:
- if ignored_role in user_roles:
- await ctx.send('The specified user is an admin and will not be added to the group.')
- return
- linked_ldap_user_role = discord.utils.get(ctx.guild.roles, name='linkedLdapUser')
- if linked_ldap_user_role:
- await user.add_roles(linked_ldap_user_role)
- else:
- await ctx.send('The linkedLdapUser role does not exist. Please create the role in the server.')
- group_dn = 'cn={},{}'.format(GROUP_TO_ADD, LDAP_GROUPS_OU)
- user_dn = 'uid={},{}'.format(ldap_username, LDAP_USERS_OU)
- admin_connection.modify(group_dn, {'member': [(ldap3.MODIFY_ADD, [user_dn])]})
- await ctx.send('Added user to {} group.'.format(GROUP_TO_ADD))
- except Exception as e:
- await ctx.send('An error occurred while linking the user. Error message: {}'.format(str(e)))
- # Run the Discord bot
- bot.run(DISCORD_BOT_TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement