Reputation: 13
what i want to do is something like this:
have a command modifyImage
@commands.command()
async def modifyImage(ctx: Context, imgName: DefaultImageConverter, size: int):
img = imageList[imgName]
img.scale(size)
a command setDefaltImage
@commands.command()
async def setdefaultImage(ctx: Context, imgName: str):
global defaultImg
defaultImg = imgName
and a converter DefaultImageConverter
class DefaultImageConverter(commands.Converter):
async def convert(self, ctx, imgName):
if imgName not in imageList:
if defaultImg != '':
return defaultImg
if defaultImg == '':
ctx.reply("you didn't pass a image and doesn't have a defalt image")
raise BadArgument
elif imgName in imageList:
return imgName
the DefaultImageConverter
would be a converter that checks the a image exist and return them, if not, return the default image if exist, if even that doesn't existe, he will raise a error
but the program doesn't work this way, because if i use the command !modifyImage 100
the number '100' won't be passed to the parameter size
, because technically the conversion worked
so i need to do
@commands.command()
async def modifyImage(ctx: Context, imgName: typing.Optional[DefaultImageConverter], size: int):
if imgName == None:
if defaultImg != '':
imgName = defaultImg
else:
ctx.send("you didn't pass a image and doesn't have a defalt image")
return
img = imageList[imgName]
img.scale(size)
and the converter be
class DefaultImageConverter(commands.Converter):
async def convert(self, ctx, imgName):
if imgName not in imageList:
return BadArgument
else:
return imgName
what i need to do for use something like the first option, because the if imgName == None:
part become very repetetive, and every function that modify a image has this
tl;dr
i want a way to do a discord.py converter, at the same time, return a value, and pass the current argument value to the next parameter
the only way that i know to the converter 'pass the argument' is using a typing.Optional and raising a error in the converter, but doing this the argument become None
Upvotes: 0
Views: 302
Reputation: 528
but the program doesn't work this way, because if i use the command
!modifyImage 100
the number '100' won't be passed to the parameter size, because technically the conversion worked
Just like regular functions, you can't put optional arguments before mandatory ones. You need to reorder your arguments so that your size
is expected first, then your imgName
.
what i need to do for use something like the first option, because the
if imgName == None:
part become very repetetive, and every function that modify a image has this
Your first option was mostly fine, you just need to swap the arguments as mentioned, and include the None
check in your converter. Try this:
class DefaultImageConverter(commands.Converter):
async def convert(self, ctx, imgName):
if imgName is None or imgName not in imageList:
if defaultImg:
return defaultImg
else:
ctx.reply("you didn't pass a image and doesn't have a defalt image")
raise BadArgument
else:
return imgName
@commands.command()
async def modifyImage(ctx: Context, size: int,
imgName: typing.Optional[DefaultImageConverter]):
img = imageList[imgName]
img.scale(size)
Then your !modifyImage 100
example should work, as well as !modifyImage 100 someImage
.
Upvotes: 1