2
2
import subprocess
3
3
import shutil
4
4
5
- from discord .ext import commands
6
5
from colorama import Fore , Style
6
+ from discord .ext import commands
7
7
8
8
from core .decorators import owner_only
9
+ from core .models import Bot
9
10
10
11
11
12
class DownloadError (Exception ):
@@ -15,15 +16,16 @@ class DownloadError(Exception):
15
16
class Plugins :
16
17
"""Plugins expand Mod Mail functionality by allowing third-party addons.
17
18
18
- These addons could have a range of features from moderation to simply making
19
- your life as a moderator easier!
19
+ These addons could have a range of features from moderation to simply
20
+ making your life as a moderator easier!
20
21
Learn how to create a plugin yourself here: https://link.com
21
22
"""
22
- def __init__ (self , bot ):
23
+ def __init__ (self , bot : Bot ):
23
24
self .bot = bot
24
- self .bot .loop .create_task (self .download_initital_plugins ())
25
+ self .bot .loop .create_task (self .download_initial_plugins ())
25
26
26
- def parse_plugin (self , name ):
27
+ @staticmethod
28
+ def parse_plugin (name ):
27
29
# returns: (username, repo, plugin_name)
28
30
try :
29
31
result = name .split ('/' )
@@ -32,35 +34,43 @@ def parse_plugin(self, name):
32
34
return None
33
35
return tuple (result )
34
36
35
- async def download_initital_plugins (self ):
37
+ async def download_initial_plugins (self ):
36
38
await self .bot ._connected .wait ()
37
39
for i in self .bot .config .plugins :
38
40
parsed_plugin = self .parse_plugin (i )
39
41
40
42
try :
41
43
await self .download_plugin_repo (* parsed_plugin [:- 1 ])
42
- except DownloadError as e :
43
- print (Fore .RED + f'Unable to download plugin ({ parsed_plugin [0 ]} /{ parsed_plugin [1 ]} - { e } ' + Style .RESET_ALL )
44
+ except DownloadError as exc :
45
+ msg = 'Unable to download plugin '
46
+ msg += f'({ parsed_plugin [0 ]} /{ parsed_plugin [1 ]} - { exc } '
47
+ print (Fore .RED + msg + Style .RESET_ALL )
44
48
45
49
await self .load_plugin (* parsed_plugin )
46
50
47
- async def download_plugin_repo (self , username , repo ):
51
+ @staticmethod
52
+ async def download_plugin_repo (username , repo ):
48
53
try :
49
- subprocess .run (f'git clone https://github.com/{ username } /{ repo } plugins/{ username } -{ repo } -q' , check = True , capture_output = True )
50
- # -q for quiet so there's no terminal output unless there's an error
51
- except subprocess .CalledProcessError as e :
52
- error = e .stderr .decode ('utf8' ).strip ()
53
- if not error .endswith ('already exists and is not an empty directory.' ):
54
+ cmd = f'git clone https://github.com/{ username } /{ repo } '
55
+ cmd += 'plugins/{username}-{repo} -q'
56
+ subprocess .run (cmd , check = True , capture_output = True )
57
+ # -q (quiet) so there's no terminal output unless there's an error
58
+ except subprocess .CalledProcessError as exc :
59
+ error = exc .stderr .decode ('utf-8' ).strip ()
60
+ if not error .endswith ('already exists and is '
61
+ 'not an empty directory.' ):
54
62
# don't raise error if the plugin folder exists
55
- raise DownloadError (error ) from e
63
+ raise DownloadError (error ) from exc
56
64
57
65
async def load_plugin (self , username , repo , plugin_name ):
58
66
try :
59
- self .bot .load_extension (f'plugins.{ username } -{ repo } .{ plugin_name } .{ plugin_name } ' )
60
- except ModuleNotFoundError as e :
61
- raise DownloadError ('Invalid plugin structure' ) from e
67
+ ext = f'plugins.{ username } -{ repo } .{ plugin_name } .{ plugin_name } '
68
+ self .bot .load_extension (ext )
69
+ except ModuleNotFoundError as exc :
70
+ raise DownloadError ('Invalid plugin structure' ) from exc
62
71
else :
63
- print (Fore .LIGHTCYAN_EX + f'Loading plugins.{ username } -{ repo } .{ plugin_name } ' + Style .RESET_ALL )
72
+ msg = f'Loading plugins.{ username } -{ repo } .{ plugin_name } '
73
+ print (Fore .LIGHTCYAN_EX + msg + Style .RESET_ALL )
64
74
65
75
@commands .group (aliases = ['plugins' ])
66
76
@owner_only ()
@@ -80,44 +90,54 @@ async def add(self, ctx, *, plugin_name):
80
90
81
91
try :
82
92
await self .download_plugin_repo (* parsed_plugin [:- 1 ])
83
- except DownloadError as e :
84
- return await ctx .send (f'Unable to fetch plugin from Github: { e } ' )
93
+ except DownloadError as exc :
94
+ return await ctx .send (
95
+ f'Unable to fetch plugin from Github: { exc } '
96
+ )
85
97
86
98
try :
87
99
await self .load_plugin (* parsed_plugin )
88
- except DownloadError as e :
89
- return await ctx .send (f'Unable to start plugin: { e } ' )
100
+ except DownloadError as exc :
101
+ return await ctx .send (f'Unable to start plugin: { exc } ' )
90
102
91
103
# if it makes it here, it has passed all checks and should
92
104
# be entered into the config
93
105
94
106
self .bot .config .plugins .append (plugin_name )
95
107
await self .bot .config .update ()
96
- await ctx .send ('Plugin installed. Any plugin that you install is of your OWN RISK.' )
108
+
109
+ await ctx .send ('Plugin installed. Any plugin that '
110
+ 'you install is of your OWN RISK.' )
97
111
else :
98
- await ctx .send ('Invalid plugin name format. Use username/repo/plugin.' )
112
+ await ctx .send ('Invalid plugin name format. '
113
+ 'Use username/repo/plugin.' )
99
114
100
115
@plugin .command ()
101
116
async def remove (self , ctx , * , plugin_name ):
102
117
"""Removes a certain plugin"""
103
118
if plugin_name in self .bot .config .plugins :
104
119
username , repo , name = self .parse_plugin (plugin_name )
105
- self .bot .unload_extension (f'plugins.{ username } -{ repo } .{ name } .{ name } ' )
120
+ self .bot .unload_extension (
121
+ f'plugins.{ username } -{ repo } .{ name } .{ name } '
122
+ )
106
123
107
124
self .bot .config .plugins .remove (plugin_name )
108
125
109
126
try :
110
- if not any (i .startswith (f'{ username } /{ repo } ' ) for i in self .bot .config .plugins ):
127
+ if not any (i .startswith (f'{ username } /{ repo } ' )
128
+ for i in self .bot .config .plugins ):
111
129
# if there are no more of such repos, delete the folder
112
- shutil .rmtree (f'plugins/{ username } -{ repo } ' , ignore_errors = True )
130
+ shutil .rmtree (f'plugins/{ username } -{ repo } ' ,
131
+ ignore_errors = True )
113
132
await ctx .send ('' )
114
- except Exception as e :
115
- print (e )
133
+ except Exception as exc :
134
+ print (exc )
116
135
self .bot .config .plugins .append (plugin_name )
117
- raise e
136
+ raise exc
118
137
119
138
await self .bot .config .update ()
120
- await ctx .send ('Plugin uninstalled and all related data is erased.' )
139
+ await ctx .send ('Plugin uninstalled and '
140
+ 'all related data is erased.' )
121
141
else :
122
142
await ctx .send ('Plugin not installed.' )
123
143
@@ -126,9 +146,11 @@ async def update(self, ctx, *, plugin_name):
126
146
async with ctx .typing ():
127
147
username , repo , name = self .parse_plugin (plugin_name )
128
148
try :
129
- cmd = subprocess .run (f'cd plugins/{ username } -{ repo } && git pull' , shell = True , check = True , capture_output = True )
130
- except subprocess .CalledProcessError as e :
131
- error = e .stdout .decode ('utf8' ).strip ()
149
+ cmd = f'cd plugins/{ username } -{ repo } && git pull'
150
+ cmd = subprocess .run (cmd , shell = True , check = True ,
151
+ capture_output = True )
152
+ except subprocess .CalledProcessError as exc :
153
+ error = exc .stdout .decode ('utf8' ).strip ()
132
154
await ctx .send (f'Error when updating: { error } ' )
133
155
else :
134
156
output = cmd .stdout .decode ('utf8' ).strip ()
0 commit comments