Home > Net >  is there a way to make discord.py copy a certaint part of the text sent by users
is there a way to make discord.py copy a certaint part of the text sent by users

Time:08-15

is there any way for discord.py bot to copy a certain par of the messege, fore example:

the user sends "print('hello')" in the chat, the bot responds whit the text in the brackets, in this case "hello"

CodePudding user response:

Use regex

import re
from discord.ext import commands

bot = commands.Bot()

@bot.event
async def on_message(message):
    content = message.content # in this case: print('hello')
     
    # any character inside of '' (0 or more)
    result = re.search(r'\'[^"]*\'', content).group(0)

    # remove ' around string
    result = result.strip("'")

    await message.channel.send(result)

Result

hello
  • Related