Skip to content

Commit

Permalink
Merge pull request #15 from probablyjassin:enh/use-mogicontext-everyw…
Browse files Browse the repository at this point in the history
…here

use mogi context everywhere
  • Loading branch information
probablyjassin authored Nov 2, 2024
2 parents ff8eeb7 + 3e4e42d commit 8d1ae3a
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 49 deletions.
6 changes: 3 additions & 3 deletions cogs/debugging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

from discord import SlashCommandGroup, ApplicationContext
from discord import SlashCommandGroup
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
Expand All @@ -19,11 +19,11 @@ async def current_mogi(self, ctx: MogiApplicationContext):
await ctx.respond(f"Current Mogi: \n{ctx.mogi}")

@debug.command(name="all_mogis", description="print the mogi registry")
async def all_mogis(self, ctx: ApplicationContext):
async def all_mogis(self, ctx: MogiApplicationContext):
await ctx.respond(f"Mogi Registry: \n{mogi_manager.read_registry()}")

@debug.command(name="throw_error", description="throw an error")
async def throw_error(self, ctx: ApplicationContext):
async def throw_error(self, ctx: MogiApplicationContext):
raise Exception("This is a test command error")

@debug.command(name="test_player", description="add a dummy player to the mogi")
Expand Down
5 changes: 3 additions & 2 deletions cogs/error_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from discord import Interaction, ApplicationContext, DiscordException, errors, Color
from discord import Interaction, DiscordException, errors, Color
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.command_helpers.info_embed_factory import create_embed
from logger import setup_logger

Expand All @@ -18,7 +19,7 @@ def __init__(self, bot):

@commands.Cog.listener()
async def on_application_command_error(
self, ctx: ApplicationContext, error: DiscordException
self, ctx: MogiApplicationContext, error: DiscordException
):
# predicate check failures get handled in the command, ignore them here
if isinstance(error, errors.CheckFailure):
Expand Down
6 changes: 4 additions & 2 deletions cogs/help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from discord import slash_command, ApplicationContext, Color
from discord import slash_command, Color
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.command_helpers.info_embed_factory import create_embed

