async def EditImage(self, ctx, ImageName): 'if an image is no longer in the discord bots cache this command is used to approve/reject it. e.g. $EditImage ImageRef' #set sending user sending_user = ctx.author.mention #define channel channel_name = 'image-approval' #call the function to create the channel channel = await self.CallCheckChannels('Bot', channel_name) #check the image is in either json SuccessfulyFoundMessageAppreciation = await self.GetImageToEdit( ImageName, AppreciationDirectory, 'Appreciation', channel) SuccessfulyFoundMessageMeme = await self.GetImageToEdit( ImageName, MemeDirectory, 'Memes', channel) #if it was found in a json if SuccessfulyFoundMessageAppreciation or SuccessfulyFoundMessageMeme: #just return to prevent the script from running further return else: #set error title and message Title = str(f'Message') Content = str( f'{sending_user}, no image is found by the reference {ImageName}' ) #send the embeded message return await ctx.send(embed=Error(Title, Content))
async def Image(self, ctx, MemeName): 'This posts the Image using the ImageRef/ImageName if it is not added to a society. e.g. $Image ImageRef/ImageName' with open(MemeDirectory) as f: #load the contents data = json.load(f) #loops through the json file for i in data: #look for Memes if i == 'Memes': #go through the multiple memes for Meme in data['Memes']: if Meme["Name"] == MemeName and Meme[ "ApprovalStatus"] == "Approved": return await ctx.send(Meme["URL"]) elif Meme["ApprovalStatus"] == "Denied": #send an error #set error title and message Title = str('Image has been Denied') Content = str( f'This Image has been denied {Meme["ApprovingUser"]}' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) elif Meme["ApprovalStatus"] == "Waiting": #send an error #set error title and message Title = str('Image is still pending') Content = str( f'This Image has not been approved or denied') #send the embeded message return await ctx.send(embed=Error(Title, Content)) #send an error #set error title and message Title = str('No image found') Content = str(f'No image has been found by that name') #send the embeded message return await ctx.send(embed=Error(Title, Content))
async def Smile(self, ctx): "Sends a meme image" if UseFoldersCheck(): #image location path = 'Memes/Smile.jpg' if PathExist(path): #if yes pushes the image return await ctx.send(file=discord.File(path)) else: #send an error #set error title and message Title = str('Can\'t find image') Content = str( f'Image can\'t be posted, check logs to know more') #send the embeded message await ctx.send(embed=Error(Title, Content)) else: return await ctx.send(GetMoneySmileURL())
async def Money(self, ctx): 'This sends the money meme to the channel it is sent' if UseFoldersCheck(): #image location path = 'Memes/Money.jpg' #check if the file exist if PathExist(path): #if yes pushes the image return await ctx.send(file=discord.File(path)) else: #send an error #set error title and message Title = str('Can\'t find image') Content = str( f'Image can\'t be posted, check logs to know more') #send the embeded message await ctx.send(embed=Error(Title, Content)) else: return await ctx.send(GetMoneyMemeURL())
async def UnloadLoop(self, channel): #set variables ChannelToUnload = channel.name #set error title and message Title = str(f'{ChannelToUnload} Loop Error') Content = str(f'This loop has stoppped, see the logs to know more') #send the embeded message await channel.send(embed=Error(Title, Content)) #logic to decide which loop to unload if ChannelToUnload == 'magic-mike-appreciation-society': print('MM loop has been unloaded') self.mm_image_loop.cancel() elif ChannelToUnload == 'chris-roberts-appreciation-society': print('CR loop has been unloaded') self.cr_image_loop.cancel() elif ChannelToUnload == 'keanu-reeves-appreciation-society': print('KR loop has been unloaded') self.kr_image_loop.cancel()
async def AddImage(self, ctx, ImageName, ImageURL): 'This command submits an image for approval. if the image name is kr/cr/mm then it is submitted for the appropriate society. e.g. $AddImage ImageName ImageURL' ValidImage = ImageURLValidator(ImageURL) if not ValidImage: Title = str('Invalid URL') Content = str( f'The follwing URL {ImageURL} is not a valid picture.\nPlease use a valid url' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) #define channel channel_name = 'image-approval' #call the function to create the channel channel = await self.CallCheckChannels('Bot', channel_name) #sending users name sending_user = ctx.author.mention #set variables to lower ImageNameLower = ImageName.lower() #set the approprite json to use if ImageNameLower == 'kr' or ImageNameLower == 'cr' or ImageNameLower == 'mm': DirectoryToUse = AppreciationDirectory JSONObjectName = 'Appreciation' ImageAppreciationSociety = await self.GetAppreciationName(ImageName ) else: DirectoryToUse = MemeDirectory JSONObjectName = 'Memes' with open(DirectoryToUse) as f: #load the contents data = json.load(f) #loops through the json file for i in data: #look for images if i == JSONObjectName: #go through the multiple images for Image in data[JSONObjectName]: #if the Image is waiting to be approved send the following message if Image["URL"] == ImageURL and Image[ "ApprovalStatus"] == "Waiting": #set error title and message Title = str(f'Message') Content = str( f'{sending_user}, this image has already been submitted and is waiting to be approved' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) #if the Image has been approved send the following message elif Image["URL"] == ImageURL and Image[ "ApprovalStatus"] == "Approved": #set error title and message Title = str(f'Message') Content = str( f'{sending_user}, this image has already been approved by {Image["ApprovingUser"]}' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) elif Image["URL"] == ImageURL and Image[ "ApprovalStatus"] == "Denied": #set error title and message Title = str(f'Message') Content = str( f'{sending_user}, this image has already been Denied by {Image["ApprovingUser"]}' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) #let the user know the message needs approval UniqueReferenceID = await ctx.send( f'{sending_user}, your image has been submitted for approval') UniqueID = UniqueReferenceID.id #set title and message e = discord.Embed(title="Image for approval", color=discord.Color.from_rgb(255, 255, 0)) if JSONObjectName == 'Appreciation': e.add_field( name=str(f'Message'), value=str( f'Please approve the following {ImageAppreciationSociety} image. \n\nURL: {ImageURL} \n\nReference: {UniqueID}' )) elif JSONObjectName == 'Memes': e.add_field( name=str(f'Message'), value=str( f'Please approve the following image. \n\nURL: {ImageURL} \n\nReference: {ImageName}' )) #send the embeded message message = await channel.send(embed=e) emojis = ['✅', '❌'] for emoji in emojis: await message.add_reaction(emoji) message_id = message.id with open(DirectoryToUse) as f: #load the contents data = json.load(f) if JSONObjectName == 'Appreciation': NewData = { "MessageID": message_id, "Name": UniqueID, "URL": ImageURL, "ApprovalStatus": "Waiting", "UserID": sending_user, "ApprovingUser": "", "AppreciationSociety": ImageNameLower, "ImageID": "" } elif JSONObjectName == 'Memes': NewData = { "MessageID": message_id, "Name": ImageName, "URL": ImageURL, "ApprovalStatus": "Waiting", "UserID": sending_user, "ApprovingUser": "" } #set the new json data[JSONObjectName].append(NewData) #write the new token to the file created with open(DirectoryToUse, "w") as write_file: json.dump(data, write_file, indent=4)
async def ApprovalList(self, ctx, AddRemove, UserToAddRemove): 'This adds/removes a user from the approval list by @, e.g. $ApprovalList Add/Remove @User' #set the AddRemove to lower AddRemoveLower = AddRemove.lower() #check if owner if ctx.message.author.id != OwnerID: #set error title and message Title = str(f'Not an Owner') Content = str(f'Need to be an owner to access this commmand') #send the embeded message await ctx.send(embed=Error(Title, Content)) #check a valid argument elif AddRemoveLower != 'add' and AddRemoveLower != 'remove': #set error title and message Title = str(f'Not a valid argument') Content = str(f'Please use Add or Remove') #send the embeded message await ctx.send(embed=Error(Title, Content)) #check if a valid user elif '@' not in UserToAddRemove: #set error title and message Title = str(f'Not a valid user') Content = str(f'Please @ the user you wish to add') #send the embeded message await ctx.send(embed=Error(Title, Content)) #strip the @ from the id StrippedUserID = re.sub('[^0-9]', '', UserToAddRemove) #set variable UserFound = False AddedOrRemoved = '' #open/create the json file with open(DirectoryConfig) as f: data = json.load(f) #look through all the users in the list for user in data['ApprovalList']: #if a user is found if user == StrippedUserID: #set variable to let it know a user has been found UserFound = True #if they are trying to be added if AddRemoveLower == 'add': #prepare an error letting them know they are already on the list Title = str(f'User already added') Content = str( f'The user you are trying to add has already been added' ) #send the embeded message return await ctx.send(embed=Error(Title, Content)) #if it is to remove them they will be removed elif AddRemoveLower == 'remove': data['ApprovalList'].remove(StrippedUserID) AddedOrRemoved = 'Removed' if not UserFound: if AddRemoveLower == 'add': data['ApprovalList'].append(StrippedUserID) AddedOrRemoved = 'Added' elif AddRemoveLower == 'remove': #prepare an error letting them know they are already on the list Title = str(f'User not found') Content = str( f'The user you are trying to remove was not found') #send the embeded message return await ctx.send(embed=Error(Title, Content)) #save the new json with open(DirectoryConfig, "w") as f: json.dump(data, f, indent=4) e = discord.Embed(title="User approvals", color=discord.Color.green()) e.add_field(name=str(f'Message'), value=str(f'{UserToAddRemove} has been {AddedOrRemoved}')) return await ctx.send(embed=e)
async def ApproveDenyImage(self, reaction, user, MessageID, DirectoryToUse, JSONObjectName): with open(DirectoryToUse) as f: #load the contents data = json.load(f) #loops through the json file for i in data: #look for image if i == JSONObjectName: #go through the multiple image for Image in data[JSONObjectName]: #if the message id is found in the json if Image["MessageID"] == MessageID: Image #check for the tick emoji if reaction.emoji == '✅': if user.id != OwnerID: if OwnerApproval: #set error title and message Title = str(f'Error') Content = str( f'Only admins can approve images. Please contact <@{OwnerID}>' ) #send the embeded message return await reaction.message.channel.send( embed=Error(Title, Content)) #check ig group approval is used if GroupApproval: #sets variables for is a user is not found UserFound = False #go through the users in the list for user in GroupApproval: #if a user is found if user.id == user: #set variable to true UserFound = True #if they are not found if not UserFound: #set error title and message Title = str(f'Error') Content = str( f'You do not have permission to approve images. To see who can approve images use $ApprovalListUsers' ) #send the embeded message return await reaction.message.channel.send( embed=Error(Title, Content)) #check if the image has already been approved if Image["ApprovalStatus"] == "Approved": #check first to send the appropriate messgae if Image[ "AppreciationSociety"] and JSONObjectName == 'Appreciation': await reaction.message.channel.send( f'{Image["UserID"]}, your image has already been approved by <@{user.id}>. It is now in rotation.' ) else: #if so let the user know await reaction.message.channel.send( f'{Image["UserID"]}, your image has already been approved by <@{user.id}>. Use the following command to call it: \n$Image {Image["Name"]}' ) else: #set the approval status to approved Image["ApprovalStatus"] = "Approved" #if it is looking for an appriciation image if Image[ "AppreciationSociety"] and JSONObjectName == 'Appreciation': #set the appropriate file to open dynamically AppreciationSociatyDirectory = str( f'Images/{Image["AppreciationSociety"]}.json' ) if os.path.isfile( AppreciationSociatyDirectory): #open the json file with open( AppreciationSociatyDirectory ) as s: #load the contents DataAppreciation = json.load(s) #get the length of the file to determine next key name LengthOfFile = len( DataAppreciation) #set the new key in the appreciation file DataAppreciation[ LengthOfFile] = Image[ "URL"] #set that key value in the file Image["ImageID"] = LengthOfFile #save the new json with open( AppreciationSociatyDirectory, "w") as s: json.dump(DataAppreciation, s, indent=4) #check first to send the appropriate messgae if Image[ "AppreciationSociety"] and JSONObjectName == 'Appreciation': await reaction.message.channel.send( f'{Image["UserID"]}, your image has already been approved by <@{user.id}>. It is now in rotation.' ) else: #if so let the user know await reaction.message.channel.send( f'{Image["UserID"]}, your image has already been approved by <@{user.id}>. Use the following command to call it: \n$Image {Image["Name"]}' ) #check for the tick emoji elif reaction.emoji == '❌': if Image["ApprovalStatus"] == "Denied": #if so let the user know await reaction.message.channel.send( f'{Image["UserID"]}, your image has already been denied by <@{user.id}>.' ) else: #set the approval status to denied Image["ApprovalStatus"] = "Denied" #if it is looking for an appriciation image if Image[ "AppreciationSociety"] and JSONObjectName == 'Appreciation' and Image[ "ImageID"]: #set the appropriate file to open dynamically AppreciationSociatyDirectory = str( f'Images/{Image["AppreciationSociety"]}.json' ) if os.path.isfile( AppreciationSociatyDirectory): #open the json file with open( AppreciationSociatyDirectory ) as s: #load the contents DataAppreciation = json.load(s) #set the image key ImageKeyString = str( Image['ImageID']) #Delete the key from the file del DataAppreciation[ ImageKeyString] #set that key value in the file Image["ImageID"] = "" #save the new json with open( AppreciationSociatyDirectory, "w") as s: json.dump(DataAppreciation, s, indent=4) #let the user know await reaction.message.channel.send( f'{Image["UserID"]}, your image has been denied' ) Image["ApprovingUser"] = f'<@{user.id}>' #save the new json with open(DirectoryToUse, "w") as f: json.dump(data, f, indent=4) #remove reaction await reaction.message.remove_reaction( reaction.emoji, user)
async def ChangeApprovalType(self, ctx, ApprovalType, ApprovalBool): sending_user = ctx.author.id OwnerID = ctx.guild.owner_id if sending_user != OwnerID: #set error title and message Title = str(f'Invalid Permission') Content = str( f'You (<@{sending_user}>) do not have valid permission. Please contact <@{OwnerID}>' ) #send the embeded message await ctx.send(embed=Error(Title, Content)) #set variables ApprovalBoolLower = ApprovalBool.lower() ApprovalTypeLower = ApprovalType.lower() ApprovalMethod = '' if ApprovalBoolLower == 'true' or ApprovalBoolLower == 'false': #open/create the json file with open(DirectoryConfig) as f: data = json.load(f) if ApprovalTypeLower == 'owner': ApprovalMethod = 'OwnerApproval' elif ApprovalTypeLower == 'list': ApprovalMethod = 'GroupApproval' #checks what the user has sent to be set if ApprovalBoolLower == 'true': #check the value stored already if data[ApprovalMethod] == 'True': #if it is the current vlaue it will set the appropriate messgae #set title and message e = discord.Embed(title=f"{ApprovalType} approvals", color=discord.Color.green()) e.add_field( name=str(f'Message'), value=str( f'{ApprovalType} approvals has already been set to true.' )) else: e = discord.Embed(title=f"{ApprovalType} approvals", color=discord.Color.green()) if ApprovalTypeLower == 'owner': #set approval to true in the config data['OwnerApproval'] = 'True' data['GroupApproval'] = 'False' #set title and message e.add_field( name=str(f'Message'), value=str( f'Owner approvals has been set. Only <@{OwnerID}> can approve images' )) elif ApprovalTypeLower == 'list': #set approval to true in the config data['OwnerApproval'] = 'False' data['GroupApproval'] = 'True' #set title and message e = discord.Embed(title="Group approvals", color=discord.Color.green()) #set variable GroupApprovalListString = '' #if users are in a list if GroupApprovalList: #go through each user for user in GroupApprovalList: #concat into a string GroupApprovalListString = (f"{user}\n") #set the field to use e.add_field(name=str(f'Users who can approve'), value=str(GroupApprovalListString)) else: e.add_field( name=str(f'Message'), value=str( f'Group approvals has been set. Add users to the list' )) elif ApprovalBoolLower == 'false': #check the value stored already if data[ApprovalMethod] == 'False': #if it is the current vlaue it will set the appropriate messgae #set title and message e = discord.Embed(title=f"{ApprovalType} approvals", color=discord.Color.green()) e.add_field( name=str(f'Message'), value=str( f'{ApprovalType} approvals has already been set to false.' )) else: #set approval to true in the config data[ApprovalMethod] = 'False' #set title and message e = discord.Embed(title=f"{ApprovalType} approvals", color=discord.Color.green()) e.add_field( name=str(f'Message'), value=str( f'{ApprovalType} approvals has been set. eveyone can approve images' )) if not data['OwnerID']: #find the correct key and set it data['OwnerID'] = OwnerID #save the new json with open(DirectoryConfig, "w") as f: json.dump(data, f, indent=4) #send the embeded message await ctx.send(embed=e) else: #if it is not a valid argument #set error title and message Title = str(f'Invalid Argument') Content = str(f'command needs to be True/False') #send the embeded message await ctx.send(embed=Error(Title, Content))