diff --git a/.version b/.version index fa3de586..99d85ecd 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.0.5 \ No newline at end of file +0.0.6 \ No newline at end of file diff --git a/README.md b/README.md index 2f19520f..54831915 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Reaction Light - Discord Role Bot -![Reaction Light 0.0.5](https://img.shields.io/badge/Reaction%20Light-0.0.5-yellow.svg) +![Reaction Light 0.0.6](https://img.shields.io/badge/Reaction%20Light-0.0.6-yellow.svg) ![Python 3.5.3+](https://img.shields.io/badge/python-3.5.3+-blue.svg) ![discord.py rewrite](https://img.shields.io/badge/discord.py-1.2.5+-blue.svg) diff --git a/bot.py b/bot.py index 92a48e6b..cd7eeff7 100644 --- a/bot.py +++ b/bot.py @@ -11,7 +11,7 @@ # Original Repository: https://github.com/eibex/reaction-light __author__ = "eibex" -__version__ = "0.0.5" +__version__ = "0.0.6" __license__ = "MIT" directory = path.dirname(path.realpath(__file__)) @@ -115,7 +115,20 @@ async def on_message(message): if step == 1: # If it was not, it ignores the message. # The channel the message needs to be sent to is stored # Advances to step two - rlightfm.step1(r_id, message.channel_mentions[0].id) + try: + server = bot.get_guild(message.guild.id) + bot_user = server.get_member(bot.user.id) + ch_id = message.channel_mentions[0].id + bot_permissions = bot.get_channel(ch_id).permissions_for(bot_user) + writable = bot_permissions.read_messages + readable = bot_permissions.view_channel + if not writable or not readable: + await message.channel.send("I cannot read or send messages to that channel.") + return + except IndexError: + await message.channel.send("The channel you mentioned is invalid.") + return + rlightfm.step1(r_id, ch_id) await message.channel.send( "Attach roles and emojis separated by a space (one combination per message). " "When you are done type `done`. Example:\n:smile: `@Role`" @@ -240,44 +253,103 @@ async def new(ctx): await ctx.send("You do not have an admin role.") -@bot.command(name="help") -async def hlp(ctx): - if isadmin(ctx): - await ctx.send( - "Use `rl!new` to start creating a reaction message.\n" - "Visit for a setup walkthrough." - ) - else: - await ctx.send("You do not have an admin role.") - - @bot.command(name="edit") async def edit_embed(ctx): if isadmin(ctx): # Reminds user of formatting if it is wrong - msg = ctx.message.content.split(" // ") - if len(msg) < 4: + msg = ctx.message.content.split() + if len(msg) < 2: await ctx.send( - "Formatting is: `#channelname // Message ID // New Title // New Content`" + "Type `{}edit #channelname` to get started. Replace `#channelname` " + "with the channel where the reaction-role message " + "you wish to edit is located.".format(prefix) ) return - ch_id = ctx.message.channel_mentions[0].id - old_id = int(msg[1]) - try: - # Tries to edit the embed - # Raises errors if the channel sent was invalid or if the bot cannot edit the message + elif len(msg) == 2: + try: + ch_id = ctx.message.channel_mentions[0].id + except IndexError: + await ctx.send("The channel you mentioned is invalid.") + return ch = bot.get_channel(ch_id) - old_msg = await ch.fetch_message(old_id) - title = msg[2] - content = msg[3] - em = discord.Embed(title=title, description=content, colour=botcolor) - em.set_footer(text="Reaction Light", icon_url=logo) - await old_msg.edit(embed=em) - await ctx.send("Message edited.") - except IndexError: - await ctx.send("The channel you linked is invalid.") - except discord.Forbidden: - await ctx.send("I do not have permissions to edit the message.") + r_ids = rlightfm.edit(ch_id) + if len(r_ids) == 1: + await ctx.send( + "There is only one embed in this channel. Type " + "`{}edit #channelname // 1 // New Title // New Description` " + "to edit the reaction-role message.".format(prefix) + ) + elif len(r_ids) > 1: + embeds = [] + counter = 1 + for msg_id in r_ids: + try: + old_msg = await ch.fetch_message(int(msg_id)) + except discord.NotFound: + # Skipping embeds that might have been deleted without updating CSVs + continue + except discord.Forbidden: + ctx.send("I do not have permissions to edit a reaction-role message that I previously created.") + continue + entry = "{}. {}".format(counter, old_msg.embeds[0].title) + embeds.append(entry) + counter += 1 + await ctx.send( + "There are {} embeds in this channel. Type " + "`{}edit #channelname // EMBED_NUMBER // New Title // New Description` " + "to edit the desired reaction-role message. The list of embeds is:\n".format(len(r_ids), prefix) + + "\n".join(embeds) + ) + else: + await ctx.send("There are no reaction-role messages in that channel.") + elif len(msg) > 2: + try: + # Tries to edit the embed + # Raises errors if the channel sent was invalid or if the bot cannot edit the message + ch_id = ctx.message.channel_mentions[0].id + ch = bot.get_channel(ch_id) + msg = ctx.message.content.split(" // ") + embed_number = msg[1] + r_ids = rlightfm.edit(ch_id) + counter = 1 + # Loop through all msg_ids and stops when the counter matches the user input + if r_ids: + to_edit_id = None + for msg_id in r_ids: + if str(counter) == embed_number: + to_edit_id = msg_id + break + counter += 1 + else: + await ctx.send("You selected an embed that does not exist.") + return + if to_edit_id: + old_msg = await ch.fetch_message(int(to_edit_id)) + else: + await ctx.send("Select a valid embed number (i.e. the number to the left of the embed title in the list above).") + return + title = msg[2] + content = msg[3] + em = discord.Embed(title=title, description=content, colour=botcolor) + em.set_footer(text="Reaction Light", icon_url=logo) + await old_msg.edit(embed=em) + await ctx.send("Message edited.") + except IndexError: + await ctx.send("The channel you mentioned is invalid.") + except discord.Forbidden: + await ctx.send("I do not have permissions to edit the message.") + else: + await ctx.send("You do not have an admin role.") + + +@bot.command(name="help") +async def hlp(ctx): + if isadmin(ctx): + await ctx.send( + "Use `{}new` to start creating a reaction message.\n" + "Visit " + "for a setup walkthrough.".format(prefix) + ) else: await ctx.send("You do not have an admin role.") diff --git a/rlightfm.py b/rlightfm.py index b3125633..d872a6f0 100644 --- a/rlightfm.py +++ b/rlightfm.py @@ -14,8 +14,8 @@ def readcache(): # Run at the start of the program and updated when changes are made try: with open("{}/cache.csv".format(folder), "r") as f: - r = csv.reader(f, delimiter=",") - for row in r: + read = csv.reader(f, delimiter=",") + for row in read: cache[row[0]] = row[1] except FileNotFoundError: print("No cache.csv file found. It will be created on the next wizard run.") @@ -42,9 +42,9 @@ def getch(r): def getcombo(r): # Returns the reaction-role combinations - with open("{}/".format(folder) + str(r) + ".csv", "r") as f: - r = csv.reader(f, delimiter=",") - return [i for i in r] + with open("{}/{}.csv".format(folder, str(r)), "r") as f: + read = csv.reader(f, delimiter=",") + return [i for i in read] def addids(message_id, r): @@ -63,10 +63,10 @@ def getids(message_id): def getreactions(r): # Returns a list of the reactions used by a certain embed - with open("{}/".format(folder) + r + ".csv", "r") as f: - r = csv.reader(f, delimiter=",") + with open("{}/{}.csv".format(folder, str(r)), "r") as f: + read = csv.reader(f, delimiter=",") reactions = {} - for row in r: + for row in read: try: reactions[row[0]] = int(row[1]) except IndexError: @@ -119,7 +119,7 @@ def step2(r, role, emoji, done=False): global wizardcache if done: wizard[r][3] += 1 # Set step3 (was 2) - with open("{}/".format(folder) + str(r) + ".csv", "a") as f: + with open("{}/{}.csv".format(folder, str(r)), "a") as f: w = csv.writer(f, delimiter=",") for i in wizardcache[r]: w.writerow(i) @@ -130,6 +130,20 @@ def step2(r, role, emoji, done=False): wizardcache[r].append(combo) +def edit(role_channel): + # Loops through all CSVs to check for embeds that are present in role_channel + r_ids = {} + for msg_id in cache: + r = cache[msg_id] + with open("{}/{}.csv".format(folder, str(r)), "r") as f: + read = csv.reader(f, delimiter=",") + for row in read: + channel = int(row[0]) + break # The channel is stored only at the first row + if role_channel == channel: + r_ids[str(msg_id)] = str(r) + return r_ids + def end(r): # Deletes the setup process and updates the cache del wizard[r] diff --git a/setup.py b/setup.py index f13e5fcf..01facdc2 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ # Original Repository: https://github.com/eibex/reaction-light print("Author: eibex") -print("Version: 0.0.5") +print("Version: 0.0.6") print("License: MIT\n") print("### ### Reaction Light Setup ### ###")