help_fields = {
Expand All @@ -21,7 +23,7 @@ def __init__(self, bot):
self.bot: commands.Bot = bot

@slash_command(name="help", description="A summary of most of the commands to help")
async def help(self, ctx: ApplicationContext):
async def help(self, ctx: MogiApplicationContext):
embed = create_embed(
"Help",
"Here you can find a brief summary of common commands.",
Expand Down
5 changes: 3 additions & 2 deletions cogs/mogi/close_mogi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from discord import slash_command, ApplicationContext
from discord import slash_command
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.data.mogi_manager import mogi_manager
from utils.command_helpers.confirm import confirmation
from utils.command_helpers.checks import is_mogi_not_in_progress
Expand All @@ -12,7 +13,7 @@ def __init__(self, bot):

@slash_command(name="close", description="Close a mogi")
@is_mogi_not_in_progress()
async def close(self, ctx: ApplicationContext):
async def close(self, ctx: MogiApplicationContext):
await ctx.interaction.response.defer()

close_confirm_message = "{} don't close the mogi unless it fully finished. \nClosing will remove all players and discard any points.\n **Are you sure?**".format(
Expand Down
5 changes: 3 additions & 2 deletions cogs/mogi/open_mogi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from discord import slash_command, ApplicationContext
from discord import slash_command
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.data.mogi_manager import mogi_manager


Expand All @@ -9,7 +10,7 @@ def __init__(self, bot):
self.bot: commands.Bot = bot

@slash_command(name="open", description="Open a mogi")
async def open(self, ctx: ApplicationContext):
async def open(self, ctx: MogiApplicationContext):
try:
mogi_manager.create_mogi(ctx.channel.id)
await ctx.respond("# Started a new mogi! \n Use /join to participate!")
Expand Down
8 changes: 5 additions & 3 deletions cogs/profiles/archive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from discord import SlashCommandGroup, ApplicationContext, Option
from discord import SlashCommandGroup, Option
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from models.PlayerModel import PlayerProfile

from utils.data.database import db_players, db_archived
from utils.command_helpers.find_player import search_player
from utils.command_helpers.checks import is_moderator
Expand All @@ -19,7 +21,7 @@ def __init__(self, bot):
@is_moderator()
async def archive_add(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand All @@ -38,7 +40,7 @@ async def archive_add(
@is_moderator()
async def archive_retrieve(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand Down
9 changes: 5 additions & 4 deletions cogs/profiles/disconnects.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from discord import SlashCommandGroup, ApplicationContext, Option
from discord import SlashCommandGroup, Option
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from models.PlayerModel import PlayerProfile
from utils.data.database import db_players, db_archived

from utils.command_helpers.find_player import search_player
from utils.command_helpers.checks import is_mogi_manager, is_moderator

Expand All @@ -19,7 +20,7 @@ def __init__(self, bot):
@is_mogi_manager()
async def disconnects_add(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand All @@ -39,7 +40,7 @@ async def disconnects_add(
@is_moderator()
async def disconnects_set(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand Down
8 changes: 5 additions & 3 deletions cogs/profiles/edit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pymongo import ReturnDocument

from discord import SlashCommandGroup, ApplicationContext, Option
from discord import SlashCommandGroup, Option
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from models.PlayerModel import PlayerProfile

from utils.data.database import db_players
from utils.command_helpers.find_player import search_player
from utils.command_helpers.checks import is_moderator
Expand All @@ -19,7 +21,7 @@ def __init__(self, bot):
@is_moderator()
async def mmr(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand Down Expand Up @@ -55,7 +57,7 @@ async def mmr(
@is_moderator()
async def username(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand Down
35 changes: 24 additions & 11 deletions cogs/profiles/leaderboard.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from io import BytesIO
import pandas as pd
import dataframe_image as dfi
from pymongo import DESCENDING
from io import BytesIO

import discord
from discord import slash_command, Option, ApplicationContext, Color, File
from discord import slash_command, Option, File
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from utils.data.database import db_players
from utils.maths.ranks import getRankByMMR

Expand All @@ -18,7 +18,7 @@ def __init__(self, bot):
@slash_command(name="leaderboard", description="Show the leaderboard")
async def leaderboard(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
sort=Option(
str,
name="sort",
Expand Down Expand Up @@ -90,7 +90,14 @@ async def leaderboard(
},
{
"selector": "caption",
"props": [("background-color", "rgba(21, 21, 40, 1)"), ("color", "rgba(202, 202, 227, 1)"), ("text-align", "left"), ("font-size", "16px"), ("font-weight", "bold"), ("padding", "7px")],
"props": [
("background-color", "rgba(21, 21, 40, 1)"),
("color", "rgba(202, 202, 227, 1)"),
("text-align", "left"),
("font-size", "16px"),
("font-weight", "bold"),
("padding", "7px"),
],
},
{
"selector": "th",
Expand Down Expand Up @@ -125,12 +132,18 @@ async def leaderboard(

class WebsiteLinkView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None) # Timeout set to None to keep the view persistent
self.add_item(discord.ui.Button(label="View on Website", style=discord.ButtonStyle.link, url=f"https://mk8dx-yuzu.github.io/"))

await ctx.respond(
file=file, view=WebsiteLinkView()
)
super().__init__(
timeout=None
) # Timeout set to None to keep the view persistent
self.add_item(
discord.ui.Button(
label="View on Website",
style=discord.ButtonStyle.link,
url=f"https://mk8dx-yuzu.github.io/",
)
)

await ctx.respond(file=file, view=WebsiteLinkView())


def setup(bot: commands.Bot):
Expand Down
4 changes: 2 additions & 2 deletions cogs/profiles/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from discord import (
slash_command,
ApplicationContext,
Option,
ButtonStyle,
Embed,
Expand All @@ -9,6 +8,7 @@
from discord.ui import View, Button
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from models.RankModel import Rank
from models.PlayerModel import PlayerProfile

Expand All @@ -26,7 +26,7 @@ def __init__(self, bot):
@slash_command(name="player", description="Show a player and their stats")
async def player(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_name: str = Option(
str,
name="name",
Expand Down
8 changes: 5 additions & 3 deletions cogs/profiles/register.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from discord import slash_command, ApplicationContext, Member
from discord import slash_command, Member
from discord.utils import get
from discord.ext import commands

from logger import setup_logger
from models.CustomMogiContext import MogiApplicationContext
from utils.data.database import db_players, db_archived

from logger import setup_logger
from config import GUILD_IDS, REGISTER_CHANNEL_ID

from bson.int64 import Int64
Expand All @@ -27,7 +29,7 @@ async def on_ready(self):
description="Register for playing in Lounge",
guild_ids=GUILD_IDS,
)
async def register(self, ctx: ApplicationContext):
async def register(self, ctx: MogiApplicationContext):
await ctx.defer()

if ctx.channel_id != self.REGISTER_CHANNEL.id:
Expand Down
8 changes: 5 additions & 3 deletions cogs/profiles/suspend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from discord import SlashCommandGroup, ApplicationContext, Option
from discord import SlashCommandGroup, Option
from discord.ext import commands

from models.CustomMogiContext import MogiApplicationContext
from models.PlayerModel import PlayerProfile

from utils.command_helpers.find_player import search_player
from utils.command_helpers.checks import is_moderator

Expand All @@ -18,7 +20,7 @@ def __init__(self, bot):
@is_moderator()
async def suspend_add(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand All @@ -36,7 +38,7 @@ async def suspend_add(
@is_moderator()
async def suspend_remove(
self,
ctx: ApplicationContext,
ctx: MogiApplicationContext,
searched_player: str = Option(
str, name="player", description="username | @ mention | discord_id"
),
Expand Down
5 changes: 3 additions & 2 deletions utils/command_helpers/apply_update_roles.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from discord import ApplicationContext, Member
from discord import Member
from discord.utils import get

from models.CustomMogiContext import MogiApplicationContext
from models.MogiModel import Mogi
from utils.maths.ranks import getRankByMMR


async def update_roles(
ctx: ApplicationContext,
ctx: MogiApplicationContext,
mogi: Mogi,
):
"""
Expand Down
13 changes: 8 additions & 5 deletions utils/command_helpers/confirm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from discord import ApplicationContext
import asyncio
from models.CustomMogiContext import MogiApplicationContext

async def confirmation(ctx: ApplicationContext, text: str) -> bool:

async def confirmation(ctx: MogiApplicationContext, text: str) -> bool:
"""
Sends a confirmation message and waits for the user to react with a checkmark or X.
:param ctx: The context of the command.
:param text: The custom text to display in the confirmation message.
:return: True if the user confirms, False otherwise.
Expand All @@ -17,11 +18,13 @@ def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ["✅", "❌"]

try:
reaction, user = await ctx.bot.wait_for("reaction_add", timeout=60.0, check=check)
reaction, user = await ctx.bot.wait_for(
"reaction_add", timeout=60.0, check=check
)
if str(reaction.emoji) == "✅":
return True
else:
return False
except asyncio.TimeoutError:
await ctx.send("Confirmation timed out.")
return False
return False
5 changes: 3 additions & 2 deletions utils/command_helpers/wait_for.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio

from discord import ApplicationContext, TextChannel, Bot
from discord import TextChannel, Bot
from models.CustomMogiContext import MogiApplicationContext


async def get_awaited_message(
bot: Bot, ctx: ApplicationContext, target_channel: TextChannel
bot: Bot, ctx: MogiApplicationContext, target_channel: TextChannel
) -> str:
try:
message = await bot.wait_for(
Expand Down

0 comments on commit 8d1ae3a

Please sign in to comment.