How to use Raspberry Pi with Discord to check if friends are online

Thumbnail How to use Raspberry Pi with Discord.py to see if someone is online

Last christmas I got an Raspberry Pi 4 with the Freenove RFID Starter Kit as an extension kit and watched some YouTube videos about some interesting Raspberry Pi projects.

There are a lot of interesting videos, but I had my own idea:
What if I could see if someone of my friends is online on our Discord channel?

The main idea was building a Discord bot which shows to Raspberry Pi number of online Members and passes it then to an LED light or a display.

In the following I will guide you through the process of installing the right packages on your Raspberry Pi and setting up the discord bot. My main source was this article from gngrninja.com.

Disclaimer:

I don’t take any responsability for damage on your Raspbarry Pi or any hardware. I just want to know you what worked for me 🙂

Add the Bot to your Disord server

1. Go to Discord Developer Console

How to use Raspberry Pi with Discord.py to see if someone is online

2. Click on “New Application”

How to use Raspberry Pi with Discord.py to see if someone is online

3. Type in a name and click “Create”

How to use Raspberry Pi with Discord.py to see if someone is online

4. Click “Bot” on the left side and then “Add Bot”

How to use Raspberry Pi with Discord.py to see if someone is online

5. Write your Bot Token down, because you’ll need it later

How to use Raspberry Pi with Discord.py to see if someone is online

7. Go to “OAuth2” in the navigation, select “bot” in scopes and “Administrator” at bot
permissions. Then you should open the link.

How to use Raspberry Pi with Discord.py to see if someone is online

8. Add the bot to your server and confirm it

Your bot should be now in your server but offline

Now we have to update your Pi

1. You need to update package lists

sudo apt-get update

2. Then you should upgrade it to

sudo apt-get upgrade

3. Now we should clean things up a bit

sudo apt-get dist-upgrade

4. Reboot your Raspberry Pi

Check which version of Raspbian your Pi is running on

cat /etc/os-release

Raspian Buster (Version 10.x) ships with Python 3.7.x and should work great with discord.py. You can click here if you want to upgrade your OS to Buster.

If your Raspberry Pi is not running on Buster or Stretch you have to install Python manually, but if not you can skip this part.

1. Install libssl-dev (to ensure we can use pip when we install the newest version of Python)

sudo apt-get install libssl-dev
How to use Raspberry Pi with Discord.py to see if someone is online

2. Install libffi-dev (to ensure audio works with the Discord bot)

sudo apt-get install libffi-dev

3. Install libsqlite3-dev (this will be handy, as it installs libraries needed to install sqlite3 support)

sudo apt-get install libsqlite3-dev

4. Grab the latest version of Python 3.x from https://www.python.org/downloads/

wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

5. Extract the files, and enter the directory

tar xzvf Python-3.6.0.tgz

6. Then run cd Python-3.6.0/ to navigate to the folder

cd Python-3.6.0/

7. Run configure to check for dependencies, and get ready to build the Python installation
(This will take 2-5 minutes)

./configure

8. Run make to start compiling the software
(This will take 15-30 minutes)

make

9. Install Python 3.6.x
(This will take 10-15 minutes)

sudo make install

10. Reboot your Pi!

Install Discord.py

You may need to install the following, if you have installed Buster a short time ago

sudo apt install python3-pip
sudo apt install python3-cffi
sudo pip3 install discord.py[voice] 

Install latest version of the Discord library for Python (Discord.Py)

sudo python3 -m pip install -U discord.py[voice]

Create the bot

1. Create a directory

mkdir ~/isOnline_bot

2. Move to that directory

cd ~/isOnline_bot

3. Create an empty file

touch bot.py

4. Edit the file

nano bot.py

5. Copy and paste the following code inside the terminal. Don’t forget to change YOUR_TOKEN to the token you copied earlier)

import discord
from discord.ext import commands
import RPi.GPIO as GPIO
import time


TOKEN = 'YOUR_TOKEN'

description = '''isOnline Bot'''
bot = commands.Bot(command_prefix='?', description=description)


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
    



    
@bot.command()
async def membercount(ctx):
    """Counts the members"""
    
    while True:
   
        member_count = len(ctx.guild.members) # includes bots
        true_member_count = len ([m for m in ctx.guild.members if not m.bot]) #doesn't include bots
        print(true_member_count)
        await ctx.send(true_member_count)
        
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(18, GPIO.OUT)
    
   
        if true_member_count >= 1:
            GPIO.output(18, True)
            print("LED ON")
    
    
        if true_member_count < 1:
            GPIO.output(18, False)
            print("LED OFF")
        

    


bot.run(TOKEN)

Build the circuit for the LED light

Run the script and test the bot

1. Run it by typing in

python3 bot.py

2. Go to your Discord Channel and type in “?membercount” and if someone is in a channel it
will glow up 🙂
Side note: Make shure you use a text channel which is empty or not used, because the
bot will spam it

3. Have some fun with this bot and be updated everytime someone is online!

Which components could you use as well?

I connected the LCD Display and the 1 digit 7 segment display correct to the Raspberry Pi, but I wasn’t able to output the variable from the bot.
I tried to use the 1 digit 7 segment display for the output of the number and the LCD display for showing the latest message someone send to a specific channel.

Maybe I’ll implement it later on 🙂

Thank you very much for reading this post and I hope you have fun with your new bot!