commit bc2cd49bdfac4694acfad3693a7b05b621eedd13 Author: Salastil Date: Tue Oct 14 23:09:17 2025 -0400 Initial Commit diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ecc62f0 --- /dev/null +++ b/.env.example @@ -0,0 +1,14 @@ +#Copy or rename this example file to .env +DISCORD_BOT_TOKEN=your_discord_bot_token_here +DISCORD_CHANNEL_ID=your_discord_channel_id_here +DISCORD_GUILD_ID=your_discord_guild_id_here +DISCORD_PING_USER_ID=your_discord_user_id_here # Get this by right-clicking your Discord profile +DISCORD_WEBHOOK_URL=your_discord_webhook_url_here +RECONNECT_INTERVAL=5 # Interval between reconnect attempts if connection is lost +SNEEDCHAT_ROOM_ID=1 # Which room will be bridged, append integer at the end of room name. Current options: general.1, beauty-parlor.18, gunt.8, keno-kasino.15, sports.19, fishtank.16 +ENABLE_FILE_LOGGING=false # Enable logging to bridge.log file (true/false, default: false) + +# Optional: Prevent echo loops by filtering messages from the bridge bot +# BRIDGE_USER_ID=123456 # Numeric user ID of your bridge bot account on Sneedchat +# BRIDGE_USERNAME=YourBridgeBot # Username of your bridge bot account on Sneedchat +# BRIDGE_PASSWORD=Password # Password of your account \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b3ff994 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.env +bridge.log +chattest.py +chat.log +.env.fishtank +.env.general +newchat.log +newestchat.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..aed3bbd --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +# Sneedchat-Discord-Puppet-Bridge +A python program to bridge a Kiwifarms Sneedchat channel to Discord via webhooks by puppeting a user account. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fa75990 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +aiohttp>=3.8.5 +websockets>=11.0.3 +discord.py>=2.3.2 +python-dotenv>=1.0.1 +beautifulsoup4>=4.12.0 \ No newline at end of file diff --git a/sneedchatdiscordbridge.py b/sneedchatdiscordbridge.py new file mode 100755 index 0000000..12ac1c9 --- /dev/null +++ b/sneedchatdiscordbridge.py @@ -0,0 +1,1485 @@ +#!/usr/bin/env python3 +import argparse +import asyncio +import aiohttp +import json +import html +import hashlib +import logging +import os +import random +import re +import time +from collections import deque, OrderedDict +from typing import Optional, Dict, Any, List + +import websockets +from bs4 import BeautifulSoup +import discord +from discord.ext import commands +from dotenv import load_dotenv + +# ----------------------------- +# Constants +# ----------------------------- +PROCESSED_CACHE_SIZE = 250 # sliding cache for processed sneed ids +OUTBOUND_MATCH_WINDOW = 60 # seconds to match Sneed echo with outbound Discord message +COOKIE_REFRESH_INTERVAL = 4 * 60 * 60 # 4 hours +OUTAGE_UPDATE_INTERVAL = 10 # outage embed update interval in seconds +QUEUED_MESSAGE_TTL = 90 # seconds before queued message is abandoned +MAX_ATTACHMENTS = 4 # refuse > 4 attachments +LITTERBOX_TTL = "72h" # 72 hours + +# Memory management constants +MAPPING_CACHE_SIZE = 1000 # Max message ID mappings to keep +MAPPING_CLEANUP_INTERVAL = 300 # Cleanup every 5 minutes +MAPPING_MAX_AGE = 3600 # Mappings older than 1 hour are eligible for removal + +# Outage tracking constants +OUTAGE_CLEANUP_DELAY = 120 # Delete outage message 2 minutes after reconnect +OUTAGE_INSTABILITY_WINDOW = 600 # 10 minute window for tracking outages +OUTAGE_INSTABILITY_THRESHOLD = 5 # 5+ outages = unstable + +# CLI / env +# ----------------------------- +parser = argparse.ArgumentParser(description="Sneedchat ↔ Discord Bridge") +parser.add_argument("--debug", action="store_true", help="Enable debug logging") +parser.add_argument("--env", type=str, default=".env", help="Path to .env file (default: .env)") +args = parser.parse_args() +load_dotenv(args.env) + +# ----------------------------- +# Logging +# ----------------------------- +handlers = [logging.StreamHandler()] +if os.getenv("ENABLE_FILE_LOGGING", "false").lower() == "true": + handlers.append(logging.FileHandler("bridge.log")) +logging.basicConfig( + level=logging.DEBUG if args.debug else logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + handlers=handlers +) +logger = logging.getLogger(__name__) + +if args.debug: + logger.debug("Debug mode enabled") + logging.getLogger("websockets.client").setLevel(logging.INFO) + logging.getLogger("websockets.protocol").setLevel(logging.INFO) + +# ----------------------------- +# Required env vars (match existing names) +# ----------------------------- +REQUIRED_ENV_VARS = [ + "DISCORD_BOT_TOKEN", + "DISCORD_CHANNEL_ID", + "DISCORD_GUILD_ID", + "DISCORD_WEBHOOK_URL", + "SNEEDCHAT_ROOM_ID", + "BRIDGE_USERNAME", + "BRIDGE_PASSWORD" +] +for v in REQUIRED_ENV_VARS: + if not os.getenv(v): + raise ValueError(f"Required environment variable {v} is not set") + +DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN") +DISCORD_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) +DISCORD_GUILD_ID = int(os.getenv("DISCORD_GUILD_ID")) +DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL") +SNEEDCHAT_ROOM_ID = int(os.getenv("SNEEDCHAT_ROOM_ID")) + +BRIDGE_USERNAME = os.getenv("BRIDGE_USERNAME") +BRIDGE_PASSWORD = os.getenv("BRIDGE_PASSWORD") + +BRIDGE_USER_ID = os.getenv("BRIDGE_USER_ID") +if BRIDGE_USER_ID: + BRIDGE_USER_ID = int(BRIDGE_USER_ID) + +DISCORD_PING_USER_ID = os.getenv("DISCORD_PING_USER_ID") +if DISCORD_PING_USER_ID: + try: + DISCORD_PING_USER_ID = int(DISCORD_PING_USER_ID) + except: + DISCORD_PING_USER_ID = None + +RECONNECT_INTERVAL = int(os.getenv("RECONNECT_INTERVAL", 7)) +ENABLE_FILE_LOGGING = os.getenv("ENABLE_FILE_LOGGING", "false").lower() == "true" + +logger.info(f"Using .env file: {args.env}") +logger.info(f"Using Sneedchat room ID: {SNEEDCHAT_ROOM_ID}") +logger.info(f"Bridge username: {BRIDGE_USERNAME}") +if BRIDGE_USER_ID: + logger.info(f"Bridge user filtering enabled - ID: {BRIDGE_USER_ID}") +logger.info(f"File logging: {'enabled' if ENABLE_FILE_LOGGING else 'disabled'}") + +# ----------------------------- +# BBCode -> Markdown parser +# ----------------------------- +def bbcode_to_markdown(text: str) -> str: + if not text: + return "" + # Normalize CRLF + text = text.replace("\r\n", "\n").replace("\r", "\n") + + # Basic replacements (case-insensitive, DOTALL) + text = re.sub(r'\[img\](.*?)\[/img\]', r'\1', text, flags=re.IGNORECASE | re.DOTALL) + text = re.sub(r'\[video\](.*?)\[/video\]', r'\1', text, flags=re.IGNORECASE | re.DOTALL) + + # [url=link]text[/url] -> [text](link) unless text is itself a link + def _url_replace(m): + link = m.group(1).strip() + txt = m.group(2).strip() + if re.match(r'^https?://', txt, re.IGNORECASE): + return txt + return f'[{txt}]({link})' + text = re.sub(r'\[url=(.*?)\](.*?)\[/url\]', _url_replace, text, flags=re.IGNORECASE | re.DOTALL) + + # [url]link[/url] -> link + text = re.sub(r'\[url\](.*?)\[/url\]', r'\1', text, flags=re.IGNORECASE | re.DOTALL) + + # Bold/italic/underline/strike (handle nested) + text = re.sub(r'\[(?:b|strong)\](.*?)\[/\s*(?:b|strong)\]', r'**\1**', text, flags=re.IGNORECASE | re.DOTALL) + text = re.sub(r'\[(?:i|em)\](.*?)\[/\s*(?:i|em)\]', r'*\1*', text, flags=re.IGNORECASE | re.DOTALL) + text = re.sub(r'\[(?:u)\](.*?)\[/\s*u\]', r'__\1__', text, flags=re.IGNORECASE | re.DOTALL) + text = re.sub(r'\[(?:s|strike)\](.*?)\[/\s*(?:s|strike)\]', r'~~\1~~', text, flags=re.IGNORECASE | re.DOTALL) + + # Code & code blocks + text = re.sub(r'\[code\](.*?)\[/code\]', r'`\1`', text, flags=re.IGNORECASE | re.DOTALL) + text = re.sub(r'\[(?:php|plain|code=\w+)\](.*?)\[/(?:php|plain|code)\]', r'```\1```', text, flags=re.IGNORECASE | re.DOTALL) + + # Quote blocks + def _quote(m): + inner = m.group(1).strip() + return '\n'.join('> ' + line for line in inner.splitlines()) + text = re.sub(r'\[quote\](.*?)\[/quote\]', _quote, text, flags=re.IGNORECASE | re.DOTALL) + + def _quote_attr(m): + who = m.group(1).strip() + inner = m.group(2).strip() + header = f'> **{who} said:**' + lines = '\n'.join('> ' + line for line in inner.splitlines()) + return header + '\n' + lines + text = re.sub(r'\[quote=["\']?(.*?)["\']?\](.*?)\[/quote\]', _quote_attr, text, flags=re.IGNORECASE | re.DOTALL) + + # Spoilers + text = re.sub(r'\[spoiler\](.*?)\[/spoiler\]', r'||\1||', text, flags=re.IGNORECASE | re.DOTALL) + + # Color/size - strip tags but keep content + text = re.sub(r'\[(?:color|size)=.*?\](.*?)\[/\s*(?:color|size)\]', r'\1', text, flags=re.IGNORECASE | re.DOTALL) + + # Lists & bullets + text = re.sub(r'^\[\*\]\s*', '• ', text, flags=re.MULTILINE) + text = re.sub(r'\[/?list\]', '', text, flags=re.IGNORECASE) + + # Remove unknown tags but leave content (for forgiving behavior) + # This strips any [tag] or [/tag] style constructs left + text = re.sub(r'\[/?[A-Za-z0-9\-=_]+\]', '', text) + + return text.strip() + +# ----------------------------- +# Bounded Mapping Dictionary (Memory Management) +# ----------------------------- +class BoundedMappingDict: + + def __init__(self, maxsize: int = 1000, max_age: int = 3600): + self.maxsize = maxsize + self.max_age = max_age + self.data = OrderedDict() # Maintains insertion order + self.timestamps = {} # Track when entries were added + + def __setitem__(self, key, value): + # If key exists, move to end (mark as recently used) + if key in self.data: + self.data.move_to_end(key) + else: + self.data[key] = value + self.timestamps[key] = time.time() + + # Evict oldest if over capacity + if len(self.data) > self.maxsize: + oldest_key = next(iter(self.data)) + del self.data[oldest_key] + self.timestamps.pop(oldest_key, None) + + def __getitem__(self, key): + # Mark as recently accessed + if key in self.data: + self.data.move_to_end(key) + return self.data[key] + + def get(self, key, default=None): + try: + return self[key] + except KeyError: + return default + + def pop(self, key, default=None): + self.timestamps.pop(key, None) + return self.data.pop(key, default) + + def __contains__(self, key): + return key in self.data + + def cleanup_old_entries(self) -> int: + """Remove entries older than max_age. Returns count removed.""" + now = time.time() + to_remove = [ + key for key, ts in self.timestamps.items() + if now - ts > self.max_age + ] + for key in to_remove: + self.data.pop(key, None) + self.timestamps.pop(key, None) + return len(to_remove) + + def __len__(self): + return len(self.data) + +# ----------------------------- +# Cookie Refresh Service +# ----------------------------- +class CookieRefreshService: + """Automatic cookie fetching and refresh service""" + def __init__(self, username: str, password: str, domain: str = "kiwifarms.st"): + self.username = username + self.password = password + self.domain = domain + self.current_cookie: Optional[str] = None + self.cookie_ready = asyncio.Event() + self.refresh_task: Optional[asyncio.Task] = None + self.shutdown_event = asyncio.Event() + + async def get_clearance_token(self, session: aiohttp.ClientSession) -> str: + try: + url = f"https://{self.domain}/" + async with session.get(url) as response: + html_text = await response.text() + soup = BeautifulSoup(html_text, 'html.parser') + challenge_element = soup.find('html', {'id': 'sssg'}) + if not challenge_element: + logger.debug("No KiwiFlare challenge required") + return "" + salt = challenge_element.get('data-sssg-challenge') + difficulty = int(challenge_element.get('data-sssg-difficulty', 0)) + if not salt or difficulty == 0: + return "" + logger.info(f"Solving KiwiFlare challenge (difficulty={difficulty})") + nonce = random.randint(0, 2**63 - 1) + attempts = 0 + max_attempts = 10_000_000 + while attempts < max_attempts: + nonce += 1 + attempts += 1 + input_string = f"{salt}{nonce}" + hash_result = hashlib.sha256(input_string.encode('utf-8')).digest() + required_bytes = difficulty // 8 + required_bits = difficulty % 8 + valid = True + for i in range(required_bytes): + if hash_result[i] != 0: + valid = False + break + if valid and required_bits > 0 and required_bytes < len(hash_result): + byte_val = hash_result[required_bytes] + mask = 0xFF << (8 - required_bits) + if byte_val & mask != 0: + valid = False + if valid: + submit_url = f"https://{self.domain}/.sssg/api/answer" + data = {'a': salt, 'b': str(nonce)} + async with session.post(submit_url, data=data) as submit_response: + result = await submit_response.json() + if 'auth' in result: + token = result['auth'] + session.cookie_jar.update_cookies({'sssg_clearance': token}, response_url=url) + return token + logger.warning("Failed to solve challenge within attempt limit") + return "" + except Exception as e: + logger.error(f"Clearance token error: {e}") + return "" + + async def fetch_fresh_cookie(self) -> Optional[str]: + try: + async with aiohttp.ClientSession(headers={'User-Agent': 'Mozilla/5.0'}) as session: + await self.get_clearance_token(session) + login_url = f"https://{self.domain}/login" + async with session.get(login_url) as response: + html_text = await response.text() + soup = BeautifulSoup(html_text, 'html.parser') + html_element = soup.find('html') + if not html_element: + logger.error("❌ Could not parse login page") + return None + csrf_token = html_element.get('data-csrf') + if not csrf_token: + logger.error("❌ Could not find CSRF token") + return None + login_data = { + '_xfToken': csrf_token, + 'login': self.username, + 'password': self.password, + '_xfRedirect': f'https://{self.domain}/', + 'remember': '1' + } + post_url = f"https://{self.domain}/login/login" + async with session.post(post_url, data=login_data, allow_redirects=False) as response: + auth_cookies = [] + cookie_names = ['xf_user', 'xf_toggle', 'xf_csrf', 'xf_session', 'sssg_clearance'] + for cookie in session.cookie_jar: + if cookie.key in cookie_names and cookie.value: + auth_cookies.append(f"{cookie.key}={cookie.value}") + if not auth_cookies: + logger.error("❌ Login failed: no cookies received") + return None + cookie_string = "; ".join(auth_cookies) + logger.info(f"✅ Successfully fetched fresh cookie ({len(auth_cookies)} tokens)") + return cookie_string + except Exception as e: + logger.error(f"❌ Failed to fetch fresh cookie: {e}") + return None + + async def refresh_loop(self): + try: + logger.info("🔑 Fetching initial cookie...") + fresh_cookie = await self.fetch_fresh_cookie() + if fresh_cookie: + self.current_cookie = fresh_cookie + self.cookie_ready.set() + logger.info("✅ Initial cookie acquired, bridge can start") + else: + logger.error("❌ Failed to acquire initial cookie, cannot start bridge") + return + while not self.shutdown_event.is_set(): + try: + await asyncio.wait_for(self.shutdown_event.wait(), timeout=COOKIE_REFRESH_INTERVAL) + break + except asyncio.TimeoutError: + pass + logger.info("🔄 Starting automatic cookie refresh (interval)") + fresh_cookie = await self.fetch_fresh_cookie() + if fresh_cookie: + self.current_cookie = fresh_cookie + logger.info("✅ Cookie refresh completed successfully") + else: + logger.warning("⚠️ Cookie refresh failed, keeping existing cookie") + except Exception as e: + logger.error(f"❌ Cookie refresh loop error: {e}") + + async def start(self): + logger.info("Starting cookie refresh service") + self.refresh_task = asyncio.create_task(self.refresh_loop()) + + async def wait_for_cookie(self): + await self.cookie_ready.wait() + + async def stop(self): + self.shutdown_event.set() + if self.refresh_task and not self.refresh_task.done(): + self.refresh_task.cancel() + try: + await self.refresh_task + except asyncio.CancelledError: + pass + + def get_current_cookie(self) -> Optional[str]: + return self.current_cookie + +# ----------------------------- +# SneedChatClient +# ----------------------------- +class SneedChatClient: + def __init__(self, cookie: str, room_id: int = 16, reconnect_interval: int = 7, + cookie_service: Optional[CookieRefreshService] = None): + self.ws_url = "wss://kiwifarms.st:9443/chat.ws" + self.cookie = cookie + self.cookie_service = cookie_service + self.room_id = room_id + self.ws: Optional[websockets.client.WebSocketClientProtocol] = None + self.connected = False + + self.last_message_time = time.time() + self.read_task: Optional[asyncio.Task] = None + self.write_task: Optional[asyncio.Task] = None + self.heartbeat_task: Optional[asyncio.Task] = None + self.cleanup_task: Optional[asyncio.Task] = None + self.write_queue = asyncio.Queue() + self.shutdown_event = asyncio.Event() + self.reconnect_interval = reconnect_interval + + # callbacks assigned by DiscordBridge + self.on_message = None + self.on_edit = None + self.on_delete = None + self.on_connect = None + self.on_disconnect = None + + # dedupe / edit tracking + self.processed_message_ids = deque(maxlen=PROCESSED_CACHE_SIZE) + self.message_edit_dates = BoundedMappingDict( + maxsize=MAPPING_CACHE_SIZE, + max_age=MAPPING_MAX_AGE + ) + + # reconnection attempts counter + self.reconnect_attempts = 0 + + # helper overrides (injected by DiscordBridge) + self._recent_outbound_iter = lambda: [] + self._map_discord_sneed = lambda discord_id, sneed_id, username: None + + async def cleanup_loop(self): + """Periodic cleanup of old mappings""" + try: + while not self.shutdown_event.is_set(): + await asyncio.sleep(MAPPING_CLEANUP_INTERVAL) + + # Cleanup edit dates + removed = self.message_edit_dates.cleanup_old_entries() + if removed > 0: + logger.info(f"🧹 Cleaned up {removed} old message edit tracking entries") + + except asyncio.CancelledError: + logger.debug("Cleanup task cancelled") + except Exception as e: + logger.error(f"Cleanup loop error: {e}") + + async def connect(self) -> bool: + if self.connected: + return True + if self.cookie_service: + fresh_cookie = self.cookie_service.get_current_cookie() + if fresh_cookie: + logger.debug("Using refreshed cookie for connection") + self.cookie = fresh_cookie + headers = {"Cookie": self.cookie} + try: + logger.info(f"Connecting to Sneedchat room {self.room_id} (attempting websocket)") + self.ws = await websockets.connect(self.ws_url, additional_headers=headers, ping_interval=20, ping_timeout=10) + self.connected = True + self.reconnect_attempts = 0 + self.read_task = asyncio.create_task(self.read_loop()) + self.write_task = asyncio.create_task(self.write_loop()) + self.heartbeat_task = asyncio.create_task(self.heartbeat_loop()) + self.cleanup_task = asyncio.create_task(self.cleanup_loop()) + await self.send_command(f"/join {self.room_id}") + logger.info(f"✅ Successfully connected to Sneedchat room {self.room_id}") + if self.on_connect: + await self.on_connect() + return True + except Exception as e: + logger.error(f"Sneedchat connection failed: {e}") + await self.handle_disconnect() + return False + + async def disconnect(self): + logger.info("Disconnecting from Sneedchat") + self.shutdown_event.set() + self.connected = False + for task in (self.read_task, self.write_task, self.heartbeat_task, self.cleanup_task): + if task and not task.done(): + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + if self.ws: + try: + await self.ws.close() + except Exception: + pass + self.ws = None + + async def read_loop(self): + try: + async for message in self.ws: + if self.shutdown_event.is_set(): + break + self.last_message_time = time.time() + await self.handle_message(message) + except Exception as e: + logger.error(f"Sneedchat read loop error: {e}") + finally: + if not self.shutdown_event.is_set(): + await self.handle_disconnect() + + async def write_loop(self): + try: + while not self.shutdown_event.is_set(): + try: + msg = await asyncio.wait_for(self.write_queue.get(), timeout=1.0) + if self.ws and self.connected: + logger.debug(f"➡️ Sending to Sneedchat: {msg}") + await self.ws.send(msg) + except asyncio.TimeoutError: + continue + except Exception as e: + logger.error(f"Sneedchat write loop error: {e}") + if not self.shutdown_event.is_set(): + await self.handle_disconnect() + + async def heartbeat_loop(self): + try: + while not self.shutdown_event.is_set(): + await asyncio.sleep(30) + if self.connected and time.time() - self.last_message_time > 60: + await self.send_command("/ping") + except Exception as e: + logger.error(f"Heartbeat error: {e}") + + async def handle_message(self, raw: str): + # debug full payload if requested + if args.debug: + try: + parsed = json.loads(raw) + logger.debug("=== Full Sneedchat Payload ===") + logger.debug(json.dumps(parsed, indent=2)) + except Exception: + logger.debug(f"📨 Raw non-JSON data: {raw}") + + try: + content = json.loads(raw) + except json.JSONDecodeError: + return + + # top-level deletes + if "delete" in content: + delete_field = content["delete"] + del_list = delete_field if isinstance(delete_field, list) else [delete_field] + for did in del_list: + try: + did_int = int(did) + except Exception: + continue + logger.info(f"🗑️ Received top-level Sneed delete for id={did_int}") + self.message_edit_dates.pop(did_int, None) + try: + if did_int in self.processed_message_ids: + self.processed_message_ids.remove(did_int) + except Exception: + pass + if self.on_delete: + try: + await self.on_delete(did_int) + except Exception as e: + logger.error(f"Error in on_delete callback for id={did_int}: {e}") + + messages = [] + if "messages" in content: + messages = content["messages"] + elif "message" in content: + messages = [content["message"]] + + for msg in messages: + try: + author = msg.get("author", {}) or {} + username = author.get("username", "Unknown") + user_id = author.get("id") + message_id = msg.get("message_id") + message_text = msg.get("message_raw") or msg.get("message") or "" + message_text = html.unescape(message_text) + edit_date = int(msg.get("message_edit_date", 0) or 0) + deleted_flag = msg.get("deleted") or msg.get("is_deleted") or False + + # message-scoped deletion + if deleted_flag: + logger.info(f"🗑️ Sneed message-scoped deletion id={message_id}") + if message_id: + self.message_edit_dates.pop(message_id, None) + try: + if message_id in self.processed_message_ids: + self.processed_message_ids.remove(message_id) + except Exception: + pass + if self.on_delete: + await self.on_delete(message_id) + continue + + # If message is from the bridge user: attempt mapping but do not forward + if (BRIDGE_USER_ID and user_id == BRIDGE_USER_ID) or (BRIDGE_USERNAME and username == BRIDGE_USERNAME): + logger.debug(f"🚫 Received bridge-user echo from Sneed id={message_id}; attempting mapping but not forwarding") + if message_id: + now = time.time() + matched_entry = None + for entry in reversed(list(self._recent_outbound_iter())): + if entry.get("mapped"): + continue + if entry.get("content") == message_text and (now - entry.get("ts", 0)) <= OUTBOUND_MATCH_WINDOW: + matched_entry = entry + break + if matched_entry: + discord_id = matched_entry["discord_id"] + self._map_discord_sneed(discord_id, int(message_id), username) + matched_entry["mapped"] = True + logger.debug(f"Mapped Discord->{message_id} (discord_id={discord_id}) via bridge echo") + if message_id: + self.processed_message_ids.append(message_id) + self.message_edit_dates[message_id] = edit_date + # DO NOT forward to Discord + continue + + # Dedup / edit detection + if message_id and message_id in self.processed_message_ids: + prev_edit = self.message_edit_dates.get(message_id, 0) + if edit_date and edit_date > prev_edit: + logger.info(f"✏️ Edit detected for sneed_id={message_id}") + self.message_edit_dates[message_id] = edit_date + if self.on_edit: + await self.on_edit(message_id, message_text) + else: + logger.debug(f"📄 Skipping duplicate message ID {message_id} from {username}") + continue + + # New message + logger.info(f"📄 New Sneed message from {username}: {message_text[:120]}...") + if message_id: + self.processed_message_ids.append(message_id) + self.message_edit_dates[message_id] = edit_date + + if self.on_message: + await self.on_message({ + "username": username, + "content": message_text, + "raw": msg, + "message_id": message_id, + "author_id": user_id + }) + except Exception as e: + logger.error(f"Error processing Sneed message: {e}") + + def _recent_outbound_iter(self): + return [] + + def _map_discord_sneed(self, discord_id: int, sneed_id: int, username: str): + pass + + async def send_message(self, content: str) -> bool: + """Send a plain message to sneed via websocket queue. Return True if queued for send, + False if not connected (caller should queue).""" + if not self.connected or not self.ws: + logger.warning("Cannot send to Sneedchat: not connected") + return False + await self.write_queue.put(content) + logger.debug("Queued message for Sneedchat websocket send") + return True + + async def send_command(self, command: str): + if not self.connected or not self.ws: + logger.warning("Cannot send command to Sneedchat: not connected") + return + await self.write_queue.put(command) + + async def handle_disconnect(self): + if self.shutdown_event.is_set(): + return + try: + self.reconnect_attempts = getattr(self, "reconnect_attempts", 0) + 1 + except Exception: + self.reconnect_attempts = 1 + self.connected = False + logger.warning("🔴 Sneedchat disconnected") + if self.on_disconnect: + await self.on_disconnect() + await asyncio.sleep(self.reconnect_interval) + await self.connect() + +# ----------------------------- +# Discord Bridge +# ----------------------------- +class DiscordBridge: + def __init__(self, sneed_client: SneedChatClient): + intents = discord.Intents.default() + intents.message_content = True + self.bot = commands.Bot(command_prefix="!", intents=intents) + self.sneed_client = sneed_client + + # hook callbacks + self.sneed_client.on_message = self.on_sneed_message + self.sneed_client.on_edit = self._handle_sneed_edit + self.sneed_client.on_delete = self._handle_sneed_delete + self.sneed_client.on_connect = self.on_sneed_connect + self.sneed_client.on_disconnect = self.on_sneed_disconnect + + # provide sneed client with mapping helpers + self.sneed_client._recent_outbound_iter = self._recent_outbound_iter + self.sneed_client._map_discord_sneed = self._map_discord_sneed + + self.session: Optional[aiohttp.ClientSession] = None + + # mapping tables (now with memory management) + self.sneed_to_discord = BoundedMappingDict( + maxsize=MAPPING_CACHE_SIZE, + max_age=MAPPING_MAX_AGE + ) + self.discord_to_sneed = BoundedMappingDict( + maxsize=MAPPING_CACHE_SIZE, + max_age=MAPPING_MAX_AGE + ) + self.sneed_usernames = BoundedMappingDict( + maxsize=MAPPING_CACHE_SIZE, + max_age=MAPPING_MAX_AGE + ) + + # recent outbound messages (Discord -> Sneed) awaiting echo mapping + self.recent_outbound = deque(maxlen=PROCESSED_CACHE_SIZE) + + # queued outbound messages when Sneedchat is down (older than TTL dropped) + self.queued_outbound: List[Dict[str, Any]] = [] # {content, channel_id, ts, discord_id} + + # outage tracking + self.outage_message: Optional[Any] = None + self.outage_start: Optional[float] = None + self.outage_task: Optional[asyncio.Task] = None + self.cleanup_task: Optional[asyncio.Task] = None + self.outage_cleanup_task: Optional[asyncio.Task] = None + + # outage event history (for instability detection) + self.outage_events: List[float] = [] # timestamps of outages + + self.shutdown_event = asyncio.Event() + + # start bot handlers + self.setup_bot() + + async def cleanup_loop(self): + """Periodic cleanup of old mappings and queued messages""" + try: + while not self.shutdown_event.is_set(): + await asyncio.sleep(MAPPING_CLEANUP_INTERVAL) + + # Cleanup mapping dictionaries + removed_s2d = self.sneed_to_discord.cleanup_old_entries() + removed_d2s = self.discord_to_sneed.cleanup_old_entries() + removed_usernames = self.sneed_usernames.cleanup_old_entries() + + total_removed = removed_s2d + removed_d2s + removed_usernames + if total_removed > 0: + logger.info(f"🧹 Cleaned up {total_removed} old message mappings") + + # Cleanup expired queued messages + now = time.time() + before_count = len(self.queued_outbound) + self.queued_outbound = [ + msg for msg in self.queued_outbound + if now - msg.get("ts", now) <= QUEUED_MESSAGE_TTL + ] + after_count = len(self.queued_outbound) + if before_count > after_count: + logger.info(f"🧹 Removed {before_count - after_count} expired queued messages") + + except asyncio.CancelledError: + logger.debug("Bridge cleanup task cancelled") + except Exception as e: + logger.error(f"Bridge cleanup loop error: {e}") + + def _get_outage_stats(self) -> Dict[str, Any]: + """Get outage statistics for the last 10 minutes""" + now = time.time() + window_start = now - OUTAGE_INSTABILITY_WINDOW + + # Filter outage events within the window + recent_outages = [ts for ts in self.outage_events if ts >= window_start] + + # Calculate total downtime (approximate: assume each outage lasted until next event or now) + total_downtime = 0 + for i, ts in enumerate(recent_outages): + if i + 1 < len(recent_outages): + total_downtime += recent_outages[i + 1] - ts + else: + # Last outage - only count if still ongoing + if not self.sneed_client.connected: + total_downtime += now - ts + + return { + "count": len(recent_outages), + "total_downtime": total_downtime, + "is_unstable": len(recent_outages) >= OUTAGE_INSTABILITY_THRESHOLD + } + + async def _delete_old_outage_messages(self): + """Delete all old outage messages from the channel""" + try: + channel = self.bot.get_channel(DISCORD_CHANNEL_ID) + if not channel: + return + + # Fetch recent messages and find outage notices + async for message in channel.history(limit=100): + if message.author == self.bot.user and message.embeds: + embed = message.embeds[0] + if embed.title == "🌉 Bridge Status": + await message.delete() + logger.debug(f"Deleted old outage message id={message.id}") + except Exception as e: + logger.debug(f"Could not delete old outage messages: {e}") + + async def _schedule_outage_cleanup(self): + """Schedule deletion of outage message 2 minutes after reconnect""" + if self.outage_cleanup_task and not self.outage_cleanup_task.done(): + self.outage_cleanup_task.cancel() + try: + await self.outage_cleanup_task + except asyncio.CancelledError: + pass + + async def cleanup_after_delay(): + try: + await asyncio.sleep(OUTAGE_CLEANUP_DELAY) + if self.outage_message: + try: + if isinstance(self.outage_message, discord.Message): + await self.outage_message.delete() + else: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + await webhook.delete_message(getattr(self.outage_message, "id", self.outage_message)) + logger.info("🗑️ Deleted outage message after 2 minute delay") + except Exception as e: + logger.debug(f"Could not delete outage message: {e}") + finally: + self.outage_message = None + self.outage_start = None + except asyncio.CancelledError: + pass + + self.outage_cleanup_task = asyncio.create_task(cleanup_after_delay()) + + def setup_bot(self): + @self.bot.event + async def on_ready(): + logger.info(f"🤖 Discord bot ready: {self.bot.user} (id={self.bot.user.id})") + self.session = aiohttp.ClientSession() + + # Start cleanup task + if not self.cleanup_task or self.cleanup_task.done(): + self.cleanup_task = asyncio.create_task(self.cleanup_loop()) + + # ensure sneedclient connected + if not self.sneed_client.connected: + asyncio.create_task(self.sneed_client.connect()) + + @self.bot.event + async def on_message(message: discord.Message): + # ignore bots + if message.author.bot: + return + + # commands + if message.content.startswith("!"): + await self.bot.process_commands(message) + return + + if message.channel.id != DISCORD_CHANNEL_ID: + return + + logger.info(f"📤 Discord → Sneedchat: {message.author.display_name}: {message.content}") + await self.on_discord_message(message) + + @self.bot.event + async def on_message_edit(before: discord.Message, after: discord.Message): + try: + discord_id = after.id + if discord_id in self.discord_to_sneed: + sneed_id = self.discord_to_sneed[discord_id] + payload = json.dumps({"id": int(sneed_id), "message": after.content.strip()}) + logger.info(f"↩️ Discord edit -> Sneedchat (sneed_id={sneed_id})") + await self.sneed_client.send_command(f"/edit {payload}") + else: + logger.debug(f"No mapping for edited discord_id={discord_id}") + except Exception as e: + logger.error(f"Error handling discord edit: {e}") + + @self.bot.event + async def on_message_delete(message: discord.Message): + try: + discord_id = message.id + if discord_id in self.discord_to_sneed: + sneed_id = self.discord_to_sneed[discord_id] + logger.info(f"↩️ Discord delete -> Sneedchat (sneed_id={sneed_id})") + await self.sneed_client.send_command(f"/delete {int(sneed_id)}") + else: + logger.debug(f"No mapping for deleted discord_id={discord_id}") + except Exception as e: + logger.error(f"Error handling discord delete: {e}") + + @self.bot.command(name="status") + async def status_command(ctx): + status = "🟢 Connected" if self.sneed_client.connected else "🔴 Disconnected" + embed = discord.Embed( + title="🌉 Bridge Status", + description=f"**Sneedchat:** {status}\n**Room ID:** {self.sneed_client.room_id}", + color=0x00FF00 if self.sneed_client.connected else 0xFF0000 + ) + await ctx.send(embed=embed) + + @self.bot.command(name="test") + async def test_command(ctx, *, text: str = "This is a test from !test"): + if not self.session: + await ctx.send("❌ No HTTP session available for webhook.") + return + try: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + response = await webhook.send(content=text, username="SneedTestUser", wait=True) + await ctx.send("✅ Test message sent via webhook.") + if args.debug: + logger.debug(f"Webhook test response: {response}") + except Exception as e: + logger.error(f"❌ Test command webhook send failed: {e}") + await ctx.send(f"❌ Failed: {e}") + + def _recent_outbound_iter(self): + return list(self.recent_outbound) + + def _map_discord_sneed(self, discord_id: int, sneed_id: int, username: str): + try: + self.discord_to_sneed[int(discord_id)] = int(sneed_id) + self.sneed_to_discord[int(sneed_id)] = int(discord_id) + self.sneed_usernames[int(sneed_id)] = username + if args.debug: + logger.debug(f"Mapped sneed_id={sneed_id} <-> discord_id={discord_id} (username='{username}')") + except Exception as e: + logger.error(f"Failed to create map discord->{sneed_id}: {e}") + + # -------- Attachment uploads -> litterbox -------- + async def upload_to_litterbox(self, file_url: str, filename: str) -> Optional[str]: + """Download from Discord CDN and upload to Litterbox; return direct URL or None.""" + try: + # Download from the provided URL (this will usually be discordcdn) + async with self.session.get(file_url) as resp: + if resp.status != 200: + logger.error(f"Failed to download attachment '{filename}': HTTP {resp.status}") + return None + data = await resp.read() + # Prepare form data + form = aiohttp.FormData() + # temporary: reqtype=fileupload, time=72h + form.add_field('reqtype', 'fileupload') + form.add_field('time', LITTERBOX_TTL) + form.add_field('fileToUpload', data, filename=filename) + async with self.session.post('https://litterbox.catbox.moe/resources/internals/api.php', data=form) as upl: + if upl.status != 200: + logger.error(f"Litterbox upload failed for '{filename}': HTTP {upl.status}") + txt = await upl.text() + logger.debug(f"Litterbox response: {txt}") + return None + url = (await upl.text()).strip() + logger.info(f"SUCCESS: Uploaded '{filename}' to Litterbox: {url}") + return url + except Exception as e: + logger.error(f"Exception during Litterbox upload for '{filename}': {e}") + return None + + async def format_attachment_bbcode(self, attachment: discord.Attachment) -> Optional[str]: + """Upload attachment and return BBCode string for Sneedchat.""" + url = await self.upload_to_litterbox(attachment.url, attachment.filename) + if not url: + return None + ctype = (attachment.content_type or "").lower() + if ctype.startswith('video/') or attachment.filename.lower().endswith(('.mp4', '.webm', '.mov', '.mkv')): + # Use [url=link]Video N[/url] + return url + else: + # For images, return url (Sneed will embed) + return url + + # -------- Discord -> Sneed message handling -------- + async def on_discord_message(self, message: discord.Message): + # If Sneedchat offline: queue message + content_text = message.content.strip() + # Handle reply mapping (Discord -> Sneed) + if message.reference and getattr(message.reference, "message_id", None): + ref_discord_id = message.reference.message_id + try: + sneed_id = self.discord_to_sneed.get(ref_discord_id) + if sneed_id: + original_username = self.sneed_usernames.get(sneed_id) + if original_username: + # do NOT strip spaces from username per instruction; only strip message text + content_text = f"@{original_username}, {content_text}" + except Exception as e: + logger.error(f"Failed to resolve reply username mapping: {e}") + + # Attachments handling: limit and upload + attachments_bb: List[str] = [] + if message.attachments: + if len(message.attachments) > MAX_ATTACHMENTS: + await message.channel.send(f"❌ Refusing to upload attachments: limit is {MAX_ATTACHMENTS}.") + return + # Upload each and produce BBCode lines + for idx, att in enumerate(message.attachments[:MAX_ATTACHMENTS]): + # Upload and get url + catbox_url = await self.upload_to_litterbox(att.url, att.filename) + if not catbox_url: + # error reporting in discord buffer + await message.channel.send(f"❌ Failed to upload attachment `{att.filename}` to Litterbox; aborting send.") + logger.error(f"Attachment upload failed for {att.filename}; aborting Discord->Sneed send.") + return + # Build bbcode: video -> [url=..]Video N[/url], images -> [url=..][img]..[/img][/url] per earlier spec + content_type = (att.content_type or "").lower() + if content_type.startswith('video') or att.filename.lower().endswith(('.mp4', '.webm', '.mov', '.mkv')): + attachments_bb.append(f"[url={catbox_url}][video]{catbox_url}[/video][/url]") + else: + attachments_bb.append(f"[url={catbox_url}][img]{catbox_url}[/img][/url]") + + # Build final message to send to Sneed + combined = content_text + if attachments_bb: + combined = combined + ("\n" if combined else "") + "\n".join(attachments_bb) + + # Try to send to Sneedchat (non-blocking) + sent = await self.sneed_client.send_message(combined) + if sent: + # record for outbound mapping waiting for echo (so bridge can map sneed id to discord id) + try: + entry = { + "discord_id": message.id, + "content": combined, + "ts": time.time(), + "mapped": False + } + self.recent_outbound.append(entry) + if args.debug: + logger.debug(f"Queued outbound mapping for discord_id={message.id}") + except Exception as e: + logger.error(f"Failed to record outbound mapping: {e}") + else: + # Not connected -> queue with timestamp and inform discord channel + self.queued_outbound.append({ + "content": combined, + "channel_id": message.channel.id, + "ts": time.time(), + "discord_id": message.id + }) + logger.info("Queued message for delivery when Sneedchat reconnects") + try: + # notify channel + await message.channel.send(f"⚠️ Sneedchat appears offline. Your message has been queued for delivery (will expire after {QUEUED_MESSAGE_TTL}s).") + except Exception: + logger.debug("Failed sending queue-notice to channel") + + # -------- Sneedchat -> Discord handlers -------- + async def on_sneed_message(self, msg: Dict[str, Any]): + username = msg.get("username") + raw_content = msg.get("content") + # Always parse through bbcode parser + content = bbcode_to_markdown(raw_content) + raw = msg.get("raw", {}) or {} + message_id = raw.get("message_id") + author_id = msg.get("author_id") + + # Replace mentions of BRIDGE_USERNAME with Discord ping (if configured) + if BRIDGE_USERNAME and DISCORD_PING_USER_ID: + try: + pattern = re.compile(rf'@{re.escape(BRIDGE_USERNAME)}(?=\W|$)', re.IGNORECASE) + content = pattern.sub(f'<@{DISCORD_PING_USER_ID}>', content) + except Exception: + pass + + avatar_url = None + author = raw.get("author", {}) or {} + if author.get("avatar_url"): + avatar_path = author["avatar_url"] + avatar_url = f"https://kiwifarms.st{avatar_path}" if avatar_path.startswith("/") else avatar_path + + # If this Sneed message is an echo of the bridge user -> attempt to map to the outbound discord message, DO NOT forward + if (author_id and BRIDGE_USER_ID and author_id == BRIDGE_USER_ID) or (BRIDGE_USERNAME and username == BRIDGE_USERNAME): + if args.debug: + logger.debug(f"Bridge-echo from sneed_id={message_id} (username={username}); attempting mapping but not forwarding") + if message_id: + now = time.time() + matched_entry = None + for entry in list(self.recent_outbound): + if entry.get("mapped"): + continue + if entry.get("content") == (raw_content) and (now - entry.get("ts", 0)) <= OUTBOUND_MATCH_WINDOW: + matched_entry = entry + break + if matched_entry: + discord_id = matched_entry["discord_id"] + self._map_discord_sneed(discord_id, int(message_id), username) + matched_entry["mapped"] = True + if args.debug: + logger.debug(f"Mapped outbound discord_id={discord_id} -> sneed_id={message_id} (bridge echo)") + return + if args.debug: + logger.debug("No recent outbound match for bridge echo; dropping silently") + return + + # Normal Sneed-origin message: post via webhook with parsed content + if not self.session: + logger.error("❌ No HTTP session for webhook operations") + return + + try: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + # send parsed content; username is verbatim from Sneed JSON + sent = await webhook.send(content=content, username=username, avatar_url=avatar_url, wait=True) + logger.info(f"✅ Sent Sneedchat → Discord: {username}") + # map to track edits/deletes + if message_id: + discord_msg_id = None + try: + # some webhook libs return an object, sometimes id directly; handle both + discord_msg_id = int(getattr(sent, "id", None) or sent) + except Exception: + discord_msg_id = None + if discord_msg_id: + self.sneed_to_discord[int(message_id)] = discord_msg_id + self.discord_to_sneed[discord_msg_id] = int(message_id) + self.sneed_usernames[int(message_id)] = username + if args.debug: + logger.debug(f"Mapped Sneed->{discord_msg_id} (sneed_id={message_id})") + except Exception as e: + logger.error(f"❌ Failed to send Sneed → Discord webhook message: {e}") + + async def _handle_sneed_edit(self, sneed_id: int, new_content: str): + try: + sneed_id = int(sneed_id) + except Exception: + return + discord_msg_id = self.sneed_to_discord.get(sneed_id) + if not discord_msg_id: + logger.debug(f"No discord mapping for sneed edit id={sneed_id}") + return + if not self.session: + logger.error("❌ No HTTP session for webhook edit") + return + + # run through parser BEFORE editing so bbcode isn't shown raw + parsed = bbcode_to_markdown(new_content) + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + try: + await webhook.edit_message(discord_msg_id, content=parsed) + logger.info(f"✏️ Edited Discord (webhook) message id={discord_msg_id} (sneed_id={sneed_id})") + except Exception as e: + logger.error(f"❌ Failed to edit Discord message id={discord_msg_id}: {e}") + + async def _handle_sneed_delete(self, sneed_id: int): + try: + sneed_id = int(sneed_id) + except Exception: + return + discord_msg_id = self.sneed_to_discord.get(sneed_id) + if not discord_msg_id: + logger.debug(f"No discord mapping for sneed delete id={sneed_id}") + return + if not self.session: + logger.error("❌ No HTTP session for webhook delete") + return + + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + try: + await webhook.delete_message(discord_msg_id) + logger.info(f"🗑️ Deleted Discord (webhook) message id={discord_msg_id} (sneed_id={sneed_id})") + self.sneed_to_discord.pop(sneed_id, None) + self.discord_to_sneed.pop(discord_msg_id, None) + self.sneed_usernames.pop(sneed_id, None) + except Exception as e: + logger.error(f"❌ Failed to delete Discord message id={discord_msg_id}: {e}") + + # -------- Sneedchat connect/disconnect (outage embed) -------- + async def on_sneed_connect(self): + logger.info("🟢 Sneedchat connected") + if self.bot.is_ready(): + await self.bot.change_presence(status=discord.Status.online) + + # Get outage stats before finalizing + stats = self._get_outage_stats() + + # finalize outage embed if present + if self.outage_message: + try: + elapsed = int(time.time() - (self.outage_start or time.time())) + attempts = getattr(self.sneed_client, "reconnect_attempts", 0) + + # Build embed based on whether system is unstable + if stats["is_unstable"]: + embed = discord.Embed( + title="🌉 Bridge Status", + description="✅ **Sneedchat reconnected (instability resolved)**", + color=0x00FF00 + ) + embed.add_field(name="Last Incident Duration", value=f"{elapsed}s", inline=True) + embed.add_field(name="Total Downtime (10min)", value=f"{int(stats['total_downtime'])}s", inline=True) + embed.add_field(name="Outages (10min)", value=str(stats["count"]), inline=True) + else: + embed = discord.Embed( + title="🌉 Bridge Status", + description="✅ **Sneedchat reconnected**", + color=0x00FF00 + ) + embed.add_field(name="Downtime", value=f"{elapsed}s", inline=True) + embed.add_field(name="Reconnect Attempts", value=str(attempts), inline=True) + + embed.add_field(name="Room ID", value=str(self.sneed_client.room_id), inline=True) + + try: + if isinstance(self.outage_message, discord.Message): + await self.outage_message.edit(content=None, embed=embed) + else: + # attempt webhook edit if outage_message is webhook response + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + await webhook.edit_message(getattr(self.outage_message, "id", self.outage_message), embed=embed) + logger.info("🔔 Outage notice updated as restored") + except Exception as e: + logger.error(f"Failed to update outage message on reconnect: {e}") + except Exception as e: + logger.error(f"Error finalizing outage message: {e}") + + # Schedule deletion 2 minutes after reconnect + await self._schedule_outage_cleanup() + else: + # No outage message, but still schedule cleanup (cleanup after 2 min if Sneedchat goes down again and comes back) + pass + + # After reconnect, try flushing queued_outbound + asyncio.create_task(self._flush_queued_messages()) + + async def on_sneed_disconnect(self): + logger.warning("🔴 Sneedchat disconnected") + if self.bot.is_ready(): + await self.bot.change_presence(status=discord.Status.idle) + + # Record this outage event + self.outage_events.append(time.time()) + + # Clean up old events outside the 10-minute window + now = time.time() + self.outage_events = [ts for ts in self.outage_events if now - ts <= OUTAGE_INSTABILITY_WINDOW] + + # If there's an existing outage message, delete old ones and reset + if self.outage_message: + logger.debug("Deleting old outage message due to new outage") + try: + if isinstance(self.outage_message, discord.Message): + await self.outage_message.delete() + else: + try: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + await webhook.delete_message(getattr(self.outage_message, "id", self.outage_message)) + except Exception: + pass + except Exception: + pass + + self.outage_message = None + self.outage_start = None + + # Cancel cleanup task if running + if self.outage_cleanup_task and not self.outage_cleanup_task.done(): + self.outage_cleanup_task.cancel() + try: + await self.outage_cleanup_task + except asyncio.CancelledError: + pass + + # Cancel updater task if running + if self.outage_task and not self.outage_task.done(): + self.outage_task.cancel() + try: + await self.outage_task + except asyncio.CancelledError: + pass + + # Get current stats + stats = self._get_outage_stats() + + # record outage start and post embed + self.outage_start = time.time() + current_attempts = getattr(self.sneed_client, "reconnect_attempts", 0) + try: + channel = self.bot.get_channel(DISCORD_CHANNEL_ID) + + # Build embed based on stability + if stats["is_unstable"]: + embed = discord.Embed( + title="🌉 Bridge Status", + description="⚠️ **Sneedchat unstable - multiple reconnections**", + color=0xFF0000 + ) + embed.add_field(name="Outages (10min)", value=str(stats["count"]), inline=True) + embed.add_field(name="Last Outage Duration", value="0s", inline=True) + embed.add_field(name="Total Downtime", value=f"{int(stats['total_downtime'])}s", inline=True) + else: + embed = discord.Embed( + title="🌉 Bridge Status", + description="⚠️ **Sneedchat disconnected**", + color=0xFF0000 + ) + embed.add_field(name="Outage Duration", value="0s", inline=True) + embed.add_field(name="Reconnect Attempts", value=str(current_attempts), inline=True) + + embed.add_field(name="Room ID", value=str(self.sneed_client.room_id), inline=True) + + if channel: + self.outage_message = await channel.send(embed=embed) + logger.info("🔔 Outage notice posted to Discord") + else: + # fallback to webhook if channel isn't available + if self.session: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + sent = await webhook.send(embed=embed, username="SneedBridge", wait=True) + logger.info("🔔 Outage notice posted to Discord via webhook") + self.outage_message = sent + else: + logger.error("No channel found and no session available to post outage notice") + + # start updater task + async def updater(): + try: + while self.outage_message and not self.sneed_client.connected: + elapsed = int(time.time() - (self.outage_start or time.time())) + attempts = getattr(self.sneed_client, "reconnect_attempts", 0) + current_stats = self._get_outage_stats() + + # Update embed based on stability + if current_stats["is_unstable"]: + embed = discord.Embed( + title="🌉 Bridge Status", + description="⚠️ **Sneedchat outage ongoing (system unstable)**", + color=0xFF0000 + ) + embed.add_field(name="Outages (10min)", value=str(current_stats["count"]), inline=True) + embed.add_field(name="Last Outage Duration", value=f"{elapsed}s", inline=True) + embed.add_field(name="Total Downtime", value=f"{int(current_stats['total_downtime'])}s", inline=True) + else: + embed = discord.Embed( + title="🌉 Bridge Status", + description="⚠️ **Sneedchat outage ongoing**", + color=0xFF0000 + ) + embed.add_field(name="Outage Duration", value=f"{elapsed}s", inline=True) + embed.add_field(name="Reconnect Attempts", value=str(attempts), inline=True) + + embed.add_field(name="Room ID", value=str(self.sneed_client.room_id), inline=True) + + try: + if isinstance(self.outage_message, discord.Message): + await self.outage_message.edit(embed=embed) + else: + try: + webhook = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, session=self.session) + await webhook.edit_message(getattr(self.outage_message, "id", self.outage_message), embed=embed) + except Exception: + logger.debug("Could not edit outage webhook message; skipping edit") + except Exception as e: + logger.error(f"Failed to update outage message: {e}") + await asyncio.sleep(OUTAGE_UPDATE_INTERVAL) + except asyncio.CancelledError: + logger.debug("Outage updater task cancelled") + return + self.outage_task = asyncio.create_task(updater()) + except Exception as e: + logger.error(f"Failed to send outage notice: {e}") + + # -------- Queue flush & maintenance -------- + async def _flush_queued_messages(self): + """Attempt to send queued messages to Sneedchat after reconnection.""" + if not self.queued_outbound: + return + logger.info(f"Flushing {len(self.queued_outbound)} queued messages to Sneedchat") + now = time.time() + # iterate copy to allow removal + for entry in list(self.queued_outbound): + age = now - entry.get("ts", now) + channel_id = entry.get("channel_id") + channel = self.bot.get_channel(channel_id) + if age > QUEUED_MESSAGE_TTL: + # abandon and inform channel + try: + if channel: + await channel.send(f"❌ Failed to deliver message queued {int(age)}s ago (expired):\n{entry.get('content')[:400]}") + except Exception: + pass + self.queued_outbound.remove(entry) + continue + # try sending + sent = await self.sneed_client.send_message(entry.get("content")) + if sent: + # add to recent_outbound for mapping via echo + self.recent_outbound.append({ + "discord_id": entry.get("discord_id"), + "content": entry.get("content"), + "ts": time.time(), + "mapped": False + }) + # remove from queue + self.queued_outbound.remove(entry) + if channel: + try: + await channel.send("✅ Queued message delivered to Sneedchat after reconnect.") + except Exception: + pass + else: + # still not connected (shouldn't happen inside on_sneed_connect), break + logger.debug("Sneedchat still not accepting messages during flush") + break + + # -------- Cleanup -------- + async def cleanup(self): + self.shutdown_event.set() + + if self.cleanup_task and not self.cleanup_task.done(): + self.cleanup_task.cancel() + try: + await self.cleanup_task + except asyncio.CancelledError: + pass + + if self.outage_task and not self.outage_task.done(): + self.outage_task.cancel() + try: + await self.outage_task + except asyncio.CancelledError: + pass + + if self.outage_cleanup_task and not self.outage_cleanup_task.done(): + self.outage_cleanup_task.cancel() + try: + await self.outage_cleanup_task + except asyncio.CancelledError: + pass + + if self.session and not self.session.closed: + await self.session.close() + + async def start(self): + await self.bot.start(DISCORD_BOT_TOKEN) + +# -------- Main -------- +async def main(): + logger.info("Starting Discord-Sneedchat Bridge") + + # start cookie refresh + cookie_service = CookieRefreshService(username=BRIDGE_USERNAME, password=BRIDGE_PASSWORD) + await cookie_service.start() + logger.info("⏳ Waiting for initial cookie...") + await cookie_service.wait_for_cookie() + initial_cookie = cookie_service.get_current_cookie() + if not initial_cookie: + logger.error("❌ Failed to obtain initial cookie, cannot start bridge") + await cookie_service.stop() + return + + # instantiate sneed client & bridge + sneed_client = SneedChatClient(cookie=initial_cookie, room_id=SNEEDCHAT_ROOM_ID, reconnect_interval=RECONNECT_INTERVAL, cookie_service=cookie_service) + bridge = DiscordBridge(sneed_client=sneed_client) + + # run the bridge (discord bot) + try: + await bridge.start() + except KeyboardInterrupt: + logger.info("Shutdown requested") + finally: + await cookie_service.stop() + await sneed_client.disconnect() + await bridge.cleanup() + +if __name__ == "__main__": + try: + asyncio.run(main()) + except KeyboardInterrupt: + logger.info("Interrupted by user") + except Exception as e: + logger.error(f"Fatal error: {e}") + exit(1) \ No newline at end of file