Skip to content

Instantly share code, notes, and snippets.

@Immotay
Last active July 30, 2023 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Immotay/280104daf505338638028ec51dfcb5b6 to your computer and use it in GitHub Desktop.
Save Immotay/280104daf505338638028ec51dfcb5b6 to your computer and use it in GitHub Desktop.
Gets FC member list, compares to Discord users with or without FC role and adds or remove said role accordingly.
############################################
# CHAT GPT'S ENHANCED CODE WAS NOT TESTED! #
############################################
import json
import requests as req
# API endpoint for retrieving FC information
xivAPI_link = "https://xivapi.com/freecompany/9234349560946624156?data=fcm"
# Name of the Discord role associated with the FC membership
FCroleName = 'Bolinho'
# ID of the Discord role associated with the FC membership
FCroleID = 1016945192126906368
# Whitedlisted Users
whitelisted = ['85091960884322304']
@client.command(name="remove")
async def remove_fc_role(context):
# Check if the user is authorized to use this command
if str(context.message.author.id) not in whitelisted:
await context.message.channel.send("You are not authorized to use this command.")
return
# Fetch FC information from the API
url = req.get(xivAPI_link)
if url.status_code != 200:
await context.message.channel.send("Failed to retrieve FC information from the API.")
return
try:
data = url.json()
# Extract the character names of FC members from the API response
fc_members = [member['Name'] for member in data['FreeCompanyMembers']]
except (KeyError, ValueError):
await context.message.channel.send("Failed to parse FC information.")
return
# Get the Discord role associated with the FC membership
fc_role = discord.utils.get(context.message.guild.roles, name=FCroleName)
if not fc_role:
await context.message.channel.send("The specified FC role doesn't exist.")
return
# Get the list of members who have the FC role and a nickname
members_with_fc_role = [member for member in context.message.guild.members if fc_role in member.roles and member.nick]
# Find members who have the FC role but are not in the FC (based on their nickname)
members_without_fc = [member for member in members_with_fc_role if member.nick not in fc_members]
if not members_without_fc:
await context.message.channel.send("All members with the FC role are already in the FC.")
return
# Remove the FC role from members who are not in the FC
for member in members_without_fc:
await member.remove_roles(fc_role)
print(f'Removed {FCroleName} from {member.nick}')
removed_members_mentions = [member.mention for member in members_without_fc]
await context.message.channel.send('Removed the FC role from the following members:\n' + '\n'.join(removed_members_mentions))
@client.command(name="add")
async def add_fc_role(context):
# Check if the user is authorized to use this command
if str(context.message.author.id) not in whitelisted:
await context.message.channel.send("You are not authorized to use this command.")
return
# Fetch FC information from the API
url = req.get(xivAPI_link)
if url.status_code != 200:
await context.message.channel.send("Failed to retrieve FC information from the API.")
return
try:
data = url.json()
# Extract the character names of FC members from the API response
fc_members = [member['Name'] for member in data['FreeCompanyMembers']]
except (KeyError, ValueError):
await context.message.channel.send("Failed to parse FC information.")
return
# Get the Discord role associated with the FC membership
fc_role = discord.utils.get(context.message.guild.roles, name=FCroleName)
if not fc_role:
await context.message.channel.send("The specified FC role doesn't exist.")
return
# Get the list of members who do not have the FC role and have a nickname
members_without_fc_role = [member for member in context.message.guild.members if fc_role not in member.roles and member.nick]
# Find members who do not have the FC role but are in the FC (based on their nickname)
members_in_fc = [member for member in members_without_fc_role if member.nick in fc_members]
if not members_in_fc:
await context.message.channel.send("No members found in the FC to add the role.")
return
# Add the FC role to members who are in the FC
for member in members_in_fc:
await member.add_roles(fc_role)
print(f'Added {FCroleName} to {member.nick}')
added_members_mentions = [member.mention for member in members_in_fc]
await context.message.channel.send('Added the FC role to the following members:\n' + '\n'.join(added_members_mentions))
import json
import requests as req
xivAPI_link = "https://xivapi.com/freecompany/9234349560946624156?data=fcm"
FCroleName = 'Bolinho'
FCroleID = 1016945192126906368
whitelisted = ['85091960884322304']
###
# THIS ASSUMES THAT EVERYONE IN YOUR DISCORD USES THEIR CHARACTER NAME AS THEIR NICKNAMES AS ENFORCED BY OTHER FFXIV RELATED BOTS VERIFICATION METHOD
###
######################
# REMOVE ROLE CROSS CHECK
######################
@client.command(name="remove")
async def update(context):
if str(context.message.author.id) in whitelisted:
url = req.get(xivAPI_link) #gets FC information
text = url.text
data = json.loads(text)
listaFFXIV = []
member = data['FreeCompanyMembers'] #Creates a lis with the FC members.
for x in range(0,len(member)):
listaFFXIV += list(member[x].values())[4].split(",") # For each fc member, gets value [4] (character name)
await context.message.channel.send("Member list populated with a total of "+str(len(listaFFXIV))+" members.") #sends a message on the channel with the amount of members in the list.
#print('\n'.join(listaFFXIV))
listaDISCORD = []
role = discord.utils.get(context.message.guild.roles, name=FCroleName) #Gets Discord Role information
if role is None: #If role doesn't exist, then exit.
return
empty = True
for member in context.message.guild.members: # Goes through every discord member
if role in member.roles: # if member has your FC role
listaDISCORDusr = "{}".format(member.nick) # gets member nickname
usr = ''.join(listaDISCORDusr) # concatenates member nickname (couldn't find another way to do this)
if usr != 'None': # ignore user if he does not have a nickname, otherwise
listaDISCORD.append(usr) # adds that user to a list
await context.message.channel.send("Encontrados "+str(len(listaDISCORD))+" bolinhos com o cargo") # announces i nthe chanel how many people have your FC role
remover = [user for user in listaDISCORD if user not in listaFFXIV] # For every member that has your role but is not in your FC, creates a new list
lista = []
cargo = context.guild.get_role(FCroleID) # gets role info
for entry in remover: # for every user in the removal list
usuario = discord.utils.get(context.guild.members, nick=entry) # gets their discord user info through their nickname
await usuario.remove_roles(cargo) # remove their role
print(f'Removed {role} from {entry}') # print statement
lista.append(usuario.mention) # converts nickname list to mentions
await context.message.channel.send('Removido cargo dos seguintes membros: \n'+'\n'.join(lista)) # sends said list on a channel so you can double check if everything went well.
empty = False
if empty:
print("There is no one")
return
######################
# ADD ROLE CROSS CHECK
######################
@client.command(name="add")
async def update(context):
if str(context.message.author.id) in whitelisted:
url = req.get(xivAPI_link) # gets fc information
text = url.text
data = json.loads(text)
listaFFXIV = []
member = data['FreeCompanyMembers'] # creates member list from FC
for x in range(0,len(member)):
listaFFXIV += list(member[x].values())[4].split(",") # gets their character names
await context.message.channel.send("Member list populated with a total of "+str(len(listaFFXIV))+" members.")
listaDISCORD = []
role = discord.utils.get(context.message.guild.roles, name=FCroleName) # gets role info
if role is None: # if no role then exit
return
empty = True
for member in context.message.guild.members: # for every user in your discord
if role not in member.roles: # if user DOES NOT have your FC role
listaDISCORDusr = "{}".format(member.nick) # gets their nickname
usr = ''.join(listaDISCORDusr) # again, just a fix I couldn't find another way to do
if usr != 'None': # if no nickname ignore, otherwise
listaDISCORD.append(usr) # adds that user to a list
await context.message.channel.send("Found "+str(len(listaDISCORD))+" users without the role") # announces how many users there are without your fc role
adicionar = [user for user in listaFFXIV if user in listaDISCORD] # for every user in XIV not with discord role, creates a third list
lista = []
cargo = context.guild.get_role(FCroleID)
for entry in adicionar:
usuario = discord.utils.get(context.guild.members, nick=entry)
await usuario.add_roles(cargo)
print(f'Added {role} to {entry}')
lista.append(usuario.mention) # converts list into mention list
await context.message.channel.send('I have given the FC role to the following Discord Users: \n'+'\n'.join(lista)) # anounces who got the role
empty = False
if empty:
print("There is no one")
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment