Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: context menu for !raw #2773

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions bot/exts/info/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from textwrap import shorten
from typing import Any, TYPE_CHECKING

import discord
import rapidfuzz
from discord import AllowedMentions, Colour, Embed, Guild, Message, Role
from discord import AllowedMentions, Colour, Embed, Guild, Interaction, Message, Role
from discord.ext.commands import BucketType, Cog, Context, Paginator, command, group, has_any_role
from discord.utils import escape_markdown
from pydis_core.site_api import ResponseCodeError
Expand Down Expand Up @@ -43,6 +44,11 @@ class Information(Cog):

def __init__(self, bot: Bot):
self.bot = bot
self.raw_context_menu = discord.app_commands.ContextMenu(
name="Raw Text",
callback=self._raw_context_menu_callback,
)
self.bot.tree.add_command(self.raw_context_menu, guild=discord.Object(bot.guild_id))

@staticmethod
def get_channel_type_counts(guild: Guild) -> defaultdict[str, int]:
Expand Down Expand Up @@ -486,19 +492,15 @@ def format_fields(self, mapping: Mapping[str, Any], field_width: int | None = No
# remove trailing whitespace
return out.rstrip()

async def send_raw_content(self, ctx: Context, message: Message, json: bool = False) -> None:
async def get_raw_content(self, message: Message, json: bool = False) -> Paginator:
"""
Send information about the raw API response for a `discord.Message`.
Gets information about the raw API response for a `discord.Message`.

If `json` is True, send the information in a copy-pasteable Python format.
"""
if not message.channel.permissions_for(ctx.author).read_messages:
await ctx.send(":x: You do not have permissions to see the channel this message is in.")
return

# I *guess* it could be deleted right as the command is invoked but I felt like it wasn't worth handling
# doing this extra request is also much easier than trying to convert everything back into a dictionary again
raw_data = await ctx.bot.http.get_message(message.channel.id, message.id)
raw_data = await self.bot.http.get_message(message.channel.id, message.id)

paginator = Paginator()

Expand All @@ -524,15 +526,26 @@ def add_content(title: str, content: str) -> None:
title = f"Raw {field_name} ({current}/{total})"
add_content(title, transformer(item))

for page in paginator.pages:
await ctx.send(page, allowed_mentions=AllowedMentions.none())
return paginator

@cooldown_with_role_bypass(2, 60 * 3, BucketType.member, bypass_roles=constants.STAFF_PARTNERS_COMMUNITY_ROLES)
@group(invoke_without_command=True)
@in_whitelist(channels=(constants.Channels.bot_commands,), roles=constants.STAFF_PARTNERS_COMMUNITY_ROLES)
async def raw(self, ctx: Context, message: Message) -> None:
"""Shows information about the raw API response."""
await self.send_raw_content(ctx, message)
if not message.channel.permissions_for(ctx.author).read_messages:
await ctx.send(":x: You do not have permissions to see the channel this message is in.")
return

paginator = await self.get_raw_content(message)

for page in paginator.pages:
await ctx.send(page, allowed_mentions=AllowedMentions.none())

async def _raw_context_menu_callback(self, interaction: Interaction, message: Message) -> None:
"""The callback to be ran when the Raw Text context menu button is used."""
paginator = await self.get_raw_content(message)
await interaction.response.send_message(paginator.pages[1], ephemeral=True)
Copy link
Contributor

@Robin5605 Robin5605 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could trial out some button-based pagination here? It's a shame that users can only see one page with the context menu version. I think one of the largest (pun intended) issues people had with buttons for pagination is their large vertical size, though it may be less of an issue in ephemeral messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, okay. I'll try adding a view with some buttons to see if we can scroll through.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to do what was suggested in #1037 and just send a pastebin link if the message is too long, maybe in a similar way to the eval command where it displays as much as it can first.


@raw.command()
async def json(self, ctx: Context, message: Message) -> None:
Expand Down
Loading