Giter Club home page Giter Club logo

Comments (6)

motonari728 avatar motonari728 commented on May 20, 2024

I run discord bot with RoR2DS on one VM.
So I want to make it clear whether mentenance, some accidents or VM shutdown.

from pyre-bot.

motonari728 avatar motonari728 commented on May 20, 2024

I completed Server autostop feature for me.
I'm setting server waits 30min to autostop.
I notice that the server autostop before autostart.
So this feature is Unnecessary right now. Sorry.😅

By the way, I made autostop function post to discord before autostop.
May be reusable, so I past here.
Code will tell more than my bad english.

from pyre-bot.

motonari728 avatar motonari728 commented on May 20, 2024
async def server_autostop(self):
    n = 30
    print('AutoStop enabled every ' + str(n) + ' minutes')
    while True:
        # 秒単䜍で埅぀. n分
        # n分ごずにサヌバヌのプレむ人数をチェックしお、0人だったら終了する
        await asyncio.sleep(60*n)
        is_server_running = await server()
        if (not is_server_running) or server_info.player_count == 0:
            # stop RoR2DS first
            await server_stop()
            await asyncio.sleep(10)
            if os.path.exists(BepInEx / "LogOutput.log"):
                try:
                    os.remove(BepInEx / "LogOutput.log")
                except Exception:
                    print('Unable to remove log file')
            if os.path.exists(BepInEx / "LogOutput.log.offset"):
                try:
                    os.remove(BepInEx / "LogOutput.log.offset")
                except Exception:
                    print('Unable to remove offset! Chat may not work!')

            # make POST request to Azure Automation
            url = "Azure Automation Webhook(HTTP POST) Here"
            data = {}
            headers = {
                'Content-Type': 'application/json',
            }
            try:
                print("Start autostop function. Requesting Azure API")
                request = urllib.request.Request(url, json.dumps(data).encode(), headers)
                response = urllib.request.urlopen(request)
                responseCode = response.getcode()
                body = response.read()
                # json = body.decode('utf-8')
                print('Server Shutdown is requested because no one playing. ResponseCode: ' + str(responseCode) + "\n body: " + str(body))
            except Exception as e:
                print("error in requesting Azure API")
                print(e)
                print(traceback.format_exc())

            # Post result to Discord
            try: 
                print("Post to discord before auto server shutdown")
                channel = config_object.getint('RoR2', 'channel')
                channel = self.bot.get_channel(channel)
                chatContent = 'しばらく誰も䜿っおいないため、サヌバヌにシャットダりンリク゚ストを送りたす。 \nresponseCode: ' + str(responseCode) + '\n responseBody: ' + str(body)
                await channel.send(chatContent)
            except Exception as e:
                print("error in posting to discord before auto server shutdown")
                print(e)
                print(traceback.format_exc())

        elif server_info.player_count > 0:
            print('Players currently in server')

from pyre-bot.

motonari728 avatar motonari728 commented on May 20, 2024

(But this feature looks good when use with autorestart function)

from pyre-bot.

InfernalPlacebo avatar InfernalPlacebo commented on May 20, 2024

Thanks for showing the code you used to fix this! I'll look into maybe including this into the main program. Sorry I wasn't able to look into this today for you. Did that resolve your issue/can I close this?

from pyre-bot.

motonari728 avatar motonari728 commented on May 20, 2024

OK

from pyre-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.