Skip to content

Commit 58eb6c4

Browse files
committed
Allow users to turn off their own level up notifications. Also allow guild owners to set channels to ignore level up notifications.
1 parent 6df9a66 commit 58eb6c4

File tree

4 files changed

+95
-20
lines changed

4 files changed

+95
-20
lines changed

levelup/commands/admin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ async def view_settings(self, ctx: commands.Context):
354354
[f"<#{channel_id}>" for channel_id in conf.ignoredchannels if ctx.guild.get_channel(channel_id)]
355355
)
356356
embed.add_field(name=_("Ignored Channels"), value=joined, inline=False)
357+
if conf.ignore_notification_channels:
358+
joined = ", ".join(
359+
[
360+
f"<#{channel_id}>"
361+
for channel_id in conf.ignore_notification_channels
362+
if ctx.guild.get_channel(channel_id)
363+
]
364+
)
365+
embed.add_field(name=_("Notification Ignored Channels"), value=joined, inline=False)
357366
if conf.ignoredusers:
358367
joined = ", ".join([f"<@{user_id}>" for user_id in conf.ignoredusers if ctx.guild.get_member(user_id)])
359368
embed.add_field(name=_("Ignored Users"), value=joined, inline=False)
@@ -855,6 +864,29 @@ async def ignore(self, ctx: commands.Context):
855864
"""Base command for all ignore lists"""
856865
pass
857866

867+
@ignore.command(name="notify")
868+
async def ignore_notify(
869+
self,
870+
ctx: commands.Context,
871+
*,
872+
channel: t.Union[discord.TextChannel, discord.VoiceChannel, discord.CategoryChannel, discord.ForumChannel],
873+
):
874+
"""
875+
Add/Remove a channel in the notify ignore list
876+
Channels in the notify ignore list won't have level up notifications sent there
877+
878+
Use the command with a channel already in the notify ignore list to remove it
879+
"""
880+
conf = self.db.get_conf(ctx.guild)
881+
if channel.id in conf.ignore_notification_channels:
882+
conf.ignore_notification_channels.remove(channel.id)
883+
txt = _("Channel {} has been removed from the notify ignore list").format(channel.mention)
884+
else:
885+
conf.ignore_notification_channels.append(channel.id)
886+
txt = _("Channel {} has been added to the notify ignore list").format(channel.mention)
887+
self.save()
888+
await ctx.send(txt)
889+
858890
@ignore.command(name="channel")
859891
async def ignore_channel(
860892
self,

levelup/commands/user.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ async def profile_data(self, ctx: commands.Context, user_id: int):
136136
)
137137
await ctx.send(txt)
138138

139+
@commands.hybrid_command(name="lvlupnotify", aliases=["notify"])
140+
@commands.guild_only()
141+
@commands.cooldown(3, 10, commands.BucketType.user)
142+
async def level_up_notify(self, ctx: commands.Context):
143+
"""
144+
If enabled, level up notifications won't be sent for you in the server
145+
"""
146+
conf = self.db.get_conf(ctx.guild)
147+
profile = conf.get_profile(ctx.author.id)
148+
149+
profile.ignore_level_up_notification = not profile.ignore_level_up_notification
150+
151+
if profile.ignore_level_up_notification:
152+
await ctx.reply(
153+
"Level up notifications are now **disabled** for you.",
154+
mention_author=False,
155+
delete_after=30,
156+
)
157+
else:
158+
await ctx.reply(
159+
"Level up notifications are now **enabled** for you.",
160+
mention_author=False,
161+
delete_after=30,
162+
)
163+
164+
self.save()
165+
139166
@commands.hybrid_command(name="profile", aliases=["pf"])
140167
@commands.guild_only()
141168
@commands.cooldown(3, 10, commands.BucketType.user)
@@ -226,13 +253,17 @@ async def prestige(self, ctx: commands.Context):
226253
if not role:
227254
return await ctx.send(_("The prestige role for this level no longer exists, please contact an admin!"))
228255

