From 587a57ef5cdbb64db1c09a22ef3fce3ab7a760a6 Mon Sep 17 00:00:00 2001 From: Lemon Rose Date: Sun, 24 Sep 2023 05:32:21 +0530 Subject: [PATCH] [ConversationGames] enhance cog --- conversationgames/core.py | 22 +++++++++++----------- conversationgames/http.py | 7 ++++++- conversationgames/views.py | 10 ++++++++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/conversationgames/core.py b/conversationgames/core.py index 4a315165..3df25c20 100644 --- a/conversationgames/core.py +++ b/conversationgames/core.py @@ -32,7 +32,7 @@ from redbot.core.bot import Red from redbot.core.utils.chat_formatting import humanize_list -from .constants import Ratings, RequestType +from .constants import Ratings from .http import TruthOrDareAPIClient from .views import CGView @@ -71,7 +71,7 @@ async def _get_rating(self, guild: discord.Guild) -> Ratings: @commands.guild_only() @commands.cooldown(1, 3, commands.BucketType.guild) - @commands.bot_has_guild_permissions(embed_links=True) + @commands.bot_has_permissions(embed_links=True) @app_commands.default_permissions(use_application_commands=True) @commands.hybrid_command(name="wouldyourather", aliases=["wyr"]) async def _wyr(self, ctx: commands.Context): @@ -96,7 +96,7 @@ async def _wyr(self, ctx: commands.Context): @commands.guild_only() @commands.cooldown(1, 3, commands.BucketType.guild) - @commands.bot_has_guild_permissions(embed_links=True) + @commands.bot_has_permissions(embed_links=True) @app_commands.default_permissions(use_application_commands=True) @commands.hybrid_command(name="neverhaveiever", aliases=["nhie"]) async def _nhie(self, ctx: commands.Context): @@ -120,10 +120,10 @@ async def _nhie(self, ctx: commands.Context): _view._message = _out @commands.guild_only() - @commands.hybrid_command(name="paranoia") @commands.cooldown(1, 3, commands.BucketType.guild) - @commands.bot_has_guild_permissions(embed_links=True) + @commands.bot_has_permissions(embed_links=True) @app_commands.default_permissions(use_application_commands=True) + @commands.hybrid_command(name="paranoia") async def _paranoia(self, ctx: commands.Context): """ Paranoia questions. @@ -145,11 +145,11 @@ async def _paranoia(self, ctx: commands.Context): _view._message = _out @commands.guild_only() - @commands.hybrid_command(name="truth") @commands.cooldown(1, 3, commands.BucketType.guild) - @commands.bot_has_guild_permissions(embed_links=True) + @commands.bot_has_permissions(embed_links=True) @app_commands.default_permissions(use_application_commands=True) @app_commands.describe(member="The member you want to ask question.") + @commands.hybrid_command(name="truth") async def _truth(self, ctx: commands.Context, *, member: Optional[discord.Member] = None): """ Truth questions, optionally ask truth questions to members! @@ -165,7 +165,7 @@ async def _truth(self, ctx: commands.Context, *, member: Optional[discord.Member title=title, description=result["question"], color=await ctx.embed_color() ) embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}") - _view = CGView(ctx, result) + _view = CGView(ctx, result, member) _out = await ctx.send( embed=embed, view=_view, @@ -175,10 +175,10 @@ async def _truth(self, ctx: commands.Context, *, member: Optional[discord.Member _view._message = _out @commands.guild_only() - @commands.hybrid_command(name="dare") @commands.cooldown(1, 3, commands.BucketType.guild) - @commands.bot_has_guild_permissions(embed_links=True) + @commands.bot_has_permissions(embed_links=True) @app_commands.default_permissions(use_application_commands=True) + @commands.hybrid_command(name="dare") @app_commands.describe(member="The member you want to ask question.") async def _dare(self, ctx: commands.Context, *, member: Optional[discord.Member] = None): """ @@ -195,7 +195,7 @@ async def _dare(self, ctx: commands.Context, *, member: Optional[discord.Member] title=title, description=result["question"], color=await ctx.embed_color() ) embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}") - _view = CGView(ctx, result) + _view = CGView(ctx, result, member) _out = await ctx.send( embed=embed, view=_view, diff --git a/conversationgames/http.py b/conversationgames/http.py index 82ba7da1..096f88a0 100644 --- a/conversationgames/http.py +++ b/conversationgames/http.py @@ -1,3 +1,4 @@ +import pathlib import logging from datetime import timedelta from types import TracebackType @@ -7,6 +8,7 @@ from aiohttp_client_cache import SQLiteBackend from aiohttp_client_cache.session import CachedSession from redbot.core import commands +from redbot.core.data_manager import cog_data_path from typing_extensions import Self from .constants import ( @@ -21,6 +23,9 @@ log: logging.Logger = logging.getLogger("red.seina.conversationgames.http") +DATA_PATH: pathlib.Path = cog_data_path(raw_name="ConversationGames") +CACHE_DIRECTORY: str = str(DATA_PATH / "cache/conversationgames.db") + class HTTPClient: __slots__: Tuple = ("_base_url", "_session") @@ -39,7 +44,7 @@ async def __request(self, method: Methods, route: str, **kwargs: Any) -> Dict[st if not self._session: timeout = aiohttp.ClientTimeout(total=SESSION_TIMEOUT) cache = SQLiteBackend( - cache_name="~/.cache/conversationgames.db", + cache_name=CACHE_DIRECTORY, expire_after=timedelta(seconds=3), allowed_methods=["GET", "HEAD"], urls_expire_after=URL_EXPIRE_AFTER, diff --git a/conversationgames/views.py b/conversationgames/views.py index a29104e4..0a6d5fe9 100644 --- a/conversationgames/views.py +++ b/conversationgames/views.py @@ -61,11 +61,13 @@ def __init__( self, ctx: commands.Context, result: Dict[str, Union[str, Dict[str, str]]], - timeout: float = 120.0, + member: Optional[discord.Member] = None, + timeout: Optional[float] = 120.0, ) -> None: super().__init__(timeout=timeout) self._ctx: commands.Context = ctx self._result: Dict[str, Union[str, Dict[str, str]]] = result + self._member: Optional[discord.Member] = member self._message: Optional[discord.Message] = None self.add_item(Select(self._callback)) # type: ignore @@ -80,7 +82,11 @@ async def on_timeout(self) -> None: pass async def interaction_check(self, interaction: discord.Interaction) -> bool: - if self._ctx.author.id != interaction.user.id: + if ( + self._member.id + if self._member is not None + else self._ctx.author.id != interaction.user.id + ): await interaction.response.send_message( "You are not allowed to use this interaction", ephemeral=True )