async def setAlignment(message, embed, creator, payload): url = embed.author.url ranges = (await getDetailFromHistory(message, creator)).upper() while " " in ranges: ranges = ranges.replace(" ", " ") if (ranges.split(" ")[0] not in ["ALL", "NONE"]): ranges = [ x.strip() for x in re.sub(r'[^A-Z0-9: ]', '', ranges).split(" ") ] # keep good chars, split into sep ranges for i in range(len(ranges)): ranges[i] = numericToA1(a1ToNumeric(ranges[i])) ranges = ", ".join(ranges) else: ranges = ranges.title() if (payload.emoji.name == RandomSupport.numberEmojis[5]): embed = updateDetailInURL(embed, "leftAligned", ranges.replace(" ", "")) elif (payload.emoji.name == RandomSupport.numberEmojis[6]): embed = updateDetailInURL(embed, "rightAligned", ranges.replace(" ", "")) elif (payload.emoji.name == RandomSupport.numberEmojis[7]): embed = updateDetailInURL(embed, "centered", ranges.replace(" ", "")) ranges = ranges.split(",") for i in range(len(ranges)): if (ranges[i][-1] == "0"): ranges[i] = ranges[i][:-1] embed = updateFieldValue(embed, payload.emoji.name, spaceChar * 2 + " " + "".join(ranges)) await message.edit(embed=embed)
async def setTableRange(message, embed, creator): moBotMember = message.guild.get_member(moBot) url = embed.author.url tableDetails = getTableFromEmbedURL(url) workbook = await openUserSpreadsheet(tableDetails.workbookKey, message.channel, creator, moBotMember) if (workbook is not None): tableRange = await getDetailFromHistory(message, creator) if (tableRange is not None): tableRange = numericToA1( a1ToNumeric(tableRange)) # validated range embed = updateDetailInURL(embed, "tableRange", tableRange) embed = updateFieldValue( embed, "Table Range", spaceChar * 2 + " " + (tableRange if tableRange[-1] != "0" else tableRange[:-1])) await message.edit(embed=embed) else: await messageNotFound(message, creator, "Table Range", RandomSupport.numberEmojis[2]) else: return
async def setWorksheetID(message, embed, creator): moBotMember = message.guild.get_member(moBot) url = embed.author.url tableDetails = getTableFromEmbedURL(url) workbook = await openUserSpreadsheet(tableDetails.workbookKey, message.channel, creator, moBotMember) if (workbook is not None): sheetName = await getDetailFromHistory(message, creator) if (sheetName is not None): # sheet was given worksheet = await getSheetFromSheetName(workbook, sheetName, message, creator) if (worksheet is not None): # sheet given is valid embed = updateDetailInURL(embed, "worksheetID", worksheet.id) embed = updateFieldValue(embed, "Sheet Name", spaceChar * 2 + " " + worksheet.title) await message.edit(embed=embed) else: # message already sent if sheet is none return else: await messageNotFound(message, creator, "Sheet Name", RandomSupport.numberEmojis[2]) else: # message already sent if workbook is none return
async def toggleSmartMerging(message, embed): url = embed.author.url smartMergingStatus = int(getDetailFromURL(url, "smartMerging")) smartMergingStatus = 1 if smartMergingStatus is 0 else 0 smartMerging = "Yes" if smartMergingStatus is 1 else "No" embed = updateDetailInURL(embed, "smartMerging", smartMergingStatus) embed = updateFieldValue(embed, "Smart Merging", spaceChar * 2 + " " + smartMerging) await message.edit(embed=embed)
async def toggleAutoUpdate(message, embed): url = embed.author.url autoUpdateStatus = int(getDetailFromURL(url, "autoUpdating")) autoUpdateStatus = 1 if autoUpdateStatus is 0 else 0 autoUpdate = "Yes" if autoUpdateStatus is 1 else "No" embed = updateDetailInURL(embed, "autoUpdating", autoUpdateStatus) embed = updateFieldValue(embed, "Auto Updating", spaceChar * 2 + " " + autoUpdate) await message.edit(embed=embed)
async def setBufferMessages(message, embed, creator): url = embed.author.url bufferMessages = await getDetailFromHistory(message, creator) try: bufferMessages = str(int(bufferMessages.split(" ")[0])) except ValueError: # no number given await message.channel.send( "**Invalid Value for `Number of Buffer Messages`**\n%s, Should be a number... 0, 1, 5, etc." % creator.mention) return embed = updateDetailInURL(embed, "bufferMessages", bufferMessages) embed = updateFieldValue(embed, "Number of Buffer Messages", spaceChar * 2 + " " + bufferMessages) await message.edit(embed=embed)
async def setNumberOfHeaders(message, embed, creator): url = embed.author.url numberOfHeaders = await getDetailFromHistory(message, creator) try: numberOfHeaders = str(int(numberOfHeaders.split(" ")[0])) except ValueError: # no number given await message.channel.send( "**Invalid Value for `Number of Headers`**\n%s, Should be a number... 0, 1, 5, etc." % creator.mention) return if (int(numberOfHeaders) > -1): embed = updateDetailInURL(embed, "headers", numberOfHeaders) embed = updateFieldValue(embed, "Number of Headers", spaceChar * 2 + " " + numberOfHeaders) await message.edit(embed=embed)
async def sendTable(tableDetails, message, client): # embed may be None try: guild = client.get_guild(tableDetails.guildID) channel = guild.get_channel(tableDetails.channelID) creator = guild.get_member(tableDetails.creatorID) moBotMember = guild.get_member(moBot) except discord.errors.NotFound: return workbook = await openUserSpreadsheet(tableDetails.workbookKey, channel, creator, moBotMember) tables = createTable(tableDetails, workbook) msgIDs = [int(msgID) for msgID in tableDetails.messageIDs] if (msgIDs[0] == -1): msgIDs = [] async def clearMessages(): await channel.trigger_typing() for msgID in msgIDs: try: msg = await channel.fetch_message(msgID) await msg.delete() except discord.errors.NotFound: pass return [] # end clearMessages async def sendNewMessages(): await channel.trigger_typing() for table in tables: msg = await channel.send(table) msgIDs.append(msg.id) # end sendNewMessages async def sendBufferMessages(): msg = await channel.fetch_message(msgIDs[0]) for i in range( len(tables) + tableDetails.bufferMessages - len(msgIDs)): if (msg.created_at + relativedelta(minutes=7) > datetime.utcnow()): msg = await channel.send(spaceChar) msgIDs.append(msg.id) else: await clearMessages() await sendNewMessages() await sendBufferMessages() break # end sendBufferMessages if (tables is not None): await channel.trigger_typing() for msgID in msgIDs: # first assuming table is already created try: msg = await channel.fetch_message(msgID) await msg.edit(content=tables[msgIDs.index(msgID)]) except discord.errors.NotFound: # if message doesn't exist, delete all existing messages msgIDs = await clearMessages() await sendNewMessages() break except IndexError: # less tables than messages for msgID in msgIDs[len(tables):]: try: msg = await channel.fetch_message(msgID) await msg.edit(content=spaceChar) except discord.errors.NotFound: pass if (len(msgIDs) < len(tables)): # more tables than existing messages for table in tables[len(msgIDs):]: msg = await channel.send(table) msgIDs.append(msg.id) await sendBufferMessages() if (message is not None): embed = message.embeds[0] embed = updateDetailInURL( embed, "messageIDs", ",".join([str(msgID) for msgID in msgIDs])) await message.edit(embed=embed) tableDetails.messageIDs = [str(msgID) for msgID in msgIDs] saveTable(tableDetails)
async def sendTable(tableDetails, message, client): # embed may be None refreshed = datetime.strftime(datetime.utcnow(), "*Refreshed: %b %d %H:%M UTC*") try: guild = client.get_guild(tableDetails.guildID) channel = guild.get_channel(tableDetails.channelID) creator = guild.get_member(tableDetails.creatorID) moBotMember = guild.get_member(moBot) except discord.errors.NotFound: return except AttributeError: return workbook = await openUserSpreadsheet(tableDetails.workbookKey, channel, creator, moBotMember) tables = createTable(tableDetails, workbook) msgIDs = [int(msgID) for msgID in tableDetails.messageIDs] if (msgIDs[0] == -1): msgIDs = [] async def clearMessages(): await channel.trigger_typing() for msgID in msgIDs: try: msg = await channel.fetch_message(msgID) await msg.delete() except discord.errors.NotFound: pass return [] # end clearMessages async def sendNewMessages(): await channel.trigger_typing() for table in tables: msg = await channel.send( "%s\n%s" % (refreshed if (tables.index(table) == 0) else "", table)) msgIDs.append(msg.id) # end sendNewMessages async def sendBufferMessages(): og_msg = await channel.fetch_message(msgIDs[0]) buffer_messages_present = 0 for i, msgID in enumerate(msgIDs): msg = await channel.fetch_message(msgID) buffer_messages_present += 1 if msg.content == spaceChar else 0 if og_msg.created_at + relativedelta(minutes=7) > datetime.utcnow( ): # this means if the table is trying to be updated after 7 minutes, it will not send new buffer messages if the buffer message number is increased for i in range(buffer_messages_present, tableDetails.bufferMessages): msg = await channel.send(spaceChar) msgIDs.append(msg.id) # end sendBufferMessages if (tables is not None): await channel.trigger_typing() for msgID in msgIDs: # first assuming table is already created try: msg = await channel.fetch_message(msgID) await msg.edit( content="%s\n%s" % (refreshed if (msgIDs.index(msgID) == 0) else "", tables[msgIDs.index(msgID)])) except discord.errors.NotFound: # if message doesn't exist, delete all existing messages msgIDs = await clearMessages() await sendNewMessages() break except IndexError: # less tables than messages for msgID in msgIDs[len(tables):]: try: msg = await channel.fetch_message(msgID) await msg.edit(content=spaceChar) except discord.errors.NotFound: pass if not msgIDs: # first time around, no messages exist for table in tables: msg = await channel.send( "%s\n%s" % (refreshed if (tables.index(table) == 0) else "", table)) msgIDs.append(msg.id) await sendBufferMessages() if (message is not None): embed = message.embeds[0] embed = updateDetailInURL( embed, "messageIDs", ",".join([str(msgID) for msgID in msgIDs])) await message.edit(embed=embed) tableDetails.messageIDs = [str(msgID) for msgID in msgIDs] saveTable(tableDetails)
async def editTable(message): await message.channel.trigger_typing() tableMessageID = message.content.split("edit")[-1].strip() moBotDB = connectDatabase() table = findTable(moBotDB, tableMessageID, message.author.id) if (table): # exists if (type(table) != int): # is creator moBotMember = message.guild.get_member(moBot) embed = defaultEmbed( moBotMember, await openUserSpreadsheet(table.workbookKey, message.guild.get_channel(table.channelID), message.author, moBotMember), table.workbookKey, table.guildID, table.channelID, message.author) tableDict = vars(table) for var in tableDict: # update the embed with table values if (var != "tableID"): tableDict[var] = ",".join(tableDict[var]) if type( tableDict[var]) == list else tableDict[ var] # converts arrays to csv strings embed = updateDetailInURL(embed, var, tableDict[var]) workbook = await openUserSpreadsheet(table.workbookKey, message.channel, message.author, moBotMember) for sheet in workbook.worksheets(): if (sheet.id == table.worksheetID): embed = updateFieldValue(embed, 'Sheet Name', spaceChar * 2 + " " + sheet.title) break embed = updateFieldValue( embed, 'Table Range', spaceChar * 2 + " " + RandomSupport.stripZero(table.tableRange)) embed = updateFieldValue( embed, 'Auto Updating', spaceChar * 2 + " " + ("Yes" if table.autoUpdating == 1 else "No")) embed = updateFieldValue( embed, 'Number of Buffer Messages', spaceChar * 2 + " " + str(table.bufferMessages)) embed = updateFieldValue( embed, 'Left Aligned Ranges', spaceChar * 2 + " " + RandomSupport.stripZero(table.leftAligned)) embed = updateFieldValue( embed, 'Right Aligned Ranges', spaceChar * 2 + " " + RandomSupport.stripZero(table.rightAligned)) embed = updateFieldValue( embed, 'Centered Ranges', spaceChar * 2 + " " + RandomSupport.stripZero(table.centered)) embed = updateFieldValue(embed, 'Number of Headers', spaceChar * 2 + " " + str(table.headers)) embed = updateFieldValue( embed, 'Smart Merging', spaceChar * 2 + " " + ("Yes" if table.smartMerging == 1 else "No")) msg = await message.channel.send(embed=embed) await addReactions(msg) else: # is not creator creatorID = table await message.channel.send( "**Not Authorizied**\n%s, currently only the creator of the table is authorized to edit a table. The creator for the given table is `%s`." % (message.author.mention, message.guild.get_member(creatorID).display_name)) else: # doesn't exist await message.channel.send( "**Table Not Found**\n%s, the `message_id` given was not found in any of MoBot's existing tables. Make sure the given `message_id` was actually an id of a table." % message.author.mention) moBotDB.connection.close()