我有一个在 Raspberry Pi 3B+ 上以 python 运行的 Discord 机器人。该程序的目的是通过按下物理按钮将实验室状态发送到我们的服务器。我注意到程序本身并不总是想要合作,并且一直在使用 100% 的内核。我相信硬件仍然健康,唯一正在运行的就是操作系统本身。每次我检查时,htop 都会读取单个程序正在使用 98-101% 的内核。
from os import system
from datetime import date
from time import sleep
from gpiozero import LED, Button
import discord
from discord.ext import commands
import asyncio
# Pull in server token and channel ID
with open('/home/pi/EVC-Discord-Bot/token.txt', 'r') as f:
TOKEN = f.read()
with open('/home/pi/EVC-Discord-Bot/channel.txt', 'r') as f:
CHANNEL = f.read()
# Define bot properties
description = '''EVC Lab Bot'''
bot = commands.Bot(command_prefix='!', description=description)
# Define client
client = discord.Client()
# Button and LED GPIO pins
openbutton=Button(17)
pendingbutton=Button(27)
closedbutton=Button(22)
###
openlight=LED(4)
pendinglight=LED(23)
closedlight=LED(24)
# Saved Messages
openmessage=':green_circle: :radio_button: :radio_button: OPEN :green_circle: :radio_button: :radio_button:'
pendingmessage=':radio_button: :yellow_circle: :radio_button: PENDING :radio_button: :yellow_circle: :radio_button:'
closedmessage=':radio_button: :radio_button: :red_circle: CLOSED :radio_button: :radio_button: :red_circle:'
print('Program Initialized')
@bot.event
async def msg(status):
# Define desired channel
channel = bot.get_channel(int(CHANNEL))
# Terminal print channel and status
print('Bot channel: ' + str(channel))
print('Channel int: ' + str(CHANNEL))
print('Sending message...')
# Print respective status
if status == 'open':
await channel.send(openmessage)
elif status == 'pending':
await channel.send(pendingmessage)
elif status == 'closed':
await channel.send(closedmessage)
# Terminal print "completed"
print('Finished.')
@bot.event
async def on_ready():
# Tell me on_ready() def has started
print('On ready!')
# Endless while loop
while True:
# Open status
if openbutton.is_pressed:
# Show status in terminal
print('Green Button Pressed')
# Call server message function
await msg('open')
# Toggle open light
openlight.on()
# Toggle pending light
pendinglight.off()
# Toggle closed light
closedlight.off()
# Wait for active button to be released
openbutton.wait_for_release()
# Sleep for spam-safety
sleep(10)
# Pending status
elif pendingbutton.is_pressed:
# Show status in terminal
print('Yellow Button Pressed')
# Call server message function
await msg('pending')
# Toggle open light
openlight.off()
# Toggle pending light
pendinglight.on()
# Toggle closed light
closedlight.off()
# Wait for active button to be released
pendingbutton.wait_for_release()
# Sleep for spam-safety
sleep(10)
# Closed status
elif closedbutton.is_pressed:
# Show status in terminal
print('Red Button Pressed')
# Call server message function
await msg('closed')
# Toggle open light
openlight.off()
# Toggle pending light
pendinglight.off()
# Toggle closed light
closedlight.on()
# Wait for active button to be released
closedbutton.wait_for_release()
# Sleep for spam-safety
sleep(10)
# Start bot
bot.run(TOKEN)
# Tell me the bot booted
print('Bot is booted')