Skip to content

Commit

Permalink
Merge pull request #4 from Shirataki2/#2-alias-support
Browse files Browse the repository at this point in the history
[FEATURE(#2)] add alias support
  • Loading branch information
Shirataki2 authored Feb 28, 2021
2 parents de62a40 + c86778b commit f8e3f17
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

### Expected Behavior


Expand Down
6 changes: 1 addition & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@


### Fix or Enhancement?


- [ ] All tests passed


### Environment
- OS: Write here
- Go version: Write here
- Python Version: Write here
22 changes: 13 additions & 9 deletions discord/ext/levenshtein/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from collections import namedtuple
from discord.ext import commands
from typing import List
from typing import Set

from .inner_cog import InnerLevenshtein

VersionInfo = namedtuple('VersionInfo', 'major minor micro releaselevel serial')
version_info = VersionInfo(major=0, minor=2, micro=2, releaselevel='alpha', serial=0)
version_info = VersionInfo(major=0, minor=3, micro=0, releaselevel='alpha', serial=0)

__version__ = '.'.join(map(str, [version_info.major, version_info.minor, version_info.micro]))

Expand Down Expand Up @@ -41,10 +41,10 @@ class Levenshtein:

def __init__(self, bot: commands.Bot, max_length=3):
self.bot = bot
self._command_names: List[str] = []
self._command_names: Set[str] = set()
self._listup_commands(self.bot)
self._max_length = max_length
cog = InnerLevenshtein(self.bot, self._max_length, self._command_names)
cog = InnerLevenshtein(self.bot, self._max_length, list(self._command_names))
self.bot.add_cog(cog)

def _listup_commands(self, group, prefix=None):
Expand All @@ -58,13 +58,17 @@ def _listup_commands(self, group, prefix=None):
continue

elif isinstance(command, commands.Group):
self._command_names.append(prefix_str + command.name)
prefix.append(command.name)
self._listup_commands(command, prefix)
prefix.pop()
names = [command.name] + list(command.aliases)
for name in names:
self._command_names.add(prefix_str + name)
prefix.append(command.name)
self._listup_commands(command, prefix)
prefix.pop()

elif isinstance(command, commands.Command):
self._command_names.append(prefix_str + command.name)
names = [command.name] + list(command.aliases)
for name in names:
self._command_names.add(prefix_str + name)

@property
def max_length(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/cog-based/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, *args, **kwargs):

async def on_ready(self):
print('Bot is ready')
levenshtein.Levenshtein(self, max_length=3)
levenshtein.Levenshtein(self, max_length=4)

async def on_command_suggest(self, ctx, suggested_commands):
body = 'suggested commands: ' + ' '.join([f'`{command}`' for command in suggested_commands])
Expand Down
10 changes: 7 additions & 3 deletions examples/cog-based/math_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ class Math(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.command()
@commands.command(aliases=['a'])
async def add(self, ctx, a: int, b: int):
await ctx.send(a + b)

@commands.command()
@commands.command(aliases=['m', 'multiply'])
async def mul(self, ctx, a: int, b: int):
await ctx.send(a * b)

@commands.group(invoke_without_command=True)
@commands.group(aliases=['constants'], invoke_without_command=True)
async def consts(self, ctx):
await ctx.send_help('consts')

@consts.command()
async def pi(self, ctx):
await ctx.send(f'{math.pi}')

@consts.command(aliases=['2pi'])
async def tau(self, ctx):
await ctx.send(f'{math.pi*2.0}')

@consts.command()
async def e(self, ctx):
await ctx.send(f'{math.e}')
Expand Down

0 comments on commit f8e3f17

Please sign in to comment.