Search code examples
pythondiscorddiscord.py

Python | discord.py check if channel-id = xxx to proceed


I am trying to make an if clause in discord.py to check if the channel is the one the bot should respond to.

With Hikari I managed to do it with:

async def ping(event: hikari.GuildMessageCreateEvent) -> None:
    global link
    if event.is_bot or not event.content:
        return

    if event.content.startswith("$shares"):
        if event.channel_id != 966338808075411486:
            return
        split = event.content.split()

But I can't find a working solution for discord.py, how can I replcate the if event.channel_id != 966338808075411486: using discord.py ?


Solution

  • You can do this in discord.py as so:

    @client.event
    async def on_message(message):
      if message.author == client.user:
            return
    
      if message.content.startswith("$shares"):
        if message.channel.id != 966338808075411486:
          return
    

    References:

    on_message event

    How do I check if a message was sent in a specific channel via. channel ID discord.py?