This is a continuation of Adcare article. I wondered if I could do something with ** RaspberryPi ** + ** discord.py **. The Raspberry Pi Blynk remote control I made earlier died due to a malfunction on the Raspberry Pi side (probably an SD card), so I thought I should take this opportunity.
Basic parts are available at Akizuki Denshi. (Soldering irons, breadboards, and jumper wires are on the premise)
First, in order to receive infrared rays, connect the receiving modules as shown in this table.
Raspberry Pi side | Module side(The receiving part is facing you) |
---|---|
GPIO for input | left |
GND | Central |
3.3V | right |
I tried Lirc for infrared exchange, but the environment in my room was bad and I could not set it well, so I use WiringPi.
This article was helpful. Scan and send infrared remote control signals using GPIO of Raspberry Pi
Maintenance is easier if you let it be executed indirectly by a shell script. Execution from the python side uses ** subprocess **.
index.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import discord
from discord.ext import commands
import subprocess
from subprocess import PIPE
# DiscordToken
TOKEN = 'Your Token'
#Generate the objects needed for the connection
client = discord.Client()
#Processing that operates at startup
@client.event
async def on_ready():
#When started, a login notification will be displayed in the terminal
print('ready...')
#Processing that operates when receiving a message
@client.event
async def on_message(message):
#Ignore if the message sender is a bot
if message.author.bot:
return
#Lighting switch
if message.content == 'on light':
#User selection
if (str)(message.author) == me_id:
#ON command
subprocess.run("[ABSOLUTE PATH]/lightOn.sh",shell=True, text=True)
await message.channel.send('I set the lighting to ON'+rasp)
else: #When a user other than yourself tries to operate
await message.channel.send(f'{message.author.mention}No command right')
#Launching a bot and connecting to a Discord server
client.run(TOKEN)
Under / etc / systemd / system /
,
discordBot.service
[Unit]
Description=Discord Bot Service
[Service]
ExecStart=Absolute path of python file
Restart=no
Type=simple
[Install]
WantedBy=multi-user.target
Save,
raspberryPi$ sudo systemctl enable discordBot.service #activation
raspberryPi$ sudo systemctl start discordBot.service #start
To execute.
The bot is now resident.
At the terminal
raspberryPi$ sudo systemctl status discordBot.service
You can check the log by executing, so let's deal with the error.
If all goes well, you can control the room lighting with the commands you specify from the Discord talk screen. I also set up an air conditioner. Also, for the bot name, I played with my favorite character name.
very good. (good)
For the time being, the source code is here (jumps to github)
It seems that discord.py has a convenient framework, so I wanted to use it, but I used it because there was a similar bot source code before.
Scan and send infrared remote control signals using GPIO of Raspberry Pi schemeit discord.py Document TV Anime The Idolmaster Official