Skip to content

Commit

Permalink
[privacy] fix if interacting player doesnt exist (#429)
Browse files Browse the repository at this point in the history
* .

* Fix an issue where interacting player doesnt exist

---------

Co-authored-by: MAX <[email protected]>
  • Loading branch information
flaree and ltzmax authored Oct 1, 2024
1 parent f51403e commit db03d90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
49 changes: 49 additions & 0 deletions ballsdex/core/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
from typing import TYPE_CHECKING, Union

import discord

from ballsdex.core.models import Player, PrivacyPolicy
from ballsdex.settings import settings

if TYPE_CHECKING:
from ballsdex.core.bot import BallsDexBot


def is_staff(interaction: discord.Interaction) -> bool:
if interaction.guild and interaction.guild.id in settings.admin_guild_ids:
roles = settings.admin_role_ids + settings.root_role_ids
if any(role.id in roles for role in interaction.user.roles): # type: ignore
return True
return False


async def inventory_privacy(
bot: "BallsDexBot",
interaction: discord.Interaction,
player: Player,
user_obj: Union[discord.User, discord.Member],
):
privacy_policy = player.privacy_policy
interacting_player, _ = await Player.get_or_create(discord_id=interaction.user.id)
if interaction.user.id == player.discord_id:
return True
if is_staff(interaction):
return True
if privacy_policy == PrivacyPolicy.DENY:
await interaction.followup.send(
"This user has set their inventory to private.", ephemeral=True
)
return False
elif privacy_policy == PrivacyPolicy.FRIENDS:
if not await interacting_player.is_friend(player):
await interaction.followup.send(
"This users inventory can only be viewed from users they have added as friends.",
ephemeral=True,
)
return False
elif privacy_policy == PrivacyPolicy.SAME_SERVER:
if not bot.intents.members:
await interaction.followup.send(
"This user has their policy set to `Same Server`, "
"however I do not have the `members` intent to check this.",
ephemeral=True,
)
return False
if interaction.guild is None:
await interaction.followup.send(
"This user has set their inventory to private.", ephemeral=True
)
return False
elif interaction.guild.get_member(user_obj.id) is None:
await interaction.followup.send("This user is not in the server.", ephemeral=True)
return False
return True
57 changes: 3 additions & 54 deletions ballsdex/packages/balls/cog.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import enum
import logging
from collections import defaultdict
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING

import discord
from discord import app_commands
from discord.ext import commands
from discord.ui import Button, View, button
from tortoise.exceptions import DoesNotExist

from ballsdex.core.models import (
BallInstance,
DonationPolicy,
Player,
PrivacyPolicy,
Trade,
TradeObject,
balls,
)
from ballsdex.core.models import BallInstance, DonationPolicy, Player, Trade, TradeObject, balls
from ballsdex.core.utils.buttons import ConfirmChoiceView
from ballsdex.core.utils.paginator import FieldPageSource, Pages
from ballsdex.core.utils.transformers import (
Expand All @@ -26,7 +18,7 @@
SpecialEnabledTransform,
TradeCommandType,
)
from ballsdex.core.utils.utils import is_staff
from ballsdex.core.utils.utils import inventory_privacy, is_staff
from ballsdex.packages.balls.countryballs_paginator import CountryballsViewer
from ballsdex.settings import settings

Expand Down Expand Up @@ -700,46 +692,3 @@ async def count(
f"You have {balls} {special_str}{shiny_str}"
f"{country}{settings.collectible_name}{plural}{guild}."
)


async def inventory_privacy(
bot: "BallsDexBot",
interaction: discord.Interaction,
player: Player,
user_obj: Union[discord.User, discord.Member],
):
privacy_policy = player.privacy_policy
interacting_player = await Player.get(discord_id=interaction.user.id)
if interaction.user.id == player.discord_id:
return True
if is_staff(interaction):
return True
if privacy_policy == PrivacyPolicy.DENY:
await interaction.followup.send(
"This user has set their inventory to private.", ephemeral=True
)
return False
elif privacy_policy == PrivacyPolicy.FRIENDS:
if not await interacting_player.is_friend(player):
await interaction.followup.send(
"This users inventory can only be viewed from users they have added as friends.",
ephemeral=True,
)
return False
elif privacy_policy == PrivacyPolicy.SAME_SERVER:
if not bot.intents.members:
await interaction.followup.send(
"This user has their policy set to `Same Server`, "
"however I do not have the `members` intent to check this.",
ephemeral=True,
)
return False
if interaction.guild is None:
await interaction.followup.send(
"This user has set their inventory to private.", ephemeral=True
)
return False
elif interaction.guild.get_member(user_obj.id) is None:
await interaction.followup.send("This user is not in the server.", ephemeral=True)
return False
return True

0 comments on commit db03d90

Please sign in to comment.