Skip to content

Commit

Permalink
Merge pull request #66 from PaulMarisOUMary/me-per-guild
Browse files Browse the repository at this point in the history
Me per guild
  • Loading branch information
PaulMarisOUMary committed May 30, 2022
2 parents 2ca618a + d147689 commit e6a643d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ COLLATE utf8mb4_unicode_ci;
```sql
CREATE TABLE IF NOT EXISTS `table_me`
(
`guild_id` BIGINT unsigned NOT NULL,
`user_id` BIGINT unsigned NOT NULL,
`user_me` varchar(1024),
UNIQUE(`user_id`)
CONSTRAINT `me_per_guild` UNIQUE (`guild_id`, `user_id`)
)
ENGINE = InnoDB,
CHARACTER SET utf8mb4,
Expand Down
9 changes: 7 additions & 2 deletions classes/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ async def select(self, table: str, target: str, condition: str = '', order: str
async def count(self, table: str, what: str, condition: str = '') -> select:
return await self.select(table, f"COUNT({what})", condition)

async def lookup(self, table: str, target: str, what: str, which: str) -> select:
return await self.select(table, target, f"{what} REGEXP '{which}'")
async def lookup(self, table: str, target: str, dict: dict) -> select:
condition = ''
for i, items in enumerate(dict.items()):
key, value = items
condition += f"`{key}` = {self.__toKindFormat(value)} AND" if i+1 < len(dict) else f"`{key}` = {self.__toKindFormat(value)}"

return await self.select(table, target, condition)

async def exist(self, table: str, target: str, condition: str = '') -> bool:
response = await self.count(table, target, condition)
Expand Down
2 changes: 1 addition & 1 deletion cogs/birthday.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async def show_birthday(self, interaction: discord.Interaction, user: discord.Me
await self.show_birthday_message(interaction, user)

async def show_birthday_message(self, interaction: discord.Interaction, user: discord.Member) -> None:
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_birth", "user_id", str(user.id))
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_birth", {"user_id": str(user.id)})
if response:
dataDate : date = response[0][0]
timestamp = round(time.mktime(dataDate.timetuple()))
Expand Down
4 changes: 2 additions & 2 deletions cogs/croissants.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def croissants_lore(self, interaction: discord.Interaction) -> None:
@app_commands.checks.cooldown(1, 10.0, key=lambda i: (i.guild_id, i.user.id))
async def croissants_show(self, interaction: discord.Interaction, user: discord.Member) -> None:
"""Show how many croissants a user paid."""
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_count", "user_id", str(user.id))
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_count", {"user_id": str(user.id)})

if response:
text = f"{user.mention} have `{response[0][0]}` croissants {self.EMOJI} !"
Expand Down Expand Up @@ -101,7 +101,7 @@ async def __send_croissants(self, message) -> None:
async def __increment_croissants_counter(self, user_id : int) -> int:
await self.bot.database.insert_onduplicate(self.subconfig_data["table"], {"user_id": user_id, "user_count": MixedTypes("COALESCE(user_count, 0) + 1")})

response = await self.bot.database.lookup(self.subconfig_data["table"], "user_count", "user_id", str(user_id))
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_count", {"user_id": str(user_id)})
return response[0][0]

def __get_screenshot(self, author : discord.Member, content : str) -> discord.File:
Expand Down
9 changes: 4 additions & 5 deletions cogs/me.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

self.subconfig_data: dict = self.bot.config["cogs"][self.__cog_name__.lower()]
self.max_lenght_me = self.subconfig_data["max_length"]

def help_custom(self) -> tuple[str, str, str]:
emoji = '🤙'
Expand All @@ -33,10 +32,10 @@ async def me(self, interaction: discord.Interaction, description: str):
"""Allows you to set or show a brief description of yourself."""
try:
text = description.replace("'", "''")
if len(text) > self.max_lenght_me:
raise commands.CommandError(f"The max-lenght of your *me* is set to: __{self.max_lenght_me}__ (yours is {len(text)}).")
if max_lenght_me := self.subconfig_data["max_length"] < len(text) :
raise commands.CommandError(f"The max-lenght of your *me* is set to: __{max_lenght_me}__ (yours is {len(text)}).")

await self.bot.database.insert_onduplicate(self.subconfig_data["table"], {"user_id": interaction.user.id, "user_me": text})
await self.bot.database.insert_onduplicate(self.subconfig_data["table"], {"guild_id": interaction.guild_id, "user_id": interaction.user.id, "user_me": text})

await self.show_me_message(interaction, interaction.user)
except Exception as e:
Expand All @@ -52,7 +51,7 @@ async def show_me(self, interaction: discord.Interaction, user: discord.Member =
await self.show_me_message(interaction, user)

async def show_me_message(self, interaction: discord.Interaction, user: discord.Member) -> None:
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_me", "user_id", str(user.id))
response = await self.bot.database.lookup(self.subconfig_data["table"], "user_me", {"guild_id": str(user.guild.id),"user_id": str(user.id)})
message = " ".join(response[0]) if len(response) else "No description provided.."
await interaction.response.send_message(f"• **{user.display_name}** {message}")

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ PyNaCl
#Require this specific version (internal package error, issue #299 of github.com/ssut/py-googletrans)
googletrans == 4.0.0-rc1

#discordpy master branch commit 77baa06a99b00b50d24398714bce40fcda1fa0c2
git+https://github.com/Rapptz/discord.py@77baa06a99b00b50d24398714bce40fcda1fa0c2
#discordpy master branch commit 36f039a1bffb835a555be8a43976397ba6eb9c76
git+https://github.com/Rapptz/discord.py@36f039a1bffb835a555be8a43976397ba6eb9c76

0 comments on commit e6a643d

Please sign in to comment.