Skip to content

Commit f8513d0

Browse files
authored
Merge pull request #249 from practical-python-org/fix-run-command-edits
fix: run command edits
2 parents 437f238 + cdee8d4 commit f8513d0

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

src/zorak/cogs/logging/logging_message_edit.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ def __init__(self, bot):
1818

1919
@commands.Cog.listener()
2020
async def on_message_edit(self, message_before, message_after):
21-
"""
22-
Checking if there is a change to /run command to summon a new embed
23-
Or if the content before is != to the content after then log the change.
24-
"""
25-
if message_after.content.startswith('/'):
26-
edited_command = message_after.content.split()[0]
27-
if edited_command == '/run':
28-
ctx = await self.bot.get_context(message_after)
29-
await self.bot.invoke(ctx)
21+
22+
# IGNORE /run, since we will set up an on_message_edit handler there with opposite logic
23+
if message_before.content.startswith('/run') or message_after.content.startswith('/run'):
24+
return
25+
3026
elif message_before.content != message_after.content:
3127
# This guy here makes sure we use the displayed name inside the guild.
3228
if message_before.author.nick is None:

src/zorak/cogs/utility/utility_run_code.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class UtilityRunCode(commands.Cog):
1616

1717
def __init__(self, bot):
1818
self.bot = bot
19-
self.previous_message_id = None
20-
2119

2220
def get_embed(self, name, value, is_error=False):
2321
if is_error:
@@ -31,7 +29,6 @@ def get_embed(self, name, value, is_error=False):
3129
)
3230
return embed
3331

34-
3532
@commands.command()
3633
async def run(self, ctx, *, codeblock):
3734
"""
@@ -41,20 +38,12 @@ async def run(self, ctx, *, codeblock):
4138
, ctx.author.name
4239
, ctx.command)
4340

44-
if self.previous_message_id:
45-
try:
46-
previous_message = await ctx.channel.fetch_message(self.previous_message_id)
47-
if previous_message:
48-
await previous_message.delete()
49-
except discord.NotFound:
50-
pass
51-
52-
# Adjust iOS quotation marks “”‘’ to avoid SyntaxError: invalid character
41+
# Adjust iOS quotation marks “”‘’ to avoid SyntaxError: invalid character
5342
for i, c in enumerate("“‘”’"):
54-
codeblock = codeblock.replace(c, "\"'"[i%2])
43+
codeblock = codeblock.replace(c, "\"'"[i % 2])
5544

5645
if codeblock.startswith("```py") and "```" in codeblock[3:]:
57-
46+
5847
# Split input args from codeblock
5948
_, codeblock, args = codeblock.split("```")
6049
args = [arg for arg in args.split("\n") if arg]
@@ -67,14 +56,14 @@ async def run(self, ctx, *, codeblock):
6756
if args or input_count:
6857
# Check if number of input() functions matches args from user.
6958
if len(args) != input_count:
70-
value = 'I am happy to run your script but I do not want to interact with you. You can '\
71-
'remove your input() functions, however if you insist on keeping them, please '\
72-
'put your input values in order on separate lines after the codeblock:'\
73-
'\n\n\`\`\`py \nx = input("What is your first input: ")\ny = input("What is '\
59+
value = 'I am happy to run your script but I do not want to interact with you. You can ' \
60+
'remove your input() functions, however if you insist on keeping them, please ' \
61+
'put your input values in order on separate lines after the codeblock:' \
62+
'\n\n\`\`\`py \nx = input("What is your first input: ")\ny = input("What is ' \
7463
'your second input: ")\nprint(x)\nprint(y)\n\`\`\`\nmy_first_input\nmy_second_input'
7564
embed = self.get_embed("Formatting error", value, True)
7665
message = await ctx.channel.send(embed=embed)
77-
self.previous_message_id = message.id
66+
# self.previous_message_id = message.id
7867
return
7968
# Else, replace all input()s with values
8069
else:
@@ -84,21 +73,39 @@ async def run(self, ctx, *, codeblock):
8473
piston = PistonAPI()
8574
runcode = piston.execute(language="py", version="3.10.0", code=codeblock)
8675
embed = self.get_embed("Output:", runcode)
87-
message = await ctx.channel.send(embed=embed)
88-
self.previous_message_id = message.id
76+
message = await ctx.reply(embed=embed)
8977

9078
elif codeblock.startswith("'''") and codeblock.endswith("'''"):
9179
value = "Did you mean to use a \` instead of a ' ?\n\`\`\`py Your code here \`\`\`"
9280
embed = self.get_embed("Formatting error", value, True)
9381
message = await ctx.channel.send(embed=embed)
94-
self.previous_message_id = message.id
9582

9683
else:
97-
value = 'Please place your code inside a code block. (between \`\`\`py '\
84+
print(codeblock)
85+
value = 'Please place your code inside a code block. (between \`\`\`py ' \
9886
'and \`\`\`)\n\n\`\`\`py \nx = "like this"\nprint(x) \n\`\`\`'
9987
embed = self.get_embed("Formatting error", value, True)
10088
message = await ctx.channel.send(embed=embed)
101-
self.previous_message_id = message.id
89+
90+
@commands.Cog.listener()
91+
async def on_message_edit(self, before, after):
92+
if before.author.bot:
93+
# Ignore messages sent by bots
94+
return
95+
if before.content.startswith('/run') or after.content.startswith('/run'):
96+
# Make sure we ONLY re-run messages that are /run commands
97+
channel = after.channel # The channel that the edited message is in
98+
# Grab the last 10 (should be enough...) bot messages
99+
all_replies = [message async for message in channel.history(limit=20) if message.author == self.bot.user]
100+
for bot_reply in all_replies:
101+
if bot_reply.reference.message_id == after.id:
102+
await bot_reply.delete()
103+
# If the reference message of the bot message is the edited message...
104+
# Re-run the /run command with the new content
105+
# however, we dont call the command with /run, so we need to remove the '/run' from the message
106+
code = after.content.replace("/run", "").strip()
107+
new_ctx = await self.bot.get_context(after) # get a new message ctx
108+
return await new_ctx.command.callback(self, new_ctx, codeblock=code)
102109

103110

104111
def setup(bot):

0 commit comments

Comments
 (0)