Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

How to make a discord.py converter pass thorugh a argument and time return a value

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

question from:https://stackoverflow.com/questions/65884376/how-to-make-a-discord-py-converter-pass-thorugh-a-argument-and-time-return-a-val

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...