Skip to content

Commit

Permalink
Abstract guild permissions value within discord.py Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
weibeu committed Jul 18, 2020
1 parent 66b5ccc commit 5e99ed9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions flask_discord/models/guild.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .base import DiscordModelsBase
from flask import current_app

import discord
from .. import configs


Expand All @@ -25,8 +27,8 @@ class Guild(DiscordModelsBase):
Hash of guild's icon.
is_owner : bool
Boolean determining if current user is owner of the guild or not.
permissions_value : int
An integer representing permissions of current user in the guild.
permissions : discord.Permissions
An instance of discord.Permissions representing permissions of current user in the guild.
"""

Expand All @@ -39,7 +41,13 @@ def __init__(self, payload):
self.name = self._payload["name"]
self.icon_hash = self._payload.get("icon")
self.is_owner = self._payload.get("owner")
self.permissions_value = self._payload.get("permissions")
self.permissions = self.__get_permissions(self._payload.get("permissions"))

@staticmethod
def __get_permissions(permissions_value):
if permissions_value is None:
return
return discord.Permissions(int(permissions_value))

def __str__(self):
return self.name
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def me():
@app.route("/me/guilds/")
def user_guilds():
guilds = discord.fetch_guilds()
return "<br />".join([g.name for g in guilds])
return "<br />".join([f"[ADMIN] {g.name}" if g.permissions.administrator else g.name for g in guilds])


@app.route("/add_to/<int:guild_id>/")
Expand Down

0 comments on commit 5e99ed9

Please sign in to comment.