Skip to content

Commit

Permalink
Add [p]levelerset defaultbackground command (issue #23)
Browse files Browse the repository at this point in the history
New command missing translation for now
  • Loading branch information
Malarne authored Feb 9, 2019
1 parent 0d63d2a commit 68c1bbb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Leveler/leveler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

import re
from redbot.core import checks, Config
import discord
from redbot.core import commands
Expand Down Expand Up @@ -566,3 +566,14 @@ async def setxp(self, ctx, xp:int, member:discord.Member=None):
await self.profiles._set_exp(member, xp)
await ctx.send(member.name +_("'s XP set to ") + str(xp))

@levelerset.command()
@checks.mod_or_permissions(manage_messages=True)
async def defaultbackground(self, ctx, url):
"""Allow you to set a default background for your server members."""
bg = re.findall(r"(?:http\:|https\:)?\/\/.*\.(?:png|jpg|gif)", url)
if not bg:
await ctx.send(_("Please give a direct link to an image on format png, jpg or gif !"))
else:
background = bg[0]
await self.profiles._set_guild_background(ctx.guild, background)
await ctx.send(f"Default background set to {background}.")
18 changes: 18 additions & 0 deletions Leveler/userprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ def __init__(self):
self.data.register_member(**default_member)
self.data.register_guild(**default_guild)

async def update_all_members(
self,
config: Config,
guild: discord.Guild,
entries: dict,
):
base_group = config._get_base_group(config.MEMBER, str(guild.id))
async with base_group() as all_members:
to_update = {k: v for k, v in all_members.items()}
for m in guild.members:
mid = str(m.id)
to_update[mid] = to_update.get(mid, {})
to_update[mid].update(entries)
all_members.update(to_update)

async def _set_guild_background(self, guild, bg):
await self.update_all_members(self.data, guild, {"background": bg})

async def _give_exp(self, member, exp):
current = await self.data.member(member).exp()
await self.data.member(member).exp.set(current + exp)
Expand Down

0 comments on commit 68c1bbb

Please sign in to comment.