DenizenScript / dDiscordBot

A Discord bot for Denizen!
MIT License
10 stars 14 forks source link

Disconnecting does not clear event handlers #26

Closed michaelowens closed 4 years ago

michaelowens commented 4 years ago

If I disconnect & connect several times, it will fire the on discord message received event the same amount of times as I have (re-)connected.

Short proof of concept:


Discord_Command:
  type: command
  name: discord
  script:
    - define cmd <context.args.get[1]>
    - if <def[cmd]> == "connect":
      - ~discord id:bot connect code:<s@Discord_Config.yaml_key[code]>
    - if <def[cmd]> == "disconnect":
      - ~discord id:bot disconnect

Discord_Events:
  type: world
  events:
    on discord message received:
      - announce format:Discord_Chat "<context.no_mention_message>"
mcmonkey4eva commented 4 years ago

That's not a lack of unregistering event handlers, that's a failure to Disconnect. Your bot is just not disconnecting for some reason. Check console logs, there's likely related output.

michaelowens commented 4 years ago

Seems like it can't find the bot and thus not disconnect at all:

[13:22:23] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_RecyclingPubContinent' Executing: (line 13) discord "id:bot" "disconnect" ---------+
[13:22:23] [Server thread/INFO]: +> Executing 'DISCORD': id='bot'  instruction='disconnect'
[13:22:23] [Server thread/INFO]:  ERROR in script 'DISCORD_COMMAND' in queue 'DISCORD_COMMAND_RecyclingPubContinent' while executing command 'discord' in file
                   '\scripts\discord.dsc' on line '13' with player 'p@b75b147b-d45c-4eff-88a6-74efe108bf48 (xikeon)'!
     Error Message: Failed to process Discord disconnect command: unknown ID!
[13:22:23] [Server thread/INFO]:  Forcing queue DISCORD_COMMAND_RecyclingPubContinent into a timed queue...
[13:22:23] [Server thread/INFO]:  Completing queue 'DISCORD_COMMAND_RecyclingPubContinent' in 3ms.
michaelowens commented 4 years ago

Apparently that only happens after I have tried to disconnect before and reconnected. Upon restarting the server, connecting & disconnecting the first time:

[13:33:28] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_RadarEveReceiving' Executing: (line 11) discord "id:bot" "connect"
                   "code:<s@Discord_Config.yaml_key[code]>" ---------+
[13:33:28] [Server thread/INFO]:  Filled tag <s@discord_config.yaml_key[code]> with 'woops:)redacted'.
[13:33:28] [Server thread/INFO]: +> Executing 'DISCORD': id='bot'  instruction='connect'
[13:33:29] [Server thread/INFO]:  Forcing queue DISCORD_COMMAND_RadarEveReceiving into a timed queue...
[13:33:29] [Server thread/INFO]:  Completing queue 'DISCORD_COMMAND_RadarEveReceiving' in 62ms.
[13:33:29] [Server thread/INFO]:  Starting TimedQueue 'DISCORD_COMMAND_RadarEveReceiving' with player 'xikeon'...
[13:33:29] [Thread-9/WARN]: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
[13:33:29] [Thread-9/INFO]: [ INFO] (
[13:33:29] [Thread-9/INFO]: Thread-9
[13:33:29] [Thread-9/INFO]: )
[13:33:29] [Thread-9/INFO]: Shard 0 with Discord4J 3.0.4 (https://discord4j.com)
[13:33:29] [Thread-9/INFO]:

[13:33:31] [reactor-http-nio-8/INFO]: [ INFO] (
[13:33:31] [reactor-http-nio-8/INFO]: reactor-http-nio-8
[13:33:31] [reactor-http-nio-8/INFO]: )
[13:33:31] [reactor-http-nio-8/INFO]: Connected to Gateway
[13:33:31] [reactor-http-nio-8/INFO]:

[13:33:36] [Server thread/INFO]: xikeon issued server command: /discord disconnect
[13:33:36] [Server thread/INFO]:  Starting InstantQueue 'DISCORD_COMMAND_NelsonFearMeals' with player 'xikeon'...
[13:33:36] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_NelsonFearMeals' Executing: (line 9) DEFINE "cmd" "<context.args.get[1]>" ---------+
[13:33:36] [Server thread/INFO]:  Filled tag <context.args.get[1]> with 'disconnect'.
[13:33:36] [Server thread/INFO]: +> Executing 'DEFINE': queue='DISCORD_COMMAND_NelsonFearMeals'  definition='cmd'  value='disconnect'
[13:33:36] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_NelsonFearMeals' Executing: (line 10) IF "<def[cmd]>" "==" "connect" ---------+
[13:33:36] [Server thread/INFO]: +> Executing 'IF': use_braces='true'
[13:33:36] [Server thread/INFO]:  Filled tag <def[cmd]> with 'disconnect'.
[13:33:36] [Server thread/INFO]:  Comparable='Element(disconnect)', Operator='EQUALS', ComparedTo='Element(connect)' --> OUTCOME='false'
[13:33:36] [Server thread/INFO]:  No part of the if command passed, no block will run.
[13:33:36] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_NelsonFearMeals' Executing: (line 12) IF "<def[cmd]>" "==" "disconnect" ---------+
[13:33:36] [Server thread/INFO]: +> Executing 'IF': use_braces='true'
[13:33:36] [Server thread/INFO]:  Filled tag <def[cmd]> with 'disconnect'.
[13:33:36] [Server thread/INFO]:  Comparable='Element(disconnect)', Operator='EQUALS', ComparedTo='Element(disconnect)' --> OUTCOME='true'
[13:33:36] [Server thread/INFO]:  If command passed, running block.
[13:33:36] [Server thread/INFO]: +- Queue 'DISCORD_COMMAND_NelsonFearMeals' Executing: (line 13) discord "id:bot" "disconnect" ---------+
[13:33:36] [Server thread/INFO]: +> Executing 'DISCORD': id='bot'  instruction='disconnect'
[13:33:36] [Server thread/INFO]:  Forcing queue DISCORD_COMMAND_NelsonFearMeals into a timed queue...
[13:33:36] [Server thread/INFO]:  Completing queue 'DISCORD_COMMAND_NelsonFearMeals' in 6ms.
[13:33:50] [Server thread/INFO]:  Running script event 'DiscordMessageReceived', event='discord message received' for script 'DISCORD_EVENTS'
[13:33:50] [Server thread/INFO]:  Starting InstantQueue 'DISCORD_EVENTS_ZdnetFreshFilme'...
[13:33:50] [Server thread/INFO]: +- Queue 'DISCORD_EVENTS_ZdnetFreshFilme' Executing: (line 19) ANNOUNCE "<context.no_mention_message>" ---------+
[13:33:50] [Server thread/INFO]:  Filled tag <context.no_mention_message> with 'test message from discord'.
[13:33:50] [Server thread/INFO]: +> Executing 'ANNOUNCE': Message='test message from discord'  Type='ALL'
[13:33:50] [Server thread/INFO]:  Completing queue 'DISCORD_EVENTS_ZdnetFreshFilme' in 17ms.

In this log it doesn't give an error when disconnecting, but it still receives the event after.

mcmonkey4eva commented 4 years ago

After looking at both your script (which has a bad ~ usage)* and the underlying code, one of the two above commits should fix it.

* From the command description: The command may be ~waited for, but only for 'connect' and 'message' options. No other arguments should be ~waited for.

michaelowens commented 4 years ago

I must have read over that, that's embarrassing. Thanks for the fixes though!