@@ -16,8 +16,6 @@ class UtilityRunCode(commands.Cog):
16
16
17
17
def __init__ (self , bot ):
18
18
self .bot = bot
19
- self .previous_message_id = None
20
-
21
19
22
20
def get_embed (self , name , value , is_error = False ):
23
21
if is_error :
@@ -31,7 +29,6 @@ def get_embed(self, name, value, is_error=False):
31
29
)
32
30
return embed
33
31
34
-
35
32
@commands .command ()
36
33
async def run (self , ctx , * , codeblock ):
37
34
"""
@@ -41,20 +38,12 @@ async def run(self, ctx, *, codeblock):
41
38
, ctx .author .name
42
39
, ctx .command )
43
40
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
53
42
for i , c in enumerate ("“‘”’" ):
54
- codeblock = codeblock .replace (c , "\" '" [i % 2 ])
43
+ codeblock = codeblock .replace (c , "\" '" [i % 2 ])
55
44
56
45
if codeblock .startswith ("```py" ) and "```" in codeblock [3 :]:
57
-
46
+
58
47
# Split input args from codeblock
59
48
_ , codeblock , args = codeblock .split ("```" )
60
49
args = [arg for arg in args .split ("\n " ) if arg ]
@@ -67,14 +56,14 @@ async def run(self, ctx, *, codeblock):
67
56
if args or input_count :
68
57
# Check if number of input() functions matches args from user.
69
58
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 \n x = input("What is your first input: ")\n y = 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 \n x = input("What is your first input: ")\n y = input("What is ' \
74
63
'your second input: ")\n print(x)\n print(y)\n \`\`\`\n my_first_input\n my_second_input'
75
64
embed = self .get_embed ("Formatting error" , value , True )
76
65
message = await ctx .channel .send (embed = embed )
77
- self .previous_message_id = message .id
66
+ # self.previous_message_id = message.id
78
67
return
79
68
# Else, replace all input()s with values
80
69
else :
@@ -84,21 +73,39 @@ async def run(self, ctx, *, codeblock):
84
73
piston = PistonAPI ()
85
74
runcode = piston .execute (language = "py" , version = "3.10.0" , code = codeblock )
86
75
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 )
89
77
90
78
elif codeblock .startswith ("'''" ) and codeblock .endswith ("'''" ):
91
79
value = "Did you mean to use a \` instead of a ' ?\n \`\`\`py Your code here \`\`\`"
92
80
embed = self .get_embed ("Formatting error" , value , True )
93
81
message = await ctx .channel .send (embed = embed )
94
- self .previous_message_id = message .id
95
82
96
83
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 ' \
98
86
'and \`\`\`)\n \n \`\`\`py \n x = "like this"\n print(x) \n \`\`\`'
99
87
embed = self .get_embed ("Formatting error" , value , True )
100
88
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 )
102
109
103
110
104
111
def setup (bot ):
0 commit comments