Skip to content

Commit

Permalink
☃️ Convert commands to hybrid commands and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tookender committed May 28, 2024
1 parent 6620e2a commit 65dcff8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions extensions/config/levelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

import discord
from discord import app_commands
from discord.ext import commands

from utils import Embed, Interaction, Invalid

Expand Down Expand Up @@ -184,7 +184,7 @@ async def update_message(interaction: Interaction, edit: Optional[bool] = True):


class LevellingConfig(ConfigBase):
group = app_commands.Group(name="config", description="Configure your guild's bot configuration.")
group = commands.hybrid_group(name="config", description="Configure your guild's bot configuration.")

@group.command(description="Configure your guild's levelling system.")
async def levelling(self, interaction: Interaction):
Expand Down
46 changes: 30 additions & 16 deletions extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,72 @@

import discord
from discord import app_commands
from discord.ext import commands

from utils import Embed, Interaction

from ._base import LevellingBase


class CommandsCog(LevellingBase):
@app_commands.command(description="View the level of the specified member.")
@commands.hybrid_command(description="View the level of the specified member.")
@app_commands.describe(user="The user you want to view the level of.")
@app_commands.guild_only()
async def level(self, interaction: Interaction, user: Optional[discord.Member] = None):
if not interaction.guild or isinstance(interaction.user, discord.User):
async def level(self, ctx, user: Optional[discord.Member] = None):
if not ctx.guild or isinstance(ctx.author, discord.User):
return

guild_levelling = await self.bot.pool.fetchval("SELECT levelling_enabled FROM guilds WHERE guild_id = $1", interaction.guild.id)
guild_levelling = await self.bot.pool.fetchval("SELECT levelling_enabled FROM guilds WHERE guild_id = $1", ctx.guild.id)

if not guild_levelling:
embed = Embed(
title="⚡ | Levelling disabled!",
description="This guild has levelling disabled, to enable it use the `/config levelling` command.",
description=f"{self.bot.E['no']} This guild has levelling disabled, to enable it use the `/config levelling` command.",
)

return await interaction.response.send_message(embed=embed)
embed.set_author(
name=ctx.author.global_name,
icon_url=ctx.author.avatar.url if ctx.author.avatar else None,
)

return await ctx.send(embed=embed)

user = user if user else interaction.user
user = user if user else ctx.author

data = await self.bot.pool.fetchrow(
"SELECT level, xp FROM levels WHERE guild_id = $1 AND user_id = $2",
interaction.guild.id,
ctx.guild.id,
user.id,
)

if not data:
await self.bot.pool.execute(
"INSERT INTO levels (guild_id, user_id, level, xp) VALUES ($1, $2, $3, $4)",
interaction.guild.id,
ctx.guild.id,
user.id,
0,
0,
)

q = "'"

embed = Embed(
title="⚡ | No messages!",
description=f"{'You have' if interaction.user.id == user.id else f'{user.display_name} has' } not sent any messages in this server.",
description=f"{self.bot.E['no']} {f'You don{q}t have' if ctx.user.id == user.id else f'{user.display_name} doesn{q}t has'} a level yet. Try sending more messages.",
)

return await interaction.response.send_message(embed=embed)
embed.set_author(
name=user.global_name,
icon_url=user.avatar.url if user.avatar else None,
)

ap = "'"
return await ctx.send(embed=embed)

embed = Embed(
title=f"⚡ | {'Your' if interaction.user.id == user.id else f'{user.display_name}{ap}s'} level",
description=f"**Level:** `{data[0]}`\n" f"**XP:** `{data[1]} / {math.floor(10 * (data[0] ^ 2) + (55 * data[0]) + 100)}`",
)

return await interaction.response.send_message(embed=embed)
embed.set_author(
name=user.global_name,
icon_url=user.avatar.url if user.avatar else None,
)

return await ctx.send(embed=embed)

0 comments on commit 65dcff8

Please sign in to comment.