[PYTHON] Plug commands into Minecraft server from Discord text chat

≒ Insert the command into the program running on screen

Thing you want to do

――If you are a Minecraft player, when you say, "The person you were playing with fell asleep and you can't dawn in the game," you often want to temporarily leave that person from the multi-server. I think there is --Micra can execute the process of operator authority by inputting the command directly from the server console. --Alternatively, from the server console, you can give op authority to user HOGE by typing ʻop HOGE`, and HOGE can execute op authority processing in in-game chat. --If you have op authority, you will be able to handle the kick command. ――However, it is reckless to give op authority to all participants in a world accessed by a large number of people. ――Therefore, ** I want to prepare a means that anyone can execute only a specific op command from another interface **

Status of operating Micra server

--Building 4 worlds on one server at the same time --Each world can be executed in screen, and separate ports can be opened and assigned for easy parallel operation. ――There are no exceptions for people playing on this server, and they also participate in a certain Discord server ** So ...

solution

--Let's ask Discord bot! (For Discord bot, click here](https://qiita.com/charichuma_hack/items/dfcdd422be46ffc311aa))

-You can insert a character string into the screen by doing like ↓

minecraft_scripts/kick.sh


screen -S minecraft -X stuff "kick $1\n"

--Methods to add to discord bot

main.py


# send kick signal to Minecraft
#Input to method(text) =Text message from discord channel
def send_signal_to_minecraft(text):
    response_string = ''
    if text.find("minecraft kick") > -1:
        index_st = text.find('kick ') + 5
        #index_ed = text.find('')
        user_name = text[index_st:]
        response_string = "We understand."+ user_name + "Kick"
        try:
            subprocess.call(["sh", "minecraft_scripts/kick.sh", user_name])
        except Exception as e:
            response_string = 'Syntax error > <:cold_sweat:\n' + e.message + '\n' + str(e)
    else:
        response_string = "Syntax error > <:cold_sweat:\n Enter it correctly!"

    return response_string

Recommended Posts

Plug commands into Minecraft server from Discord text chat
The story of launching a Minecraft server from Discord