[PYTHON] I tried to operate Linux with Discord Bot

at first

This article was written with the hope that I could easily share a Discord Bot made in Python. I won't go into too much detail, but I hope it helps someone.

What you can do with this bot

You can operate Linux from Discord You can use basically anything, such as the "ls" command or the "less" command. However, you cannot use anything that the user inputs additionally, such as vim or sudo (it seems possible if you add code). Discord has a 2,000 character limit so you can't send anything more. Also, since error processing is not done, the bot will fly when trying to open vim etc.

Execution environment

Debian 10 Python 3.7.3


import subprocess

#Installed discord.Load py
import discord

#Replace with your bot's access token
TOKEN = 'Bot token'

#Generate the objects needed for the connection
client = discord.Client()

#Processing that operates at startup
async def on_ready():
    #When started, a login notification will be displayed in the terminal
    print('You are now logged')

async def job(message):
    await message.channel.send('Let's go to bed soon!')

#Processing that operates when receiving a message
async def on_message(message):
    #Ignore if the message sender is a bot
    if message.author.bot:
    # 「/Processing that returns "Nyan" when you say "neko"

    if message.content == '/neko':
        await message.channel.send('Nyan')

    if message.content == '/konn':
        await message.channel.send("Hello")

    if message.author.id ==Discord user ID:
        messStr = str(message.content)#Get user's message
        messList = messStr.split(" ")#Arrange user messages separated by spaces

        res = subprocess.check_output(messList)#Execute that command and assign the output to res
        res = res.decode("utf-8")#res to utf-Decode with 8
        await message.channel.send(res)#Send message
        await message.channel.send("Not authorized")


About user ID

You can check the user ID that came out on the way on Discord. The ID is used to determine if the user can execute the command.

How to display ID

Turn on User Preferences → Themes → Developer Mode You can then right-click on the Discord user and browse the ID from Copy.

Recommended Posts

I tried to operate Linux with Discord Bot
I tried to save the data with discord
I tried to reintroduce Linux
How to operate Discord API with Python (bot registration)
I tried to combine Discord Bot and face recognition-for LT-
I tried to implement Autoencoder with TensorFlow
I tried to get started with Hy
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
I tried to predict next year with AI
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
I tried to detect motion quickly with OpenCV
I tried to integrate with Keras in TFv1.1
I tried to get CloudWatch data with Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to output LLVM IR with Python
I tried to debug.
I tried to detect an object with M2Det!
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I tried to paste
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
I tried to make a translation BOT that works on Discord using googletrans
I tried to learn the sin function with chainer
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to automatically read and save with VOICEROID2
I tried to get started with blender python script_Part 02
I tried to generate ObjectId (primary key) with pymongo
I tried to implement an artificial perceptron with python
I tried adding system calls and scheduler to Linux
I tried to build ML Pipeline with Cloud Composer
I tried to implement time series prediction with GBDT
I tried to uncover our darkness with Chatwork API
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried to implement Grad-CAM with keras and tensorflow
I tried to make an OCR application with PySimpleGUI
I tried to implement SSD with PyTorch now (Dataset)
What I did to get started with Linux commands
I tried to interpolate Mask R-CNN with Optical Flow
I tried to step through Bayesian optimization. (With examples)
I tried to find an alternating series with tensorflow
[Introduction to AWS] I tried playing with voice-text conversion ♪
I tried to solve AOJ's number theory with Python
I tried fp-growth with python