229-
current_xp = int(profile.xp)
230-
xp_at_prestige = conf.algorithm.get_xp(conf.prestigelevel)
231-
leftover_xp = current_xp - xp_at_prestige if current_xp > xp_at_prestige else 0
232-
newlevel = conf.algorithm.get_level(leftover_xp)
233-
234-
profile.level = newlevel
235-
profile.xp = leftover_xp
256+
# current_xp = int(profile.xp)
257+
# xp_at_prestige = conf.algorithm.get_xp(conf.prestigelevel)
258+
# leftover_xp = current_xp - xp_at_prestige if current_xp > xp_at_prestige else 0
259+
# newlevel = conf.algorithm.get_level(leftover_xp)
260+
# change prestige to work by levels instead of exp
261+
current_level = profile.level
262+
new_level = max(1, current_level - conf.prestigelevel)
263+
new_xp = conf.algorithm.get_xp(new_level)
264+
265+
profile.level = new_level
266+
profile.xp = new_xp
236267
profile.prestige = next_prestige
237268
self.save()
238269

levelup/common/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Profile(Base):
124124
description="Last time the user was active in the guild (message sent or voice activity)",
125125
)
126126
show_tutorial: bool = True # Init with True, show tutorial on first command usage
127+
ignore_level_up_notification: bool = False # if true level up notifications will not be announced for this user
127128

128129
# Profile customization
129130
style: str = "default" # Can be default, runescape... (WIP)
@@ -308,6 +309,7 @@ class GuildSettings(Base):
308309
ignoredchannels: t.List[int] = [] # Channels that dont gain XP
309310
ignoredroles: t.List[int] = [] # Roles that dont gain XP
310311
ignoredusers: t.List[int] = [] # Ignored users won't gain XP
312+
ignore_notification_channels: t.List[int] = [] # channels where level up announcements won't be sent if configured
311313

312314
# Prestige
313315
prestigelevel: int = 0 # Level required to prestige, 0 is disabled

levelup/shared/levelups.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,23 @@ async def check_levelups(
115115
)
116116
if current_channel and conf.notify:
117117
with suppress(discord.HTTPException):
118-
if conf.notifymention:
119-
await current_channel.send(member.mention, embed=embed)
120-
else:
121-
await current_channel.send(embed=embed)
118+
if (
119+
not profile.ignore_level_up_notification
120+
and current_channel.id not in conf.ignore_notification_channels
121+
):
122+
if conf.notifymention:
123+
await current_channel.send(member.mention, embed=embed)
124+
else:
125+
await current_channel.send(embed=embed)
122126

123127
current_channel_id = current_channel.id if current_channel else 0
124128
if log_channel and log_channel.id != current_channel_id:
125129
with suppress(discord.HTTPException):
126-
if conf.notifymention:
127-
await log_channel.send(member.mention, embed=embed)
128-
else:
129-
await log_channel.send(embed=embed)
130+
if not profile.ignore_level_up_notification:
131+
if conf.notifymention:
132+
await log_channel.send(member.mention, embed=embed)
133+
else:
134+
await log_channel.send(embed=embed)
130135

131136
else:
132137
fonts = list(self.fonts.glob("*.ttf")) + list(self.custom_fonts.iterdir())
@@ -205,16 +210,21 @@ def _run() -> t.Tuple[bytes, bool]:
205210
if current_channel and conf.notify:
206211
file = discord.File(BytesIO(img_bytes), filename=f"levelup.{ext}")
207212
with suppress(discord.HTTPException):
208-
if conf.notifymention and message is not None:
209-
await message.reply(msg_txt, file=file, mention_author=True)
210-
else:
211-
await current_channel.send(msg_txt, file=file)
213+
if (
214+
not profile.ignore_level_up_notification
215+
and current_channel.id not in conf.ignore_notification_channels
216+
):
217+
if conf.notifymention and message is not None:
218+
await message.reply(msg_txt, file=file, mention_author=True)
219+
else:
220+
await current_channel.send(msg_txt, file=file)
212221

213222
current_channel_id = current_channel.id if current_channel else 0
214223
if log_channel and log_channel.id != current_channel_id:
215224
file = discord.File(BytesIO(img_bytes), filename=f"levelup.{ext}")
216225
with suppress(discord.HTTPException):
217-
await log_channel.send(msg_txt, file=file)
226+
if not profile.ignore_level_up_notification:
227+
await log_channel.send(msg_txt, file=file)
218228

219229
payload = {
220230
"guild": guild, # discord.Guild

0 commit comments

Comments
 (0)