-2

I want to fetch messages from one private discord channel to my channel. But not all messages. Just my wanted ones. Like "car" message. I want to fetch all "car" messages to my discord channel. I don't know programming at all. I'm working on it for 2 days :D. It's so easy I think. But i couldn't do it.

@bot.event
async def on_message(message):
    channel = bot.get_channel(931570478915657790)
    if message.content == "car":
        await channel.send("i found car word!")

I just did this :/

StormaS
  • 33
  • 3
  • You're likely going to need to read through ALL the history. You can't search the way the client does (which is a lot faster). Are you sure you *really* need to do that? – Eric Jin Jul 12 '22 at 18:52

3 Answers3

0

you are using the wrong callback, that callback, '''on_message''' waits until a message is typed to execute. So you kind of have it, but you need to use a different callback.

@bot.command()
async def phrase(ctx, days: int = None):
    if days:
        after_date = dt.datetime.utcnow()-dt.timedelta(days=days)
        # limit can be changed to None but that this would make it a slow operation.
        messages = await ctx.channel.history(limit=10, oldest_first=True, after=after_date).flatten()
        print(messages)
    else:
        await ctx.send("please enter the number of days wanted")

taken from Discord.py: How to go through channel history and count the occurrences of a phrase? ... You then use the messages object to loop over and count the occurrence of desired word

Noah
  • 154
  • 11
  • I don't want the channel history. When someone write "car" message at the moment, I want to get notification. Thats why I use on_message. – StormaS Jul 10 '22 at 22:40
  • what does printing the channel variable return you – Noah Jul 10 '22 at 22:54
  • I'm playing a game called growtopia. In this game people buy cheap items and sell expensive. Like trade. There are lots of discord servers, people sell item here. When I find a cheap item, I just want to get notification :D – StormaS Jul 11 '22 at 10:08
0

Make sure the channel ID is correct. Also, add an if-statement checking that the message author is not the bot itself. Try this out:

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    channel = bot.get_channel(931570478915657790)
    if "car" in message.content:
        await channel.send("i found car word!")
Aditya Tomar
  • 1,601
  • 2
  • 6
  • 25
0

Here is the preferred code :

@bot.event
async def on_message(message):
channel = bot.get_channel(931570478915657790)
if message.author == bot.user:
    return
if 'car' or 'Car' in message.content:
    await channel.send('i found a car word!')
  1. Implement a check to see that the word is not in a message from the bot.
  2. Check if the required word is present in all of the message content with the if 'car' in message.content:
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saad-
  • 52
  • 6