Skip to content

Instantly share code, notes, and snippets.

@yude
Created January 17, 2021 11:39
Show Gist options
  • Save yude/b3a9f2ba554ead6f0a20c694722c3201 to your computer and use it in GitHub Desktop.
Save yude/b3a9f2ba554ead6f0a20c694722c3201 to your computer and use it in GitHub Desktop.
Discord のテキストチャンネルから systemd をこねくり回す
import discord
from discord.utils import get
import subprocess
import getpass
import tempfile
import os
client = discord.Client()
passwd = (getpass.getpass() + '\n').encode()
token = os.environ.get('DISCORD_TOKEN')
@client.event
async def on_ready():
print("Discord へ接続しました。")
@client.event
async def on_message(message):
if message.author.bot:
return
if message.content == 'hcu start':
await message.channel.send('Minecraft サーバーを起動します...')
res = subprocess.run(['sudo', '-S', 'systemctl', 'start', 'minecraft_hcu'], input=passwd, check=True)
print(res)
if message.content == 'hcu stop':
await message.channel.send('Minecraft サーバーを停止します...')
res = subprocess.run(['sudo', '-S', 'systemctl', 'stop', 'minecraft_hcu'], input=passwd, check=True)
print(res)
client.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment