D
D
dedxded2021-10-05 18:18:20
Python
dedxded, 2021-10-05 18:18:20

How to forward messages in Discord by selecting from a list of words?

I set out with the idea of ​​sending messages from channel to channel based on a selection of words, as is done for filtering on a mat.
Created a database of certain words for which selection is needed.

For example, I need all sentences with the word "Friday", "Friday", "Friday" so as not to miss them. A person in any of the channels wrote a sentence:
"The weather will be great on Friday."
The bot takes this message and sends it to a separate channel, not a PM, with a mention of the author.

Everything seemed to work out, BUT, the bot began to loop its own message. Since he was forwarding it, it again had the word "Friday" in it, and he began to forward himself.

Is it possible to somehow limit the bot, from which SPECIFIC channel to take messages for analysis and to which Specific channel to send?

Or is it easier to leave the analysis from any channel, but forwarding to the LAN? (If yes, how)

Thank you in advance!

Here is the event code:

@bot.event
async def on_message(message):
  if {i.lower().translate(str.maketrans('','', string.punctuation)) for i in message.content.split(' ')}\
  .intersection(set(json.load(open('slyhi.json')))) != set():
        channel = bot.get_channel(894897187593551892)
        await channel.send(f'[Выхваченное Сообщение]:~/// {message.author.mention}: {message.content}') 
        
        await bot.process_commands( message )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
retUrn3d, 2021-10-06
@dedxded

To solve the problem with the constant forwarding of the same message - forbid the bot to listen to itself.

@bot.event
async def on_message(message):
    if message.author != bot.user:  # Если автор сообщения НЕ бот.
        if {i.lower().translate(str.maketrans('', '', string.punctuation)) for i in message.content.split(' ')} \
                .intersection(set(json.load(open('slyhi.json')))) != set():
            channel = bot.get_channel(894897187593551892)
            await channel.send(f'[Выхваченное Сообщение]:~/// {message.author.mention}: {message.content}')

    await bot.process_commands(message)

If there is a desire to listen to some specific channel, just check if the channel to which the message was sent matches the channels from the list.
channels = [1231234234, 1231231234]
if message.channel.id in channels:
    do_something()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question