Building the SteemMakers Welcome Bot

in #python8 years ago

Screenshot from 2018-06-14 08-54-52.png

SteemMakers is a group of DIYers, crafters and makers, who have come together to build a community and website around their passions. One of the key elements of that community that we enjoy is the Discord.

I've been working on the Welcome-Bot, and now it is mostly working, I thought I would explain how it works in the first beta version :)

Screenshot from 2018-06-14 08-57-29.png

For more details on how to set up, install and build a Discord bot, check out my series of articles here

Goal of the Welcome Bot

As well as simply saying hello, the bot has a mission to deliver orientation and onboarding information to new members.

This information helps the member understand some background, what to expect, and how to navigate, preventing embarrassment and ensuring everyone has the same expectations.

welcome.png

Code

Get the full code in this Gist

I had some trouble initially because while the test listeners worked fine, the welcome message wasn't being sent.

Turns out some of the examples on the web must have worked originally but no longer are correct, misleading me down some rabbit holes. In the end I came up with a different solution which as you see from the screen grab, is currently working.

The script runs on my Digital Ocean droplet currently, so it can run 24.7, but initially I was running manually so I could see the debug info in console.

You start by importing the discord API module, and creating your client.

import os
import discord

# discord client
client = discord.Client()

I keep the credentials secure, you might hard code them or keep in a configuration file.

# run the bot
bot = os.environ['SMBOT']
client.run(bot)

When the bot is ready, he will announce his status.

That is triggered by the on_ready event.


# create a new event
@client.event
async def on_ready():
    print()
    print("STEEMMAKERS ARE GO!")
    print("(Bot Ready)")
    servers = "\n   - ".join([s.name + " (" + s.id + ")" for s in client.servers])
    print(servers)
    channel = client.get_channel("383256479056134146")
    await client.send_message(channel, "Beep Boop Blurp! [Bot Ready]")

We use General channel for things like this currently. For the send_message we pass a channel object acquired with the get_channel search by channel ID.

That approach of sending to the channel is what helped me finally get this guy working. Now we can see when someone joins and send a message:

# welcome message
@client.event
async def on_member_join(member):
    print("** " + member.name + " joined")

    welcome_message = member.name + """ - Welcome! :)

    Read this helpful introduction to get acquainted with the group:
    https://hackmd.io/s/Bk5sNz_xm

    """
    channel = client.get_channel("383256479056134146")
    await client.send_message(channel, welcome_message)

Couple of items of note:

  • The member is passed to the function, so we can use their name in the welcome
  • I use """ to pass a multi-line string, making formatting the welcome easier
  • Right now we are sharing the URL of a welcome landing page rather than put all the info in the message

We can do other things when people join, but for now just the welcome message is a good start.

Here's the full code of the first beta version :)

import os
import discord

# discord client
client = discord.Client()


def process_message(message):
    args = message.content.split(" ")

    return args


# create a new event
@client.event
async def on_ready():
    print()
    print("STEEMMAKERS ARE GO!")
    print("(Bot Ready)")
    servers = "\n   - ".join([s.name + " (" + s.id + ")" for s in client.servers])
    print(servers)
    channel = client.get_channel("383256479056134146")
    await client.send_message(channel, "Beep Boop Blurp! [Bot Ready]")

# 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 message.content.startswith("/check-check"):
        await client.send_message(message.channel, ":+1: Channel = {}".format(message.channel))
        print(message.channel.id)


# welcome message
@client.event
async def on_member_join(member):
    print("** " + member.name + " joined")

    welcome_message = member.name + """ - Welcome! :)

    Read this helpful introduction to get acquainted with the group:
    https://hackmd.io/s/Bk5sNz_xm

    """
    channel = client.get_channel("383256479056134146")
    await client.send_message(channel, welcome_message)


# run the bot
bot = os.environ['SMBOT']
client.run(bot)

Getting the script running

As the Discord owner, @jefpatat had to give the bot permissions to run via the oAuth. It's not enough just to have the script running.

Screenshot from 2018-06-14 09-27-01.png

I then added a .sh shell script that supplies the environment variable, and ran it as a test a few times.

Now it runs on my server using the processes outlined here


makerhacks.png

Sort:  

Thank you very much!

Very well done man!! Keep it up :) and thanks for sharing it!

Our own bot.... We're gonna spoil it rotten ! (Though how you spoil a bot is up for debate XD)

The good oil?

This is just awesome! It was interesting to see the bot in action in the discord group!

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.082
BTC 60785.45
ETH 1557.47
USDT 1.00
SBD 0.47