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.
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.
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
@client.event
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
@client.event
async def on_message(message):
#Ignore if the message sender is a bot
if message.author.bot:
return
# 「/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
else:
await message.channel.send("Not authorized")
client.run(TOKEN)
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.
Turn on User Preferences → Themes → Developer Mode You can then right-click on the Discord user and browse the ID from Copy.
Recommended Posts