Skip to content

Commit

Permalink
Update shortcuts.py
Browse files Browse the repository at this point in the history
Updated per abi's suggestions
  • Loading branch information
skruglov2023 authored Aug 18, 2024
1 parent 95f1d22 commit 31c6f38
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions dozer/cogs/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import discord
from discord.ext import commands
from discord.ext.commands import BadArgument, guild_only, has_permissions
# from fuzzywuzzy import process, fuzz

from dozer.context import DozerContext
from ._utils import *
Expand Down Expand Up @@ -143,9 +142,6 @@ async def on_message(self, msg: discord.Message):
prefix = setting.prefix
prefix_index = msg.content.find(prefix)

# Check for the presence of the prefix anywhere in the message
prefix_index = msg.content.find(prefix)

if prefix_index != -1:
# Ensure there's no space directly after the prefix
if prefix_index + len(prefix) < len(msg.content) and msg.content[prefix_index + len(prefix)] == ' ':
Expand All @@ -156,7 +152,10 @@ async def on_message(self, msg: discord.Message):
remaining_content = msg.content[start_index:].strip()

# Extract the command name before any spaces or punctuation
command_name = remaining_content.split()[0] if remaining_content.split() else ""
message_segments = remaining_content.split()
if not message_segments:
return
command_name = message_segments[0]

# Check if the extracted command name matches any valid shortcuts
all_shortcuts = await ShortcutEntry.get_by(guild_id = msg.guild.id)
Expand All @@ -171,11 +170,8 @@ async def on_message(self, msg: discord.Message):
# Ping the original author in the new message
await original_message.reply(f"{shortcut.value}")
else:
# Send the shortcut value without pinging if original message is not found
# Send the shortcut value as a non-reply message if msg is not a reply
await msg.channel.send(shortcut.value)
else:
# If the prefix is not found in the message, do nothing
pass

async def setup(bot):
"""Adds the shortcuts cog to the main bot project."""
Expand Down Expand Up @@ -247,3 +243,15 @@ async def get_by(cls, **kwargs):
value=result.get("value"))
result_list.append(obj)
return result_list

@classmethod
async def get_unique_by(cls, **kwargs) -> Optional[Self]:
# In the rare case that get_by gives us tons of records this will be very inefficient,
# but that is never expected to happen, so it should be fine.
settings = await cls.get_by(**kwargs)
if not settings:
return None

assert len(settings) == 1

return settings[0]

0 comments on commit 31c6f38

Please sign in to comment.