Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of IDs #6

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.5
0.0.6
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
136 changes: 104 additions & 32 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand Down Expand Up @@ -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`"
Expand Down Expand Up @@ -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 <https://github.com/eibex/reaction-light/blob/master/README.md#example> 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 <https://github.com/eibex/reaction-light/blob/master/README.md#example> "
"for a setup walkthrough.".format(prefix)
)
else:
await ctx.send("You do not have an admin role.")

Expand Down
32 changes: 23 additions & 9 deletions rlightfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ### ###")
Expand Down