Skip to content

Commit

Permalink
[ConversationGames] simplify commands
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Oct 12, 2023
1 parent a73e0d9 commit b9e5e42
Showing 1 changed file with 35 additions and 91 deletions.
126 changes: 35 additions & 91 deletions conversationgames/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
# The idea of this cog is taken from https://github.com/Jintaku/Jintaku-Cogs-V3/tree/master/conversationgames

import logging
from typing import Dict, Final, List, Optional
from typing import Dict, Final, List, Optional, Union

import discord
from discord.ext.commands._types import Check
from redbot.core import Config, app_commands, commands
from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import humanize_list

from .constants import Ratings
from .constants import Ratings, Endpoints
from .http import TruthOrDareAPIClient
from .views import CGView

Expand Down Expand Up @@ -82,150 +82,94 @@ async def _get_rating(self, guild: discord.Guild) -> Ratings:
rating = await self.config.guild(guild).rating()
return rating

async def _api(
self, ctx: commands.Context, request: Endpoints, member: Optional[discord.Member] = None
) -> None:
title = f"{ctx.author.name} asked {member.name}" if member is not None else None
async with TruthOrDareAPIClient() as client, ctx.typing():
rating: Ratings = await self._get_rating(ctx.guild) # type: ignore
result: Dict[str, Union[str, Dict[str, str]]] = await client._request(request, rating)
embed: discord.Embed = discord.Embed(
title=title,
description=result["question"],
color=await ctx.embed_color(),
)
embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}")
_view: CGView = CGView(ctx, result)
_out: discord.Message = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out

@is_restricted()
@commands.guild_only()
@commands.cooldown(1, 3, commands.BucketType.guild)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 3, commands.BucketType.guild)
@app_commands.default_permissions(use_application_commands=True)
@commands.hybrid_command(name="wouldyourather", aliases=["wyr"])
async def _wyr(self, ctx: commands.Context):
"""
Would you rather?
"""
async with TruthOrDareAPIClient() as client, ctx.typing():
rating = await self._get_rating(ctx.guild) # type: ignore
result = await client._request("wyr", rating)
embed: discord.Embed = discord.Embed(
description=result["question"], color=await ctx.embed_color()
)
embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}")
_view = CGView(ctx, result)
_out = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out
await self._api(ctx, "wyr")

@is_restricted()
@commands.guild_only()
@commands.cooldown(1, 3, commands.BucketType.guild)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 3, commands.BucketType.guild)
@app_commands.default_permissions(use_application_commands=True)
@commands.hybrid_command(name="neverhaveiever", aliases=["nhie"])
async def _nhie(self, ctx: commands.Context):
"""
Never have I ever.
"""
async with TruthOrDareAPIClient() as client, ctx.typing():
rating = await self._get_rating(ctx.guild) # type: ignore
result = await client._request("nhie", rating)
embed: discord.Embed = discord.Embed(
description=result["question"], color=await ctx.embed_color()
)
embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}")
_view = CGView(ctx, result)
_out = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out
await self._api(ctx, "nhie")

@is_restricted()
@commands.guild_only()
@commands.cooldown(1, 3, commands.BucketType.guild)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 3, commands.BucketType.guild)
@app_commands.default_permissions(use_application_commands=True)
@commands.hybrid_command(name="paranoia")
async def _paranoia(self, ctx: commands.Context):
"""
Paranoia questions.
"""
async with TruthOrDareAPIClient() as client, ctx.typing():
rating = await self._get_rating(ctx.guild) # type: ignore
result = await client._request("paranoia", rating)
embed: discord.Embed = discord.Embed(
description=result["question"], color=await ctx.embed_color()
)
embed.set_footer(text=f"Rating: {result['rating']} | ID: {result['id']}")
_view = CGView(ctx, result)
_out = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out
await self._api(ctx, "paranoia")

@is_restricted()
@commands.guild_only()
@commands.cooldown(1, 3, commands.BucketType.guild)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 3, commands.BucketType.guild)
@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!
"""
if member is None:
title = None
else:
title = f"{ctx.author.name} asked {member.name}"
async with TruthOrDareAPIClient() as client, ctx.typing():
rating = await self._get_rating(ctx.guild) # type: ignore
result = await client._request("truth", rating)
embed: discord.Embed = discord.Embed(
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, member)
_out = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out
await self._api(ctx, "truth", member)

@is_restricted()
@commands.guild_only()
@commands.cooldown(1, 3, commands.BucketType.guild)
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 3, commands.BucketType.guild)
@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):
"""
Dare questions, optionally ask dare questions to members!
"""
if member is None:
title = None
else:
title = f"{ctx.author.name} asked {member.name}"
async with TruthOrDareAPIClient() as client, ctx.typing():
rating = await self._get_rating(ctx.guild) # type: ignore
result = await client._request("dare", rating)
embed: discord.Embed = discord.Embed(
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, member)
_out = await ctx.send(
embed=embed,
view=_view,
allowed_mentions=discord.AllowedMentions(replied_user=False),
reference=ctx.message.to_reference(fail_if_not_exists=False),
)
_view._message = _out
await self._api(ctx, "dare", member)

@commands.guild_only()
@commands.group(name="cgset") # type: ignore
@commands.admin_or_permissions(manage_guild=True)
async def _cgset(self, ctx: commands.Context):
async def _cgset(self, _: commands.Context):
"""
Configurating options for Conversation Games.
"""
Expand Down

0 comments on commit b9e5e42

Please sign in to comment.