Search code examples
pythondiscorddiscord.py

Discord Python - How to make the BOT search for a message?


Does anyone know how to make the bot look for a specific message in a specific channel on a specific server? If the bot has found it, he will do something, otherwise he would do something else.

I have this for now :

@bot.command(pass_context=True)
async def command(ctx):
    search = discord.utils.get(bot.get_message, message = 'MESSAGE', channel = bot.get_channel(id = 'CHANNEL ID'))
    if search is not None:
        await bot.say("Something")
    else:
        await bot.say("Something else")

Solution

  • Here I use logs_from to read through the messages of channel with the ids, looking for a message that contains the id of the server the command was invoked in.

    from discord import NotFound
    
    @bot.command(pass_context=True, name="command")
    async def _command(ctx):
        channel_id = "123"
        channel = bot.get_channel(channel_id)
        if not channel:
            await bot.say("Error: Could not resolve controller channel")
            return
        server_id = ctx.message.server.id
        async for message in bot.logs_from(channel, limit=500):
            if server_id in message.content:
                await bot.say("SSM")
                return
        await bot.say("SSM ELSE")