Discord bot listen to commands on specific channel

Alex Gap

I have a bunch of commands in my discord bot and what I am trying to do is to make the bot listen to some commands ONLY if they come from a specific channel.

Here is an example of a command:

@bot.command(name='bitcoin',
                brief="Shows bitcoin price for nerds.")
async def bitcoin(pass_context=True):
    url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
    response = requests.get(url)
    value = response.json()['bpi']['USD']['rate']
    await bot.send_message(discord.Object(id='<channel id is inserted here>'), "Bitcoin price is: " + value)
    # await bot.say("Bitcoin price is: " + value)

I can give the answer in a specific channel that I want, but I want the bot to only reply if the command is triggered in a specific channel, not everywhere.

I tried a if/else with if message.channel.id = 'id' but it doesn't work.

Patrick Haugh

You can write a check that you can use to decorate your command. Below we create a check using our target channel id, and then decorate our command with that check.

def in_channel(channel_id)
    def predicate(ctx):
        return ctx.message.channel.id == channel_id
    return commands.check(predicate)

@bot.command(name='bitcoin', brief="Shows bitcoin price for nerds.")
@is_channel('CHANNEL_ID')
async def bitcoin(pass_context=True):
    url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
    response = requests.get(url)
    value = response.json()['bpi']['USD']['rate']
    await bot.send_message(discord.Object(id='<channel id is inserted here>'), "Bitcoin price is: " + value)
    # await bot.say("Bitcoin price is: " + value)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Discord bot create a "channel" variable

From Dev

How do i delete the previous messages that my discord bot sent in a specific channel?

From Dev

Discord.js Voice Channel with Slash Commands

From Dev

Discord.py bot leaving voice channel

From Dev

Discord .net Bot create a private channel

From Dev

Discord Bot to DM specific roles

From Dev

Discord js / Check if user is in an specific voice Channel

From Dev

I need a django-celery daemon to listen to specific rabbitmq channel

From Dev

Discord bot can't connected to voice channel (python)

From Dev

Discord.js - Bot won't specify/mention the channel in logs

From Dev

discord.errors.ClientException: Already connected to a voice channel music bot

From Dev

Discord Py bot spams channel when looping through a dictionary

From Dev

Discord Bot shall delete "user pinned message to channel" message

From Dev

discord music bot error, ClientException: Already connected to a voice channel in this server

From Dev

Python discord bot warn specific person

From Dev

How do I send a message to a specific channel in discord.js?

From Dev

Check if a user can send message in a specific channel discord.js

From Java

My Discord-Bot stopped executing its commands

From Dev

Using Bot commands with client events - discord.py

From Dev

NodeJS discord bot commands getting error Cannot send an empty message

From Dev

How to make your bot visible to specific users in a channel

From Dev

How do I move a user to a specific channel on discord using the discord.py API

From Dev

Discord.js with discord.js-commando Prevent command from running in specific channel

From Dev

Programming a Discord bot in Python- How do I make the bot restrict commands to a certain server?

From Java

discord.py (rewrite) How can I narrow a commands use ability to a single channel?

From Dev

how can i make my discord.py bot send a message i choose to a channel i choose?

From Dev

How can I make my Discord Bot delete all messages in a channel?

From Dev

Discord.js bot joining voice channel but wont run the remaining code after joining

From Dev

How can I make my Discord Bot delete all messages in a channel?

Related Related

  1. 1

    Discord bot create a "channel" variable

  2. 2

    How do i delete the previous messages that my discord bot sent in a specific channel?

  3. 3

    Discord.js Voice Channel with Slash Commands

  4. 4

    Discord.py bot leaving voice channel

  5. 5

    Discord .net Bot create a private channel

  6. 6

    Discord Bot to DM specific roles

  7. 7

    Discord js / Check if user is in an specific voice Channel

  8. 8

    I need a django-celery daemon to listen to specific rabbitmq channel

  9. 9

    Discord bot can't connected to voice channel (python)

  10. 10

    Discord.js - Bot won't specify/mention the channel in logs

  11. 11

    discord.errors.ClientException: Already connected to a voice channel music bot

  12. 12

    Discord Py bot spams channel when looping through a dictionary

  13. 13

    Discord Bot shall delete "user pinned message to channel" message

  14. 14

    discord music bot error, ClientException: Already connected to a voice channel in this server

  15. 15

    Python discord bot warn specific person

  16. 16

    How do I send a message to a specific channel in discord.js?

  17. 17

    Check if a user can send message in a specific channel discord.js

  18. 18

    My Discord-Bot stopped executing its commands

  19. 19

    Using Bot commands with client events - discord.py

  20. 20

    NodeJS discord bot commands getting error Cannot send an empty message

  21. 21

    How to make your bot visible to specific users in a channel

  22. 22

    How do I move a user to a specific channel on discord using the discord.py API

  23. 23

    Discord.js with discord.js-commando Prevent command from running in specific channel

  24. 24

    Programming a Discord bot in Python- How do I make the bot restrict commands to a certain server?

  25. 25

    discord.py (rewrite) How can I narrow a commands use ability to a single channel?

  26. 26

    how can i make my discord.py bot send a message i choose to a channel i choose?

  27. 27

    How can I make my Discord Bot delete all messages in a channel?

  28. 28

    Discord.js bot joining voice channel but wont run the remaining code after joining

  29. 29

    How can I make my Discord Bot delete all messages in a channel?

HotTag

Archive