Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Fixes + Added Invite Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Weever1337 committed Oct 30, 2023
1 parent 7a0bfc5 commit 89175c3
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 45 deletions.
8 changes: 6 additions & 2 deletions src/cogs/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ async def title_button(
await interaction.channel.send(f"New title: `{title}`", delete_after=10)


class FormsCog(commands.Cog):
class Forms(commands.Cog):
"""Helper commands to setup forms."""

EMOJI = "📝"

def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.forms = forms
Expand Down Expand Up @@ -197,4 +201,4 @@ async def delete(


def setup(bot: commands.Bot) -> None:
bot.add_cog(FormsCog(bot=bot))
bot.add_cog(Forms(bot=bot))
73 changes: 73 additions & 0 deletions src/cogs/fun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from disnake.ext import commands
import disnake
import random


class Fun(commands.Cog):
"""Fun commands."""

EMOJI = "😂"

def __init__(self, bot):
self.bot = bot

@commands.slash_command(name="roll", description="Rolls a dice.")
async def roll(
self,
interaction: disnake.MessageCommandInteraction,
number: int = commands.Param(description="Default: 6", default=6),
):
roll = random.randint(1, number)
embed = disnake.Embed(
title="Rolled", color=0x2F3136, description=f"You rolled a `{roll}`"
)
await interaction.send(embed=embed)

@commands.slash_command(name="flip", description="Flips a coin.")
async def flip(self, interaction: disnake.MessageCommandInteraction):
if random.randint(0, 1) == 0:
embed = disnake.Embed(
title="Flip coin", color=0x2F3136, description="Heads"
)
else:
embed = disnake.Embed(
title="Flip coin", color=0x2F3136, description="Tails"
)
await interaction.send(embed=embed)

@commands.slash_command(name="8ball", description="Ask the magic 8ball a question.")
async def eight_ball(
self, interaction: disnake.MessageCommandInteraction, question: str
):
responses = [
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
]
embed = disnake.Embed(
title="Magic 8ball",
color=0x2F3136,
description=f"Question: `{question}`\nAnswer: `{random.choice(responses)}`",
)
await interaction.send(embed=embed)


def setup(bot: commands.Bot):
bot.add_cog(Fun(bot=bot))
8 changes: 6 additions & 2 deletions src/cogs/giveaways.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
time_units = {"d": "days", "h": "hours", "m": "minutes", "s": "seconds"}


class GiveawayCog(commands.Cog): # Need rewrite
class Giveaway(commands.Cog): # Need rewrite
"""Helper commands to setup giveaway."""

EMOJI = "🎉"

def __init__(self, bot) -> None:
self.bot = bot

Expand Down Expand Up @@ -139,4 +143,4 @@ async def reroll(self, interaction, message_id, winners: int):


def setup(bot: commands.Bot) -> None:
bot.add_cog(GiveawayCog(bot))
bot.add_cog(Giveaway(bot))
18 changes: 12 additions & 6 deletions src/cogs/invitetracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from src.utils.misc import check_channel


class InviteTrackerCog(commands.Cog):
class InviteTracker(commands.Cog):
"""Helper commands to setup invite tracker system."""

EMOJI = "🔗"

def __init__(self, bot) -> None:
self.bot = bot
self.invites = invites
Expand All @@ -26,16 +30,18 @@ async def set_invite_channel(
interaction: disnake.MessageCommandInteraction,
channel: disnake.TextChannel,
):
invite_channel = await check_channel(
channel=channel, interaction=interaction
invite_channel = await check_channel(channel=channel, interaction=interaction)
await self.invites.update_invite_info(
guild_id=interaction.guild.id, inviter=interaction.user, invited=[]
)
await self.invites.update_invite_info(guild_id = interaction.guild.id, inviter = interaction.user, invited = [])
await interaction.edit_original_response(
content="",
embed=disnake.Embed(
title="Invite Tracker",
description="Successfully setup invite tracker to channel!",
)
),
)


def setup(bot: commands.Bot):
bot.add_cog(InviteTrackerCog(bot))
bot.add_cog(InviteTracker(bot))
8 changes: 6 additions & 2 deletions src/cogs/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from src.utils.misc import check_channel


class LogsCog(commands.Cog):
class Logger(commands.Cog):
"""Helper commands to setup logger."""

EMOJI = "🗒️"

def __init__(self, bot) -> None:
self.bot = bot
self.logger = logger
Expand Down Expand Up @@ -134,4 +138,4 @@ async def set_log_channel(


def setup(bot: commands.Bot):
bot.add_cog(LogsCog(bot))
bot.add_cog(Logger(bot))
44 changes: 27 additions & 17 deletions src/events/invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ async def on_invite_create(self, invite: disnake.Invite) -> None:
logger_channel = await self.logger.get_loggers(
guild_id=invite.guild.id, to_return="invite"
)

if not logger_channel:
return

embed = disnake.Embed(title="Synth | Created Invite", description=None, color=0x2F3136)

embed = disnake.Embed(
title="Synth | Created Invite", description=None, color=0x2F3136
)
embed.add_field(
name="Channel",
value=f"{invite.channel.mention} (`ID: {invite.channel.id}`)",
Expand All @@ -36,28 +38,36 @@ async def on_invite_create(self, invite: disnake.Invite) -> None:
name="Inviter",
value=f"{invite.inviter.name} (`ID: {invite.inviter.id}`)",
)

age_format = disnake.utils.format_dt(datetime.now().timestamp() + invite.max_age, style = "f")
embed.add_field(name="Max age", value=(
"Infinite"
if invite.max_age == 0
else age_format
), inline=False)
embed.add_field(name="Max uses", value="Infinite" if invite.max_uses == 0 else invite.max_uses, inline=False)


age_format = disnake.utils.format_dt(
datetime.now().timestamp() + invite.max_age, style="f"
)
embed.add_field(
name="Max age",
value=("Infinite" if invite.max_age == 0 else age_format),
inline=False,
)
embed.add_field(
name="Max uses",
value="Infinite" if invite.max_uses == 0 else invite.max_uses,
inline=False,
)

channel = invite.guild.get_channel(int(logger_channel))
await channel.send(embed=embed)

@commands.Cog.listener()
async def on_invite_delete(self, invite: disnake.Invite) -> None:
logger_channel = await self.logger.get_loggers(
guild_id=invite.guild.id, to_return="invite"
)

if not logger_channel:
return

embed = disnake.Embed(title="Synth | Deleted Invite", description=None, color=0x2F3136)

embed = disnake.Embed(
title="Synth | Deleted Invite", description=None, color=0x2F3136
)
embed.add_field(
name="Channel",
value=f"{invite.channel.mention} (`ID: {invite.channel.id}`)",
Expand All @@ -69,7 +79,7 @@ async def on_invite_delete(self, invite: disnake.Invite) -> None:
value=disnake.utils.format_dt(datetime.now(), style="f"),
inline=True,
)

channel = invite.guild.get_channel(int(logger_channel))
await channel.send(embed=embed)

Expand Down
5 changes: 4 additions & 1 deletion src/events/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

from disnake import Member
from disnake.ext import commands
from src.utils import economy
from src.utils import economy, invites


class EventMember(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
super(EventMember, self).__init__()
self.bot = bot
self.economy = economy
self.invites = invites

@commands.Cog.listener()
async def on_member_join(self, member: Member) -> None:
if member.bot:
return

await self.economy.add_member(member=member)
# await self.invites.update_invite_info(guild_id=member.guild.id)
logging.info(f"Member joined: {member}")


Expand Down
8 changes: 5 additions & 3 deletions src/utils/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def _add_to_cache(self, param_filter: Mapping[str, Any]) -> Any:
self.collection_cache.append(param_filter)
return param_filter

def _update_cache(self, old_value: Mapping[str, Any], new_value: Mapping[str, Any]) -> Any:
def _update_cache(
self, old_value: Mapping[str, Any], new_value: Mapping[str, Any]
) -> Any:
try:
index = self.collection_cache.index(old_value)
self.collection_cache[index].update(new_value)
except ValueError:
self.collection_cache.append(new_value)

async def get_items_in_db(
self,
find_dict: Mapping[str, Any],
Expand Down Expand Up @@ -109,4 +111,4 @@ async def update_db(
) -> None:
await self.collection.update_one(data, {"$set": new_value}, upsert=True)
old_value = await self.find_one_from_db(data)
self._update_cache(old_value, new_value)
self._update_cache(old_value, new_value)
21 changes: 10 additions & 11 deletions src/utils/database/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ async def get_invites(
):
return []

result = (
await self.find_one({"guild_id": guild_id}, return_first_result=True)
)
result = await self.find_one({"guild_id": guild_id}, return_first_result=True)
print(result)
if result and to_return:
return result.get(to_return, "")
Expand All @@ -35,10 +33,10 @@ async def update_invite_info(
guild_id: Union[int, str, disnake.Guild],
inviter: Union[disnake.Member, disnake.User] = None,
invited: Union[disnake.Member, disnake.User] = None,

) -> Optional[Dict[str, str]]:
if await self.find_one_from_db({"guild_id": guild_id}) is None:
return await self.add_to_db({
return await self.add_to_db(
{
"guild_id": guild_id,
"invited_id": inviter.id,
"count": 0,
Expand All @@ -61,7 +59,12 @@ async def update_invite_info(
{"guild_id": guild_id},
{"invites": invites, "count": count},
)
return {"guild_id": guild_id, "inviter_id": inviter.id, "invites": invites, "count": count}
return {
"guild_id": guild_id,
"inviter_id": inviter.id,
"invites": invites,
"count": count,
}

async def create_tracker(
self,
Expand All @@ -82,9 +85,5 @@ async def create_tracker(

return await self.update_db(
{"guild_id": guild_id},
{
"invited_id": inviter_id.id,
"count": count + 1,
"invites": invites
},
{"invited_id": inviter_id.id, "count": count + 1, "invites": invites},
)
2 changes: 1 addition & 1 deletion src/utils/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def cog_help_embed(self, cog: Optional[commands.Cog]) -> Embed:
)
emoji = getattr(cog, "EMOJI", None)
return await self.embed_help(
title=f"{emoji} {cog.qualified_name}" if emoji else cog.qualified_name,
title=f"{emoji} {cog.qualified_name}\n" if emoji else cog.qualified_name,
description=cog.description,
command_list=cog.get_commands(),
)
Expand Down
31 changes: 31 additions & 0 deletions src/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Union
from json import loads

from disnake import (
Message,
Expand All @@ -13,6 +14,36 @@

from . import main_db

# with open("src\\assets\\langs\\ru.json", "r", encoding="utf-8") as ru_lang:
# r = ru_lang.read()
# ru = loads(r)
# with open("src\\assets\\langs\\ua.json", "r", encoding="utf-8") as ua_lang:
# r = ua_lang.read()
# ua = loads(r)
# with open("src\\assets\\langs\\en.json", "r", encoding="utf-8") as en_lang:
# r = en_lang.read()
# en = loads(r)
# langs_detect = {"ru": ru, "en": en, "ua": ua}

# def get_language(
# guild_id: int,
# category: Literal["errors", "lang", "logs"],
# sub_category: str,
# name: str = None,
# ):
# global langs_detect
# if (
# mongo.config_data.get(guild_id) is not None
# and mongo.config_data.get(guild_id)["lang"]
# ):
# lang = mongo.config_data.get(guild_id)["lang"]
# if name:
# return langs_detect[lang][category][sub_category][name]
# else:
# return langs_detect[lang][category][sub_category]
# else:
# return False


async def bot_get_guild_prefix(bot: commands.Bot, message: Message) -> List[str]:
if not message.guild or await main_db.get_prefix(message.guild.id) is None:
Expand Down

0 comments on commit 89175c3

Please sign in to comment.