Skip to content

Commit

Permalink
Merge branch 'master' into forum-channels
Browse files Browse the repository at this point in the history
# Conflicts:
#	discord/channel.py
  • Loading branch information
krittick committed May 7, 2022
2 parents 116e00c + 7ad078f commit 90bef7f
Show file tree
Hide file tree
Showing 27 changed files with 341 additions and 528 deletions.
1 change: 1 addition & 0 deletions discord/audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class AuditLogChanges:
"location_type",
_enum_transformer(enums.ScheduledEventLocationType),
),
"command_id": ("command_id", _transform_snowflake),
}

def __init__(
Expand Down
116 changes: 3 additions & 113 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
command,
)
from .enums import InteractionType
from .errors import CheckFailure, DiscordException, Forbidden, HTTPException
from .errors import CheckFailure, DiscordException
from .interactions import Interaction
from .shard import AutoShardedClient
from .types import interactions
Expand Down Expand Up @@ -256,7 +256,8 @@ def _check_command(cmd: ApplicationCommand, match: Dict) -> bool:
else:
as_dict = cmd.to_dict()
to_check = {
"default_permission": None,
"dm_permission": None,
"default_member_permissions": None,
"name": None,
"description": None,
"name_localizations": None,
Expand Down Expand Up @@ -572,7 +573,6 @@ async def sync_commands(
register_guild_commands: bool = True,
check_guilds: Optional[List[int]] = [],
delete_exiting: bool = True,
_register_permissions: bool = True, # TODO: Remove for perms v2
) -> None:
"""|coro|
Expand Down Expand Up @@ -645,12 +645,6 @@ async def sync_commands(
guild_commands, guild_id=guild_id, method=method, force=force, delete_existing=delete_exiting
)

# TODO: 2.1: Remove this and favor permissions v2
# Global Command Permissions

if not _register_permissions:
return

global_permissions: List = []

for i in registered_commands:
Expand All @@ -664,12 +658,7 @@ async def sync_commands(
cmd.id = i["id"]
self._application_commands[cmd.id] = cmd

# Permissions (Roles will be converted to IDs just before Upsert for Global Commands)
global_permissions.append({"id": i["id"], "permissions": cmd.permissions})

for guild_id, commands in registered_guild_commands.items():
guild_permissions: List = []

for i in commands:
cmd = find(
lambda cmd: cmd.name == i["name"]
Expand All @@ -684,105 +673,6 @@ async def sync_commands(
cmd.id = i["id"]
self._application_commands[cmd.id] = cmd

# Permissions
permissions = [
perm.to_dict()
for perm in cmd.permissions
if perm.guild_id is None
or (perm.guild_id == guild_id and cmd.guild_ids is not None and perm.guild_id in cmd.guild_ids)
]
guild_permissions.append({"id": i["id"], "permissions": permissions})

for global_command in global_permissions:
permissions = [
perm.to_dict()
for perm in global_command["permissions"]
if perm.guild_id is None
or (perm.guild_id == guild_id and cmd.guild_ids is not None and perm.guild_id in cmd.guild_ids)
]
guild_permissions.append({"id": global_command["id"], "permissions": permissions})

# Collect & Upsert Permissions for Each Guild
# Command Permissions for this Guild
guild_cmd_perms: List = []

# Loop through Commands Permissions available for this Guild
for item in guild_permissions:
new_cmd_perm = {"id": item["id"], "permissions": []}

# Replace Role / Owner Names with IDs
for permission in item["permissions"]:
if isinstance(permission["id"], str):
# Replace Role Names
if permission["type"] == 1:
role = get(
self._bot.get_guild(guild_id).roles,
name=permission["id"],
)

# If not missing
if role is not None:
new_cmd_perm["permissions"].append(
{
"id": role.id,
"type": 1,
"permission": permission["permission"],
}
)
else:
raise RuntimeError(
"No Role ID found in Guild ({guild_id}) for Role ({role})".format(
guild_id=guild_id, role=permission["id"]
)
)
# Add owner IDs
elif permission["type"] == 2 and permission["id"] == "owner":
app = await self.application_info() # type: ignore
if app.team:
for m in app.team.members:
new_cmd_perm["permissions"].append(
{
"id": m.id,
"type": 2,
"permission": permission["permission"],
}
)
else:
new_cmd_perm["permissions"].append(
{
"id": app.owner.id,
"type": 2,
"permission": permission["permission"],
}
)
# Add the rest
else:
new_cmd_perm["permissions"].append(permission)

# Make sure we don't have over 10 overwrites
if len(new_cmd_perm["permissions"]) > 10:
raise RuntimeError(
"Command '{name}' has more than 10 permission overrides in guild ({guild_id}).".format(
name=self._application_commands[new_cmd_perm["id"]].name,
guild_id=guild_id,
)
)
# Append to guild_cmd_perms
guild_cmd_perms.append(new_cmd_perm)

# Upsert
try:
await self._bot.http.bulk_upsert_command_permissions(self._bot.user.id, guild_id, guild_cmd_perms)
except Forbidden:
raise RuntimeError(
f"Failed to add command permissions to guild {guild_id}",
file=sys.stderr,
)
except HTTPException:
_log.warning(
"Command Permissions V2 not yet implemented, permissions will not be set for your commands."
)

async def process_application_commands(self, interaction: Interaction, auto_sync: bool = None) -> None:
"""|coro|
Expand Down
13 changes: 9 additions & 4 deletions discord/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@

from __future__ import annotations

import asyncio
import datetime
import time
from abc import abstractmethod
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -1140,7 +1137,11 @@ class VoiceChannel(discord.abc.Messageable, VocalGuildChannel):
.. versionadded:: 2.0
"""

__slots__ = ()
__slots__ = "nsfw"

def _update(self, guild: Guild, data: VoiceChannelPayload):
super()._update(guild, data)
self.nsfw: bool = data.get("nsfw", False)

def __repr__(self) -> str:
attrs = [
Expand All @@ -1159,6 +1160,10 @@ def __repr__(self) -> str:
async def _get_channel(self):
return self

def is_nsfw(self) -> bool:
""":class:`bool`: Checks if the channel is NSFW."""
return self.nsfw

@property
def last_message(self) -> Optional[Message]:
"""Fetches the last message from this channel in cache.
Expand Down
Loading

0 comments on commit 90bef7f

Please sign in to comment.