Linux Automation Tip: Process Discord Messages and Launch Shell Commands in Python

in #making6 years ago

Run Shell Commands with a Python Discord Bot
In the previous Discord article we set up a listener, but really that was just the foundation - enough to listen for "hello" and greet the user. Now we need a real-world example.

Previously


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

Workflow


Here is our to-do list:

  1. Listen for our specific command in the stream of messages
  2. Extract the required information from the command message
  3. Act on that information
  4. Respond back to the user
We saw in the previous article that Discord sends us message objects, and from that object we can easily take three pieces of valuable information (and more):
  • The author (if only to greet them)
  • The channel (eg. General, or the user might be using a Direct Message)
  • The Message Content (the text string containing the message content)

Listening out for the messages

In our previous example we simply created the "hello world" of Discord bots, like so:
# listen for specific messages
@client.event
async def on_message(message):
    if message.content.startswith("/hello"):
        await client.send_message(message.channel, "BY YOUR COMMAND!")
If the message string starts with our command then we reply. Easy.

In the real world, however, we want to pass more complex messages, and we need some more action. We need to break the command down into components.

We can do this by "splitting" the string. Assuming the parameters are separated by spaces, we can do this easily in Python:

args = message.content.split(" ")
Args becomes a list of the pieces of message content, essentially we took each word in order and put it in its own little box, ready to take back out again.

Real, Useful Example - Bookmark Bot

An increasing part of my blogging workflow is curation. That means I am on the look out for cool links across my blog topics. I made a database and an associated Python script, but I am not always at a terminal. Enter Bookmark Bot!

This bot takes the following command:

/add-link makerhacks http://example.com
  • The add-link command
  • The blog to associate the link with
  • The link
He looks like this:

alt

Taking Action


Recall I already had a Python script that added to the bookmarks database?

Rather than duplicate that code, I can call it as a Shell command. For convenience, I wrapped it in a .SH Bash script so that it went to the correct directory and used the correct Python version command.

So what we are saying is your Discord can become a UI for your existing scripts!

Now, obviously, there are security impacts to this, but we are big boys and girls, we already understand that.

We put that split function into a function so we can pass the message and get back the arguments, just in case I want to do more later, perhaps keep a log?

The Python script is called using Subprocess, so you need an import subprocess line at the start of your code.

if message.content.startswith("/add-link"):
    args = process_message(message)
    blog = args[1]
    link = args[2]
    result = subprocess.check_output(['/home/chrisg/steemit/add-link.sh', blog, link])
    await client.send_message(message.channel, "@{} Link added: {}".format(message.author,link))
Nice, eh?

That's pretty much it - just expand the list of commands to your heart's delight :D

Check out the full code in this Gist.


 

Posted from my blog with SteemPress : https://makerhacks.com/discord-shell-commands/

Sort:  

You got a 1.97% upvote from @buildawhale courtesy of @makerhacks!
If you believe this post is spam or abuse, please report it to our Discord #abuse channel.

If you want to support our Curation Digest or our Spam & Abuse prevention efforts, please vote @themarkymark as witness.

You got a 26.67% upvote from @ipromote courtesy of @makerhacks!
If you believe this post is spam or abuse, please report it to our Discord #abuse channel.

If you want to support our Curation Digest or our Spam & Abuse prevention efforts, please vote @themarkymark as witness.

This post has received a 19.29 % upvote from @boomerang thanks to: @makerhacks

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.029
BTC 66360.61
ETH 3420.45
USDT 1.00
SBD 2.63