如何使机器人接受我回复消息的文本作为输入?

huangapple go评论68阅读模式
英文:

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

问题

感谢您的帮助,现在我更了解了,祝大家好运!

我是编程的新手,对于在Discord中制作机器人完全不懂。尽管如此,我决定创建一个自己的机器人并将其添加到我的服务器中。

我试图制作一个能够翻译使用错误键盘布局的文本的Discord机器人(这是非拉丁字母国家的当前问题)。它可以正常运行,但我想添加一个功能,以便您可以回复另一个用户的消息,调用机器人,使用前缀(附图示例)。然而,我的知识有限。

换句话说,我想要做的是,当您回复用户的消息并输入命令时,命令将被执行,就好像字符串本身已经包含了对该用户的提及,即不需要写"!command @user",只需回复该用户的消息并使用命令机器人。然后,机器人会获取您回复的消息文本并进行处理。

我找到了这个选项:

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': 'Ь', ',': 'Б', '.': 'Ю',
                    '?': ',', '/': '?', 'б': '?'
                    }
    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)

但问题在于我不知道如何将文本输入程序,因为我需要从我正在回复的消息中获取文本。

英文:

> 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

如何使机器人接受我回复消息的文本作为输入?

答案1

得分: 2

注意:我不太理解你的问题,所以请尽快编辑。

所以基本上你想要用户回复一条消息并获取那条消息的文本内容。为了做到这一点,你可以使用 ~Context.message.reference 来获取用户正在回复的消息,就像这样:

@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("你必须提供要翻译的文本或回复要翻译的消息!")

    response = my_dict(text)

    await ctx.reply(response)

如果有其他问题,请回复给我,我会尽快回应。

编辑:

这个问题类似于这个问题

英文:

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](https://stackoverflow.com/questions/67100856/how-can-i-read-the-message-that-someone-replied-to-in-discord-py "Link to the similar problem")

huangapple
  • 本文由 发表于 2023年2月24日 03:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定