Search code examples
pythondiscorddiscord.pybotspycord

How do I make the bot take as input the text of the message to which I am responding?


Thank you so much for your help, now I know more, good luck to all!

I'm new to programming, and a complete novice at making bots in discord. Nevertheless, I decided to create one of my own, and add it to my server.

I'm trying to make a discord bot that translates text with wrong keyboard layout (a current problem for countries with non-Latin alphabet). It functions properly, but I want to add a function so that you can reply to the message of another user, calling the bot, using the prefix (example in attached screenshot). However, my knowledge isn't too much. In other words, I want to make it so that when you reply to a user's message and write the command - the command is played as if in the string itself was his mention, that is, do not write "!command @user", but just reply to the message of this user with the command bot. And then the bot already takes the text of the message to which you responded, and processes it.

I found this option:

def my_dict(text):
    PROGRAM_DICT = {'q': 'й', 'w': 'ц', 'e': 'у', 'r': 'к', 't': 'е', 'y': 'н', 'u': 'г', 'i': 'ш',
                    'o': 'щ', 'p': 'з', '[': 'х', ']': 'ъ', 'a': 'ф', 's': 'ы', 'd': 'в', 'f': 'а',
                    'g': 'п', 'h': 'р', 'j': 'о', 'k': 'л', 'l': 'д', ';': 'ж', "'": 'э', 'z': 'я',
                    'x': 'ч', 'c': 'с', 'v': 'м', 'b': 'и', 'n': 'т', 'm': 'ь', ',': 'б', '.': 'ю',
                    'Q': 'Й', 'W': 'Ц', 'E': 'У', 'R': 'К', 'T': 'Е', 'Y': 'Н', 'U': 'Г', 'I': 'Ш',
                    'O': 'Щ', 'P': 'З', '{': 'Х', '}': 'Ъ', ':': 'Ж', '"': 'Э', 'A': 'Ф', 'S': 'Ы',
                    'D': 'В', 'F': 'А', 'G': 'П', 'H': 'Р', 'J': 'О', 'K': 'Л', 'L': 'Д', 'Z': 'Я',
                    'X': 'Ч', '`': 'ё', 'C': 'С', 'V': 'М', 'B': 'И', 'N': 'Т', 'M': 'Ь', '<': 'Б',
                    '>': 'Ю', '?': ',', '/': '?', 'б': '?'
                    } 
# I know there are easier ways to make such a dictionary
    response = " "
    for l in text:

        if l in PROGRAM_DICT:
            response += PROGRAM_DICT[l]
        else:
            response += l
    return response

@bot.command()
async def translate(ctx, text):
    if len(ctx.message.mentions) > 0:
    member = ctx.message.mentions[0]
    response = my_dict(text)
    await ctx.reply(response)

But the problem here is that I don't understand how to enter text into the program, because I need to take text from the message WHERE I am replying

enter image description here


Solution

  • Note: I don't really understand your problem, so, edit it as fast as possible.

    So basically you want the user to reply to a message and get the text of that message. To do this you can use ~Context.message.reference to get the message the user is replying to, something like this:

    @bot.command()
    async def translate(ctx: commands.Context, text = None) -> None:
        if text == None:
            try:
                msg = await ctx.channel.fetch_message(ctx.message.reference.message_id)
            
                text = msg.content
    
            except:
                return await ctx.reply("You must provide a text to translate or reply to a message to translate it!")
    
        response = my_dict(text)
    
        await ctx.reply(response)
    

    Anything else reply to me and I'll try to respond as soon as possible.

    Edit:

    This problem is similar to this one