Repository: evennia/evennia

Issue:
[BUG - Develop] discord integration multiple connections
#### Describe the bug
I pulled the latest discord patch. it has successfully removed the traceback, but now it ends up sending each message 3 times. 

#### To Reproduce
Steps to reproduce the behavior:
1. set up discord integration, including a bot. 
2. leave your evennia game idle for a bit. 
3. watch the discord bot connect, disconnect, connect, connect, disconnect, etc.  I am now up to 4 discordbots logged in. They're multiplying.
4. See error when you then try to say something.
[Mudinfo] [2022-12-02(20:47)]: DiscordBot disconnected ()
[Mudinfo] [2022-12-02(20:47)]: DiscordBot connected
[Mudinfo] [2022-12-02(20:47)]: DiscordBot connected
--------- New Activity ---------
[Mudinfo] [2022-12-02(21:05)]: DiscordBot disconnected ()
[Mudinfo] [2022-12-02(21:05)]: DiscordBot connected
[Mudinfo] [2022-12-02(21:05)]: DiscordBot connected
[Mudinfo] [2022-12-02(21:05)]: DiscordBot disconnected ()
[Mudinfo] [2022-12-02(21:05)]: DiscordBot connected
[Public] Radiance(#1) yays. it workssssss.
on discord:
[Public] Radiance yays. it workssssss.
[Public] Radiance yays. it workssssss.
[Public] Radiance yays. it workssssss.
Amethyst — Today at 9:37 PM
facepalm.
and then back on Evennia
[Public] [#chat] Amethyst: facepalm.
[Public] [#chat] Amethyst: facepalm.
[Public] [#chat] Amethyst: facepalm.

#### Expected behavior
I think the discord bot should reconnect only as many times as it disconnects.

#### Develop-branch commit
Evennia version: 1.0-dev (rev 06be8e4b29)

#### Additional context
No clue how to fix, but thought I would raise this for someone to take a look at when there's time.



Discussion:
Portal| 2022-12-02 21:45:51 [..] Portal starting server ... 
|Portal| 2022-12-02 21:53:58 [..] Discord: received 'Reconnect' opcode. Reconnecting.
|Portal| 2022-12-02 21:53:58 [..] Attempting connection to Discord...
|Portal| 2022-12-02 21:53:58 [..] Discord disconnected: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
|Portal| 2022-12-02 21:54:04 [..] Attempting connection to Discord...
|Portal| 2022-12-02 21:54:08 [..] Discord disconnected: Discord WebSocket requesting client reconnect.
|Portal| 2022-12-02 21:54:16 [..] Attempting connection to Discord...
2022-12-02 21:05:42 [..] Logged in: DiscordBot(account 2) discord.gg (2 session(s) total)
2022-12-02 21:05:44 [..] Logged in: DiscordBot(account 2) discord.gg (3 session(s) total)
2022-12-02 21:05:51 [..] Logged out: DiscordBot(account 2) discord.gg (2 sessions(s) remaining)
2022-12-02 21:05:55 [..] Logged in: DiscordBot(account 2) discord.gg (3 session(s) total)
I'm not entirely sure what's happening here yet, but this line has me suspicious:
> Discord disconnected: Discord WebSocket requesting client reconnect.

That line sounds vaguely familiar, so I'm pretty sure I wrote it once upon a time, but it's not a default message and it's not written anywhere into the current code. Which makes me think there's something old floating around in there, still...

It's *possible* I guess that there's a "[name] requesting client reconnect" message template, but I triple-checked whether anything here was named "Discord WebSocket" and I don't see that either.

I'll spin up a test server and run it for a while in case I get that reconnection issue this time.

Resolving patch:
diff --git a/evennia/server/portal/discord.py b/evennia/server/portal/discord.py
--- a/evennia/server/portal/discord.py
+++ b/evennia/server/portal/discord.py
@@ -201,7 +201,7 @@ def clientConnectionLost(self, connector, reason):
             reason (str): The reason for the failure.
 
         """
-        if self.do_retry or not self.bot:
+        if self.do_retry and self.bot:
             self.retry(connector)
 
     def reconnect(self):


Test patch: