Skip to content

Commit efae942

Browse files
committed
Remove dir when plugin remove
1 parent cf08042 commit efae942

File tree

6 files changed

+75
-52
lines changed

6 files changed

+75
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ however, insignificant breaking changes does not guarantee a major version bump,
3131

3232
- Reworked `config.get` and `config.set`, it feeds through the converters before setting/getting.
3333
- To get/set the raw value, access through `config[]`.
34-
- Prerelease naming scheme is now `x.x.x-dev`.
34+
- Prerelease naming scheme is now `x.x.x-devN`.
35+
- `trigger_typing` has been moved to `core.utils.trigger_typing`, original location is deprecated.
3536

3637
# v3.2.2
3738

cogs/modmail.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
from natural.date import duration
1414

1515
from core import checks
16-
from core.decorators import trigger_typing
1716
from core.models import PermissionLevel
1817
from core.paginator import EmbedPaginatorSession
1918
from core.time import UserFriendlyTime, human_timedelta
20-
from core.utils import format_preview, User, create_not_found_embed, format_description
19+
from core.utils import format_preview, User, create_not_found_embed, format_description, trigger_typing
2120

2221
logger = logging.getLogger("Modmail")
2322

cogs/plugins.py

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from core import checks
2323
from core.models import PermissionLevel
2424
from core.paginator import EmbedPaginatorSession
25-
from core.utils import truncate
25+
from core.utils import truncate, trigger_typing
2626

2727
logger = logging.getLogger("Modmail")
2828

@@ -235,6 +235,14 @@ async def load_plugin(self, plugin):
235235

236236
async def parse_user_input(self, ctx, plugin_name, check_version=False):
237237

238+
if not self._ready_event.is_set():
239+
embed = discord.Embed(
240+
description="Plugins are still loading, please try again later.",
241+
color=self.bot.main_color,
242+
)
243+
await ctx.send(embed=embed)
244+
return
245+
238246
if plugin_name in self.registry:
239247
details = self.registry[plugin_name]
240248
user, repo = details["repository"].split("/", maxsplit=1)
@@ -281,6 +289,7 @@ async def plugins(self, ctx):
281289

