Skip to content

Instantly share code, notes, and snippets.

@astail
Last active February 6, 2024 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astail/35ac7a48868d13c4fa0c9ff68792e593 to your computer and use it in GitHub Desktop.
Save astail/35ac7a48868d13c4fa0c9ff68792e593 to your computer and use it in GitHub Desktop.
#!/usr/local/src/pyenv/shims/python3
import discord
from discord.ext import tasks
import subprocess
import json
TOKEN = 'xxxxxx'
Intents = discord.Intents.default()
Intents.members = True
client = discord.Client(intents=Intents)
def get_players():
#https://github.com/gorcon/rcon-cli/releases/download/v0.10.3/rcon-0.10.3-amd64_linux.tar.gz
command = "/home/astel/palserverbot/rcon-0.10.3-amd64_linux/rcon -a 127.0.0.1:25575 -p ppppppaaaaassswwwdd ShowPlayers"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output = result.stdout
lines = output.strip().split('\n')
players = []
for line in lines[1:]:
if line:
data = line.split(',')
players.append(data[0])
return players
@tasks.loop(minutes=3)
async def update_status():
players = get_players()
if players:
status_message = ', '.join(players)
else:
status_message = 'No players online'
await client.change_presence(activity=discord.Game(name=status_message))
@client.event
async def on_ready():
print('Startup Success!!!')
update_status.start()
@client.event
async def on_message(message):
print(message.content)
if message.author.bot:
return
if message.channel.id == 1234567890 and client.user.mentioned_in(message) and 'restart' in message.content:
await message.reply('リスタートの要求を受け付けました。')
try:
subprocess.run(['/home/astel/palserverbot/restart.sh'], check=True)
await message.reply('リスタートしました。')
except subprocess.CalledProcessError:
await message.reply('リスタートに失敗しました。管理者にメンションしてください。')
client.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment