Skip to content

Commit 94c71e7

Browse files
committed
[leveler] allow overwriting existing backgrounds
fix some docstrings
1 parent a918087 commit 94c71e7

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

leveler/commands/lvladmin/backgrounds.py

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from asyncio import TimeoutError as AsyncTimeoutError
2+
13
from redbot.core import commands
4+
from redbot.core.utils.predicates import MessagePredicate
25

36
from leveler.abc import MixinMeta
47

@@ -14,53 +17,95 @@ class Backgrounds(MixinMeta):
1417
@commands.is_owner()
1518
@commands.guild_only()
1619
async def lvladminbg(self, ctx):
17-
"""Admin background configuration"""
20+
"""Backgrounds configuration."""
1821
pass
1922

2023
@lvladminbg.command()
2124
async def addprofilebg(self, ctx, name: str, url: str):
2225
"""Add a profile background.
2326
2427
The proportions must be 290px x 290px."""
25-
backgrounds = await self.config.backgrounds()
26-
if name in backgrounds["profile"].keys():
27-
await ctx.send("That profile background name already exists!")
28-
elif not await self._valid_image_url(url):
28+
if not await self._valid_image_url(url):
2929
await ctx.send("That is not a valid image URL!")
30-
else:
31-
async with self.config.backgrounds() as backgrounds:
32-
backgrounds["profile"][name] = url
33-
await ctx.send("New profile background (`{}`) added.".format(name))
30+
return
31+
async with self.config.backgrounds() as backgrounds:
32+
if name in backgrounds["profile"].keys():
33+
pred = MessagePredicate.yes_or_no(ctx)
34+
if not ctx.assume_yes:
35+
await ctx.send(
36+
(
37+
"This will replace already existing background `{name}` "
38+
"for future users of this background. Do you want to proceed?\n"
39+
'To agree, type "yes"'
40+
).format(name=name)
41+
)
42+
try:
43+
await self.bot.wait_for("message", check=pred, timeout=30)
44+
except AsyncTimeoutError:
45+
pass
46+
if not (ctx.assume_yes or pred.result):
47+
await ctx.send("Aborting.")
48+
return
49+
backgrounds["profile"][name] = url
50+
await ctx.send("New profile background (`{}`) added.".format(name))
3451

3552
@lvladminbg.command()
3653
async def addrankbg(self, ctx, name: str, url: str):
3754
"""Add a rank background.
3855
3956
The proportions must be 360px x 100px."""
40-
backgrounds = await self.config.backgrounds()
41-
if name in backgrounds["profile"].keys():
42-
await ctx.send("That rank background name already exists!")
43-
elif not await self._valid_image_url(url):
57+
if not await self._valid_image_url(url):
4458
await ctx.send("That is not a valid image URL!")
45-
else:
46-
async with self.config.backgrounds() as backgrounds:
47-
backgrounds["rank"][name] = url
48-
await ctx.send("New rank background (`{}`) added.".format(name))
59+
return
60+
async with self.config.backgrounds() as backgrounds:
61+
if name in backgrounds["rank"].keys():
62+
pred = MessagePredicate.yes_or_no(ctx)
63+
if not ctx.assume_yes:
64+
await ctx.send(
65+
(
66+
"This will replace already existing background `{name}` "
67+
"for future users of this background. Do you want to proceed?\n"
68+
'To agree, type "yes"'
69+
).format(name=name)
70+
)
71+
try:
72+
await self.bot.wait_for("message", check=pred, timeout=30)
73+
except AsyncTimeoutError:
74+
pass
75+
if not (ctx.assume_yes or pred.result):
76+
await ctx.send("Aborting.")
77+
return
78+
backgrounds["rank"][name] = url
79+
await ctx.send("New rank background (`{}`) added.".format(name))
4980

5081
@lvladminbg.command()
5182
async def addlevelbg(self, ctx, name: str, url: str):
5283
"""Add a level-up background.
5384
5485
The proportions must be 175px x 65px."""
55-
backgrounds = await self.config.backgrounds()
56-
if name in backgrounds["levelup"].keys():
57-
await ctx.send("That level-up background name already exists!")
58-
elif not await self._valid_image_url(url):
86+
if not await self._valid_image_url(url):
5987
await ctx.send("That is not a valid image URL!")
60-
else:
61-
async with self.config.backgrounds() as backgrounds:
62-
backgrounds["levelup"][name] = url
63-
await ctx.send("New level-up background (`{}`) added.".format(name))
88+
return
89+
async with self.config.backgrounds() as backgrounds:
90+
if name in backgrounds["levelup"].keys():
91+
pred = MessagePredicate.yes_or_no(ctx)
92+
if not ctx.assume_yes:
93+
await ctx.send(
94+
(
95+
"This will replace already existing background `{name}` "
96+
"for future users of this background. Do you want to proceed?\n"
97+
'To agree, type "yes"'
98+
).format(name=name)
99+
)
100+
try:
101+
await self.bot.wait_for("message", check=pred, timeout=30)
102+
except AsyncTimeoutError:
103+
pass
104+
if not (ctx.assume_yes or pred.result):
105+
await ctx.send("Aborting.")
106+
return
107+
backgrounds["levelup"][name] = url
108+
await ctx.send("New level-up background (`{}`) added.".format(name))
64109

65110
@lvladminbg.command()
66111
async def setcustombg(self, ctx, bg_type: str, user_id: str, img_url: str):

leveler/commands/lvladmin/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def overview(self, ctx):
6363
@commands.is_owner()
6464
@lvladmin.command()
6565
async def resetrep(self, ctx):
66-
"""Resets all reputation points from MonogoDB"""
66+
"""Resets all reputation points from MongoDB."""
6767
async with ctx.typing():
6868
await self.db.users.update_many({}, {"$set": {"rep": 0}})
6969
await ctx.send("All reputation points have been removed.")
@@ -194,7 +194,7 @@ async def length(self, ctx, message_length: int = 10):
194194
@lvladmin.command(name="globaltop")
195195
@commands.is_owner()
196196
async def allow_global_top(self, ctx):
197-
"""Allow usage of `--global` argument in `[p]top` command"""
197+
"""Allow usage of `--global` argument in `[p]top` command."""
198198
# Reason: https://support-dev.discord.com/hc/en-us/articles/360043053492
199199
server = ctx.guild
200200
if await self.config.allow_global_top():

leveler/leveler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Leveler(
2929
):
3030
"""A level up thing with image generation!"""
3131

32-
__version__ = "3.0.8"
32+
__version__ = "3.0.9"
3333

3434
# noinspection PyMissingConstructor
3535
def __init__(self, bot: Red):

0 commit comments

Comments
 (0)