282290
@plugins.command(name="add", aliases=["install", "load"])
283291
@checks.has_permissions(PermissionLevel.OWNER)
292+
@trigger_typing
284293
async def plugins_add(self, ctx, *, plugin_name: str):
285294
"""
286295
Install a new plugin for the bot.
@@ -313,53 +322,51 @@ async def plugins_add(self, ctx, *, plugin_name: str):
313322
)
314323
msg = await ctx.send(embed=embed)
315324

316-
async with ctx.typing():
317-
try:
318-
await self.download_plugin(plugin, force=True)
319-
except Exception:
320-
logger.warning(f"Unable to download plugin %s.", plugin, exc_info=True)
321-
322-
embed = discord.Embed(
323-
description=f"Failed to download plugin, check logs for error.",
324-
color=self.bot.error_color,
325-
)
326-
327-
return await msg.edit(embed=embed)
325+
try:
326+
await self.download_plugin(plugin, force=True)
327+
except Exception:
328+
logger.warning(f"Unable to download plugin %s.", plugin, exc_info=True)
328329

329-
self.bot.config["plugins"].append(str(plugin))
330-
await self.bot.config.update()
330+
embed = discord.Embed(
331+
description=f"Failed to download plugin, check logs for error.",
332+
color=self.bot.error_color,
333+
)
331334

332-
if self.bot.config.get("enable_plugins"):
335+
return await msg.edit(embed=embed)
333336

334-
invalidate_caches()
337+
self.bot.config["plugins"].append(str(plugin))
338+
await self.bot.config.update()
335339

336-
try:
337-
await self.load_plugin(plugin)
338-
except Exception:
339-
logger.warning(f"Unable to load plugin %s.", plugin, exc_info=True)
340+
if self.bot.config.get("enable_plugins"):
340341

341-
embed = discord.Embed(
342-
description=f"Failed to download plugin, check logs for error.",
343-
color=self.bot.error_color,
344-
)
342+
invalidate_caches()
345343

346-
return await msg.edit(embed=embed)
344+
try:
345+
await self.load_plugin(plugin)
346+
except Exception:
347+
logger.warning(f"Unable to load plugin %s.", plugin, exc_info=True)
347348

348349
embed = discord.Embed(
349-
description="Successfully installed plugin.\n"
350-
"*Friendly reminder, plugins have absolute control over your bot. "
351-
"Please only install plugins from developers you trust.*",
352-
color=self.bot.main_color,
350+
description=f"Failed to download plugin, check logs for error.",
351+
color=self.bot.error_color,
353352
)
353+
354354
else:
355355
embed = discord.Embed(
356356
description="Successfully installed plugin.\n"
357357
"*Friendly reminder, plugins have absolute control over your bot. "
358-
"Please only install plugins from developers you trust.*\n\n"
359-
"This plugin is currently not enabled due to `ENABLE_PLUGINS=false`, "
360-
"to re-enable plugins, remove or change `ENABLE_PLUGINS=true` and restart your bot.",
358+
"Please only install plugins from developers you trust.*",
361359
color=self.bot.main_color,
362360
)
361+
else:
362+
embed = discord.Embed(
363+
description="Successfully installed plugin.\n"
364+
"*Friendly reminder, plugins have absolute control over your bot. "
365+
"Please only install plugins from developers you trust.*\n\n"
366+
"This plugin is currently not enabled due to `ENABLE_PLUGINS=false`, "
367+
"to re-enable plugins, remove or change `ENABLE_PLUGINS=true` and restart your bot.",
368+
color=self.bot.main_color,
369+
)
363370
return await msg.edit(embed=embed)
364371

365372
@plugins.command(name="remove", aliases=["del", "delete"])
@@ -390,6 +397,15 @@ async def plugins_remove(self, ctx, *, plugin_name: str):
390397

391398
self.bot.config["plugins"].remove(str(plugin))
392399
await self.bot.config.update()
400+
shutil.rmtree(
401+
plugin.abs_path,
402+
onerror=lambda *args: logger.warning('Failed to remove plugin files %s: %s', plugin, str(args[2]))
403+
)
404+
try:
405+
plugin.abs_path.parent.rmdir()
406+
plugin.abs_path.parent.parent.rmdir()
407+
except OSError:
408+
pass # dir not empty
393409

394410
embed = discord.Embed(
395411
description="The plugin is successfully uninstalled.",

cogs/utility.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from core import checks
2626
from core.changelog import Changelog
27-
from core.decorators import trigger_typing
2827
from core.models import InvalidConfigError, PermissionLevel
2928
from core.paginator import EmbedPaginatorSession, MessagePaginatorSession
3029
from core import utils
@@ -248,7 +247,7 @@ def cog_unload(self):
248247

249248
@commands.command()
250249
@checks.has_permissions(PermissionLevel.REGULAR)
251-
@trigger_typing
250+
@utils.trigger_typing
252251
async def changelog(self, ctx, version: str.lower = ""):
253252
"""Shows the changelog of the Modmail."""
254253
changelog = await Changelog.from_url(self.bot)
@@ -282,7 +281,7 @@ async def changelog(self, ctx, version: str.lower = ""):
282281

283282
@commands.command(aliases=["bot", "info"])
284283
@checks.has_permissions(PermissionLevel.REGULAR)
285-
@trigger_typing
284+
@utils.trigger_typing
286285
async def about(self, ctx):
287286
"""Shows information about this bot."""
288287
embed = discord.Embed(color=self.bot.main_color, timestamp=datetime.utcnow())
@@ -331,7 +330,7 @@ async def about(self, ctx):
331330

332331
@commands.command()
333332
@checks.has_permissions(PermissionLevel.REGULAR)
334-
@trigger_typing
333+
@utils.trigger_typing
335334
async def sponsors(self, ctx):
336335
"""Shows a list of sponsors."""
337336
resp = await self.bot.session.get(
@@ -352,7 +351,7 @@ async def sponsors(self, ctx):
352351

353352
@commands.group(invoke_without_command=True)
354353
@checks.has_permissions(PermissionLevel.OWNER)
355-
@trigger_typing
354+
@utils.trigger_typing
356355
async def debug(self, ctx):
357356
"""Shows the recent application-logs of the bot."""
358357

@@ -407,7 +406,7 @@ async def debug(self, ctx):
407406

408407
@debug.command(name="hastebin", aliases=["haste"])
409408
@checks.has_permissions(PermissionLevel.OWNER)
410-
@trigger_typing
409+
@utils.trigger_typing
411410
async def debug_hastebin(self, ctx):
412411
"""Posts application-logs to Hastebin."""
413412

@@ -450,7 +449,7 @@ async def debug_hastebin(self, ctx):
450449

451450
@debug.command(name="clear", aliases=["wipe"])
452451
@checks.has_permissions(PermissionLevel.OWNER)
453-
@trigger_typing
452+
@utils.trigger_typing
454453
async def debug_clear(self, ctx):
455454
"""Clears the locally cached logs."""
456455

@@ -658,7 +657,7 @@ async def before_loop_presence(self):
658657

659658
@commands.command()
660659
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
661-
@trigger_typing
660+
@utils.trigger_typing
662661
async def ping(self, ctx):
663662
"""Pong! Returns your websocket latency."""
664663
embed = discord.Embed(

core/decorators.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import functools
1+
import warnings
22

3-
from discord.ext import commands
3+
from core.utils import trigger_typing as _trigger_typing
44

55

66
def trigger_typing(func):
7-
@functools.wraps(func)
8-
async def wrapper(self, ctx: commands.Context, *args, **kwargs):
9-
await ctx.trigger_typing()
10-
return await func(self, ctx, *args, **kwargs)
11-
12-
return wrapper
7+
warnings.warn("trigger_typing has been moved to core.utils.trigger_typing, this will be removed.",
8+
DeprecationWarning,
9+
stacklevel=2)
10+
return _trigger_typing(func)

core/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import re
23
import shlex
34
import typing
@@ -250,3 +251,12 @@ def format_description(i, names):
250251
": ".join((str(a + i * 15), b))
251252
for a, b in enumerate(takewhile(lambda x: x is not None, names), start=1)
252253
)
254+
255+
256+
def trigger_typing(func):
257+
@functools.wraps(func)
258+
async def wrapper(self, ctx: commands.Context, *args, **kwargs):
259+
await ctx.trigger_typing()
260+
return await func(self, ctx, *args, **kwargs)
261+
262+
return wrapper

0 commit comments

Comments
 (0)