Linux Automation Tip: Listen for Commands with your Discord Bot using Python

in #linux7 years ago
Python Discord Bot
Python Discord Bot

A Discord bot that only SENDS messages is all well and good, but really we want our bots to interact. We need to make them listen and respond. Let's do that now.

Previously:

https://makerhacks.com/python-messages-discord/

Event Driven Computing


If you have done any visual or object-oriented programming you will be aware of what is called listening for or raising an "event".

Essentially your program keeps quietly processing until something happens, then it jumps into action.

So with this in mind, we need to tell our bot what to look for and how to respond!

Code

You will need the Discord code library before you can run any of these commands so grab it like so:

python3 -m pip install discord

(Copy and paste the code from this Gist here)

Discord Bot Python Code
Discord Bot Python Code

Running the Bot

First, you need to create your bott app, and associate a new bot with it:

Discord - Developer Site
Set up your App at the Discord Developer Site

Discord - Developer Documentation-1

Then you need to go to the permissions URL to authorize your bot to do its thing:

https://discordapp.com/oauth2/authorize?&client_id=YOUR_CLIENT_ID&scope=bot&permissions=0

Authorize access to your account
Authorize access to your account

When your script is running and the bot is authorized you should get notified in your server thusly:

alt


Posted from my blog with SteemPress : https://makerhacks.com/python-discord-listener/

Sort:  

It would be helpful if I can see typical "prompt and response examples". Thanks for the tutorial

My next article on the subject will show my exact use of it, as you can see above this was just enough to do "hello world" type test :)

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63525.38
ETH 2645.15
USDT 1.00
SBD 2.76