Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing converters to get called without arguments. #6383

Closed
ghost opened this issue Feb 4, 2021 · 3 comments
Closed

Allowing converters to get called without arguments. #6383

ghost opened this issue Feb 4, 2021 · 3 comments
Labels
wontfix This will not be worked on.

Comments

@ghost
Copy link

ghost commented Feb 4, 2021

Summary

Commands.Convert using Optinal[str] argument rather than mandatory

What is the feature request for?

discord.ext.commands

The Problem

I'm trying to implement a converter that has different behavior depending on the argument and even if it isn't given.

The Ideal Solution

It'd be quite useful if the converters work even if no argument is provided, taking into account that they currently don't get executed if no argument is passed, rather than allowing the convert function to handle it by itself in such cases.

The Current Solution

The current solution is basically using Optional str, in the command itself, and only refer to Converter if an argument is provided, if it's not provided, then using conditionals within the core to handle the function when no parameters are passed.

from typing import Optional
from discord.ext import commands
class Reference(commands.Converter):
    async def convert(self, ctx: commands.context.Context, argument: Optional[str]):
        if not argument: return 0
        if argument.isdigit(): return 1
        return 2

@bot.command()
async def value(ctx, *, num: Reference):
    await ctx.send(num)
@ghost ghost added the feature request This is a feature request. label Feb 4, 2021
@Rapptz
Copy link
Owner

Rapptz commented Feb 4, 2021

This isn't gonna happen.

If you want a default parameter you're free to provide one yourself:

@bot.command()
async def value(ctx, *, num: Reference = 1):
    await ctx.send(num)

@Rapptz Rapptz closed this as completed Feb 4, 2021
@Rapptz Rapptz added wontfix This will not be worked on. and removed feature request This is a feature request. labels Feb 4, 2021
@ghost
Copy link
Author

ghost commented Feb 4, 2021

Ah, I mostly suggested because I was trying to implement a default parameter that uses the context's information, in order to retrieve a message, which goes this way but the argument requirement causes the function to not get executed.

class Reference(commands.Converter):
    async def convert(self, ctx: commands.context.Context, argument: Optional[str]):
        def process(message: Optional[discord.Message], ctx: commands.context.Context):
            if not message: return None
            if message.author == ctx.bot.user and message.embeds:
                return message
            return None
        try:
            if not argument:
                ref_msg : discord.MessageReference = ctx.message.reference
                channel: discord.TextChannel = ctx.channel
                message: discord.Message = await channel.fetch_message(ref_msg.message_id)
                return await process(message, ctx)
            else:
                URL = REGEX_URL.match(argument)
                if URL:
                    guild_id, channel_id, message_id = map(int, URL.groups())
                    guild: discord.Guild = ctx.bot.get_guild(guild_id)
                    channel: discord.TextChannel = guild.get_channel(channel_id)
                    message: discord.Message = await channel.fetch_message(message_id)
                    return await process(message, ctx)
                elif argument.isdigit():
                    channel: discord.TextChannel = ctx.channel
                    message: discord.Message = await channel.fetch_message(int(argument))
                    return await process(message, ctx)
                return None
        except Exception:
            return None

@Rapptz
Copy link
Owner

Rapptz commented Feb 4, 2021

The answer is mostly the same. A different approach is implemented in #1849

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on.
Projects
None yet
Development

No branches or pull requests

1 participant