fixed dynamic command listing for gifs
This commit is contained in:
1 parent
6eb850b7b3
commit
90793e33fe
1 file changed
+7
-5
+7
-5
@@ -6,6 +6,7 @@ from discord.ext import commands
|
|||||||
class Gifs(commands.Cog):
|
class Gifs(commands.Cog):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
self._gif_names = []
|
||||||
self._load_gif_commands()
|
self._load_gif_commands()
|
||||||
|
|
||||||
def _load_gif_commands(self):
|
def _load_gif_commands(self):
|
||||||
@@ -13,20 +14,21 @@ class Gifs(commands.Cog):
|
|||||||
return
|
return
|
||||||
for filename in os.listdir('gifs'):
|
for filename in os.listdir('gifs'):
|
||||||
if filename.endswith('.gif'):
|
if filename.endswith('.gif'):
|
||||||
name = filename[:-4] # strip .gif
|
name = filename[:-4]
|
||||||
|
self._gif_names.append(name)
|
||||||
self._make_command(name)
|
self._make_command(name)
|
||||||
|
|
||||||
def _make_command(self, name: str):
|
def _make_command(self, name: str):
|
||||||
async def gif_command(self, ctx):
|
async def gif_command(ctx):
|
||||||
await ctx.reply(file=discord.File(f'gifs/{name}.gif'))
|
await ctx.reply(file=discord.File(f'gifs/{name}.gif'))
|
||||||
gif_command.__name__ = name
|
gif_command.__name__ = name
|
||||||
cmd = commands.command(name=name)(gif_command)
|
cmd = commands.command(name=name)(gif_command)
|
||||||
self.add_command(cmd)
|
self.bot.add_command(cmd)
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def gifs(self, ctx):
|
async def gifs(self, ctx):
|
||||||
gif_commands = [f'!{cmd.name}' for cmd in self.get_commands() if cmd.name != 'gifs']
|
gif_commands = [f'!{name}' for name in sorted(self._gif_names)]
|
||||||
await ctx.reply('\n'.join(sorted(gif_commands)))
|
await ctx.reply('\n'.join(gif_commands))
|
||||||
|
|
||||||
async def setup(bot):
|
async def setup(bot):
|
||||||
await bot.add_cog(Gifs(bot))
|
await bot.add_cog(Gifs(bot))
|
||||||
Reference in new issue
Block a user