├── .gitattributes ├── .gitignore ├── .gitmodules ├── Cargo.toml ├── LICENSE ├── README.md ├── rustcord-sys ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── src │ └── lib.rs └── wrapper.h └── src ├── event_handlers.rs ├── event_wrappers.rs ├── join_request.rs ├── lib.rs └── presence.rs /.gitattributes: -------------------------------------------------------------------------------- 1 | rustcord-sys/discord-rpc-3.4.0/* linguist-vendored 2 | rustcord-sys/*.h linguist-language=C -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/rust,intellij+all 3 | # Edit at https://www.gitignore.io/?templates=rust,intellij+all 4 | 5 | ### Intellij+all ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | 40 | # CMake 41 | cmake-build-*/ 42 | 43 | # Mongo Explorer plugin 44 | .idea/**/mongoSettings.xml 45 | 46 | # File-based project format 47 | *.iws 48 | 49 | # IntelliJ 50 | out/ 51 | 52 | # mpeltonen/sbt-idea plugin 53 | .idea_modules/ 54 | 55 | # JIRA plugin 56 | atlassian-ide-plugin.xml 57 | 58 | # Cursive Clojure plugin 59 | .idea/replstate.xml 60 | 61 | # Crashlytics plugin (for Android Studio and IntelliJ) 62 | com_crashlytics_export_strings.xml 63 | crashlytics.properties 64 | crashlytics-build.properties 65 | fabric.properties 66 | 67 | # Editor-based Rest Client 68 | .idea/httpRequests 69 | 70 | # Android studio 3.1+ serialized cache file 71 | .idea/caches/build_file_checksums.ser 72 | 73 | # JetBrains templates 74 | **___jb_tmp___ 75 | 76 | ### Intellij+all Patch ### 77 | # Ignores the whole .idea folder and all .iml files 78 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 79 | 80 | .idea/ 81 | 82 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 83 | 84 | *.iml 85 | modules.xml 86 | .idea/misc.xml 87 | *.ipr 88 | 89 | # Sonarlint plugin 90 | .idea/sonarlint 91 | 92 | ### Rust ### 93 | # Generated by Cargo 94 | # will have compiled files and executables 95 | /target/ 96 | 97 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 98 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 99 | Cargo.lock 100 | 101 | # These are backup files generated by rustfmt 102 | **/*.rs.bk 103 | 104 | # End of https://www.gitignore.io/api/rust,intellij+all -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "rustcord-sys/discord-rpc"] 2 | path = rustcord-sys/discord-rpc 3 | url = https://github.com/discordapp/discord-rpc 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rustcord" 3 | version = "0.2.4" 4 | authors = ["AregevDev "] 5 | edition = "2018" 6 | description = "A safe wrapper around the Discord Rich Presence API" 7 | keywords = ["discord", "rpc"] 8 | repository = "https://github.com/AregevDev/rustcord" 9 | categories = ["api-bindings"] 10 | license = "Apache-2.0" 11 | readme = "README.md" 12 | 13 | [dependencies] 14 | libc = "0.2.66" 15 | rustcord-sys = { path = "rustcord-sys", version = "0.2.4" } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rustcord 2 | A safe wrapper around the Discord Rich Presence API, updated to the latest library version. 3 | Wrapper version: 0.2.4 4 | Discord RPC version: 3.4.0 5 | 6 | ### Example 7 | ```rust 8 | use rustcord::{Rustcord, EventHandlers, User, RichPresenceBuilder}; 9 | use std::io; 10 | 11 | pub struct Handlers; 12 | 13 | impl EventHandlers for Handlers { 14 | fn ready(user: User) { 15 | println!("User {}#{} logged in...", user.username, user.discriminator); 16 | } 17 | } 18 | 19 | fn main() -> Result<(), io::Error> { 20 | let discord = Rustcord::init::("APP_ID_HERE", true, None)?; 21 | 22 | let presence = RichPresenceBuilder::new() 23 | .state("Rusting") 24 | .details("Mining few crystals") 25 | .large_image_key("rust") 26 | .large_image_text("Rust") 27 | .small_image_key("amethyst") 28 | .small_image_text("Amethyst") 29 | .build(); 30 | 31 | discord.update_presence(presence)?; 32 | loop { 33 | discord.run_callbacks(); 34 | } 35 | 36 | Ok(()) 37 | } 38 | ``` 39 | 40 | ### Documentation 41 | [docs.rs][docs_rs] 42 | 43 | ### Useful links 44 | [The C API Documentation][rpc_docs] 45 | 46 | ### License 47 | Apache-2.0 48 | 49 | [docs_rs]:https://docs.rs/rustcord 50 | [rpc_docs]:https://discordapp.com/developers/docs/rich-presence/how-to 51 | -------------------------------------------------------------------------------- /rustcord-sys/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/rust,intellij+all 3 | # Edit at https://www.gitignore.io/?templates=rust,intellij+all 4 | 5 | ### Intellij+all ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | 40 | # CMake 41 | cmake-build-*/ 42 | 43 | # Mongo Explorer plugin 44 | .idea/**/mongoSettings.xml 45 | 46 | # File-based project format 47 | *.iws 48 | 49 | # IntelliJ 50 | out/ 51 | 52 | # mpeltonen/sbt-idea plugin 53 | .idea_modules/ 54 | 55 | # JIRA plugin 56 | atlassian-ide-plugin.xml 57 | 58 | # Cursive Clojure plugin 59 | .idea/replstate.xml 60 | 61 | # Crashlytics plugin (for Android Studio and IntelliJ) 62 | com_crashlytics_export_strings.xml 63 | crashlytics.properties 64 | crashlytics-build.properties 65 | fabric.properties 66 | 67 | # Editor-based Rest Client 68 | .idea/httpRequests 69 | 70 | # Android studio 3.1+ serialized cache file 71 | .idea/caches/build_file_checksums.ser 72 | 73 | # JetBrains templates 74 | **___jb_tmp___ 75 | 76 | ### Intellij+all Patch ### 77 | # Ignores the whole .idea folder and all .iml files 78 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 79 | 80 | .idea/ 81 | 82 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 83 | 84 | *.iml 85 | modules.xml 86 | .idea/misc.xml 87 | *.ipr 88 | 89 | # Sonarlint plugin 90 | .idea/sonarlint 91 | 92 | ### Rust ### 93 | # Generated by Cargo 94 | # will have compiled files and executables 95 | /target/ 96 | 97 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 98 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 99 | Cargo.lock 100 | 101 | # These are backup files generated by rustfmt 102 | **/*.rs.bk 103 | 104 | # End of https://www.gitignore.io/api/rust,intellij+all -------------------------------------------------------------------------------- /rustcord-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rustcord-sys" 3 | version = "0.2.4" 4 | authors = ["AregevDev "] 5 | edition = "2018" 6 | description = "Raw FFI bindings for the Discord Rich Presence API" 7 | keywords = ["discord", "rpc"] 8 | repository = "https://github.com/AregevDev/rustcord" 9 | categories = ["api-bindings"] 10 | license = "Apache-2.0" 11 | readme = "README.md" 12 | 13 | [dependencies] 14 | 15 | [build-dependencies] 16 | bindgen = "0.52.0" 17 | cmake = "0.1.42" 18 | -------------------------------------------------------------------------------- /rustcord-sys/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /rustcord-sys/README.md: -------------------------------------------------------------------------------- 1 | # rustcord-sys 2 | Raw FFI bindings for the Discord Rich Presence API -------------------------------------------------------------------------------- /rustcord-sys/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::PathBuf; 3 | 4 | const LIB_VERSION: &str = "3.4.0"; 5 | 6 | fn main() { 7 | let config = cmake::Config::new("discord-rpc".to_string()) 8 | .define("BUILD_EXAMPLES", "OFF") 9 | .build(); 10 | 11 | println!("cargo:rustc-link-search={}", config.join("lib").display()); 12 | println!("cargo:rustc-link-search={}", config.join("lib64").display()); 13 | 14 | let include_path = format!("discord-rpc-{}/include", LIB_VERSION); 15 | 16 | let bindings = bindgen::Builder::default() 17 | .header("wrapper.h") 18 | .clang_arg(format!("-I{}", include_path)) 19 | .generate() 20 | .expect("Unable to generate bindings"); 21 | 22 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 23 | bindings 24 | .write_to_file(out_path.join("bindings.rs")) 25 | .expect("Couldn't write bindings!"); 26 | 27 | println!("cargo:rustc-link-lib=static=discord-rpc"); 28 | } 29 | -------------------------------------------------------------------------------- /rustcord-sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_upper_case_globals)] 2 | #![allow(non_camel_case_types)] 3 | #![allow(non_snake_case)] 4 | 5 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 6 | -------------------------------------------------------------------------------- /rustcord-sys/wrapper.h: -------------------------------------------------------------------------------- 1 | #include "discord-rpc/include/discord_rpc.h" -------------------------------------------------------------------------------- /src/event_handlers.rs: -------------------------------------------------------------------------------- 1 | use crate::event_wrappers::*; 2 | use crate::join_request::*; 3 | use rustcord_sys as sys; 4 | 5 | /// A trait for handling the presence events 6 | pub trait EventHandlers { 7 | /// Called when the presence is displayed 8 | fn ready(_user: User) {} 9 | 10 | /// Called when an error occurred with the presence 11 | fn errored(_errcode: i32, _message: &str) {} 12 | 13 | /// Called when the presence is shut down or closed 14 | fn disconnected(_errcode: i32, _message: &str) {} 15 | 16 | /// Called when another user is joining your game 17 | fn join_game(_secret: &str) {} 18 | 19 | /// Called when a join request is received 20 | fn spectate_game(_secret: &str) {} 21 | 22 | /// Called when a join request is received 23 | fn join_request(_request: User, _respond: R) {} 24 | } 25 | 26 | pub(crate) fn wrap() -> sys::DiscordEventHandlers { 27 | sys::DiscordEventHandlers { 28 | ready: Some(ready_wrapper::), 29 | disconnected: Some(disconnected_wrapper::), 30 | errored: Some(errored_wrapper::), 31 | joinGame: Some(join_game_wrapper::), 32 | spectateGame: Some(spectate_game_wrapper::), 33 | joinRequest: Some(join_request_wrapper::), 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/event_wrappers.rs: -------------------------------------------------------------------------------- 1 | use crate::join_request::{JoinRequestReply, User}; 2 | use crate::EventHandlers; 3 | use libc; 4 | use rustcord_sys as sys; 5 | use std::ffi::CStr; 6 | 7 | pub(crate) unsafe extern "C" fn ready_wrapper( 8 | discord_user: *const sys::DiscordUser, 9 | ) { 10 | let user = User { 11 | user_id: CStr::from_ptr((*discord_user).userId) 12 | .to_string_lossy() 13 | .into_owned(), 14 | username: CStr::from_ptr((*discord_user).username) 15 | .to_string_lossy() 16 | .into_owned(), 17 | discriminator: CStr::from_ptr((*discord_user).discriminator) 18 | .to_string_lossy() 19 | .into_owned(), 20 | avatar: CStr::from_ptr((*discord_user).avatar) 21 | .to_string_lossy() 22 | .into_owned(), 23 | }; 24 | EH::ready(user); 25 | } 26 | 27 | pub(crate) unsafe extern "C" fn disconnected_wrapper( 28 | errcode: libc::c_int, 29 | message: *const libc::c_char, 30 | ) { 31 | EH::disconnected(errcode as i32, &CStr::from_ptr(message).to_string_lossy()); 32 | } 33 | 34 | pub(crate) unsafe extern "C" fn errored_wrapper( 35 | errcode: libc::c_int, 36 | message: *const libc::c_char, 37 | ) { 38 | EH::errored(errcode as i32, &CStr::from_ptr(message).to_string_lossy()); 39 | } 40 | 41 | pub(crate) unsafe extern "C" fn join_game_wrapper(secret: *const libc::c_char) { 42 | EH::join_game(&CStr::from_ptr(secret).to_string_lossy()); 43 | } 44 | 45 | pub(crate) unsafe extern "C" fn spectate_game_wrapper( 46 | secret: *const libc::c_char, 47 | ) { 48 | EH::spectate_game(&CStr::from_ptr(secret).to_string_lossy()); 49 | } 50 | 51 | pub(crate) unsafe extern "C" fn join_request_wrapper( 52 | discord_user: *const sys::DiscordUser, 53 | ) { 54 | let req = User { 55 | user_id: CStr::from_ptr((*discord_user).userId) 56 | .to_string_lossy() 57 | .into_owned(), 58 | username: CStr::from_ptr((*discord_user).username) 59 | .to_string_lossy() 60 | .into_owned(), 61 | discriminator: CStr::from_ptr((*discord_user).discriminator) 62 | .to_string_lossy() 63 | .into_owned(), 64 | avatar: CStr::from_ptr((*discord_user).avatar) 65 | .to_string_lossy() 66 | .into_owned(), 67 | }; 68 | EH::join_request(req, |reply| { 69 | sys::Discord_Respond( 70 | (*discord_user).userId, 71 | match reply { 72 | JoinRequestReply::No => sys::DISCORD_REPLY_NO, 73 | JoinRequestReply::Yes => sys::DISCORD_REPLY_YES, 74 | JoinRequestReply::Ignore => sys::DISCORD_REPLY_IGNORE, 75 | } as libc::c_int, 76 | ); 77 | }); 78 | } 79 | -------------------------------------------------------------------------------- /src/join_request.rs: -------------------------------------------------------------------------------- 1 | /// Represents a Discord user that has a rich presence on 2 | #[derive(Default, Clone, Hash, PartialEq, Debug)] 3 | pub struct User { 4 | pub user_id: String, 5 | pub username: String, 6 | pub discriminator: String, 7 | pub avatar: String, 8 | } 9 | 10 | /// A reply to a user's request 11 | #[derive(Clone, Hash, PartialEq, Debug)] 12 | pub enum JoinRequestReply { 13 | No, 14 | Yes, 15 | Ignore, 16 | } 17 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A safe wrapper around the Discord RichPresence API 2 | //! ### Example 3 | //! ```rust 4 | //! use rustcord::{Rustcord, EventHandlers, User, RichPresenceBuilder}; 5 | //! use std::io; 6 | //! 7 | //! pub struct Handlers; 8 | //! 9 | //! impl EventHandlers for Handlers { 10 | //! fn ready(user: User) { 11 | //! println!("User {}#{} logged in...", user.username, user.discriminator); 12 | //! } 13 | //! } 14 | //! 15 | //! fn main() -> Result<(), io::Error> { 16 | //! let discord = Rustcord::init::("544523578855391241", true, None)?; 17 | //! 18 | //! let presence = RichPresenceBuilder::new() 19 | //! .state("Rusting") 20 | //! .details("Mining few crystals") 21 | //! .large_image_key("rust") 22 | //! .large_image_text("Rust") 23 | //! .small_image_key("amethyst") 24 | //! .small_image_text("Amethyst") 25 | //! .build(); 26 | //! 27 | //! discord.update_presence(presence)?; 28 | //! loop { 29 | //! discord.run_callbacks(); 30 | //! } 31 | //! 32 | //! Ok(()) 33 | //! } 34 | //! ``` 35 | 36 | mod event_handlers; 37 | mod event_wrappers; 38 | mod join_request; 39 | mod presence; 40 | 41 | pub use crate::event_handlers::EventHandlers; 42 | pub use crate::join_request::{JoinRequestReply, User}; 43 | pub use crate::presence::{RichPresence, RichPresenceBuilder}; 44 | use rustcord_sys as sys; 45 | 46 | use std::ffi::{CString, NulError}; 47 | use std::ptr; 48 | 49 | /// The main struct of the API providing the RPC methods 50 | pub struct Rustcord; 51 | 52 | impl Rustcord { 53 | /// Initializes the Discord Rich Presence API. 54 | pub fn init( 55 | app_id: &str, 56 | auto_register: bool, 57 | steam_id: Option<&str>, 58 | ) -> Result { 59 | let mut sys_handlers = event_handlers::wrap::(); 60 | unsafe { 61 | sys::Discord_Initialize( 62 | CString::new(app_id)?.into_raw(), 63 | &mut sys_handlers, 64 | auto_register as libc::c_int, 65 | match steam_id { 66 | None => ptr::null(), 67 | Some(id) => CString::new(id)?.into_raw(), 68 | }, 69 | ); 70 | } 71 | 72 | Ok(Rustcord) 73 | } 74 | 75 | /// Updates the callback handlers. 76 | pub fn update_handlers(&self) { 77 | let mut sys_handlers = event_handlers::wrap::(); 78 | unsafe { 79 | sys::Discord_UpdateHandlers(&mut sys_handlers); 80 | } 81 | } 82 | 83 | /// Updates the rich presence screen. 84 | pub fn update_presence(&self, presence: RichPresence) -> Result<(), NulError> { 85 | let sys_presence = presence.wrap()?; 86 | unsafe { 87 | sys::Discord_UpdatePresence(&sys_presence); 88 | } 89 | 90 | Ok(()) 91 | } 92 | 93 | /// Clears the rich present screen.DiscordRPC 94 | pub fn clear_presence(&self) { 95 | unsafe { 96 | sys::Discord_ClearPresence(); 97 | } 98 | } 99 | 100 | /// Invokes any pending callbacks from Discord on the calling thread. 101 | /// This function is allegedly thread safe. 102 | pub fn run_callbacks(&self) { 103 | unsafe { 104 | sys::Discord_RunCallbacks(); 105 | } 106 | } 107 | } 108 | 109 | impl Drop for Rustcord { 110 | fn drop(&mut self) { 111 | unsafe { 112 | sys::Discord_Shutdown(); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/presence.rs: -------------------------------------------------------------------------------- 1 | use libc; 2 | use rustcord_sys as sys; 3 | use std::ffi::{CString, NulError}; 4 | use std::ptr; 5 | use std::time::*; 6 | 7 | /// Defines the data displayed on the rich presence screen on a user's profile. 8 | /// Due to the C API limits, this data is limited to a maximum amount of bytes will be listed below 9 | #[derive(Default, Clone, Hash, PartialEq, Debug)] 10 | pub struct RichPresence { 11 | /// The user's current party status. Maximum of 128 bytes. 12 | pub state: Option, 13 | 14 | /// What the player is currently doing. Maximum of 128 bytes. 15 | pub details: Option, 16 | 17 | /// Time of game start. Including will show time as "elapsed". 18 | pub start_time: Option, 19 | 20 | /// Time of game end. Including will show time as "remaining". 21 | pub end_time: Option, 22 | 23 | /// Name of the uploaded image for the large profile artwork. Maximum of 32 bytes. 24 | pub large_image_key: Option, 25 | 26 | /// Tooltip for the large image. Maximum of 128 bytes. 27 | pub large_image_text: Option, 28 | 29 | /// Name of the uploaded image for the small profile artwork. Maximum of 32 bytes. 30 | pub small_image_key: Option, 31 | 32 | /// Tooltip for the small image. Maximum of 128 bytes. 33 | pub small_image_text: Option, 34 | 35 | /// ID of the player's party, lobby, or group. Maximum of 128 bytes. 36 | pub party_id: Option, 37 | 38 | /// Current size of the player's party, lobby, or group. 39 | pub party_size: Option, 40 | 41 | /// Maximum size of the player's party, lobby, or group. 42 | pub party_max: Option, 43 | 44 | /// Unique hashed string for Spectate button. Maximum of 128 bytes. 45 | pub spectate_secret: Option, 46 | 47 | /// Unique hashed string for chat invitations and Ask to Join. Maximum of 128 bytes. 48 | pub join_secret: Option, 49 | } 50 | 51 | impl RichPresence { 52 | pub(crate) fn wrap(self) -> Result { 53 | Ok(sys::DiscordRichPresence { 54 | state: match self.state { 55 | None => ptr::null(), 56 | Some(state) => CString::new(state.clone())?.into_raw(), 57 | }, 58 | details: match self.details { 59 | None => ptr::null(), 60 | Some(details) => CString::new(details)?.into_raw(), 61 | }, 62 | startTimestamp: match self.start_time { 63 | None => 0, 64 | Some(time) => time.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, 65 | }, 66 | endTimestamp: match self.end_time { 67 | None => 0, 68 | Some(time) => time.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, 69 | }, 70 | largeImageKey: match self.large_image_key { 71 | None => ptr::null(), 72 | Some(key) => CString::new(key)?.into_raw(), 73 | }, 74 | largeImageText: match self.large_image_text { 75 | None => ptr::null(), 76 | Some(text) => CString::new(text)?.into_raw(), 77 | }, 78 | smallImageKey: match self.small_image_key { 79 | None => ptr::null(), 80 | Some(key) => CString::new(key)?.into_raw(), 81 | }, 82 | smallImageText: match self.small_image_text { 83 | None => ptr::null(), 84 | Some(text) => CString::new(text)?.into_raw(), 85 | }, 86 | partyId: match self.party_id { 87 | None => ptr::null(), 88 | Some(id) => CString::new(id)?.into_raw(), 89 | }, 90 | partySize: match self.party_size { 91 | None => 0, 92 | Some(size) => size as libc::c_int, 93 | }, 94 | partyMax: match self.party_max { 95 | None => 0, 96 | Some(max) => max as libc::c_int, 97 | }, 98 | matchSecret: ptr::null(), // deprecated 99 | joinSecret: match self.join_secret { 100 | None => ptr::null(), 101 | Some(secret) => CString::new(secret)?.into_raw(), 102 | }, 103 | spectateSecret: match self.spectate_secret { 104 | None => ptr::null(), 105 | Some(secret) => CString::new(secret)?.into_raw(), 106 | }, 107 | instance: 0, // deprecated 108 | }) 109 | } 110 | } 111 | 112 | /// A Builder struct for RichPresence which gives more ergonomic API 113 | #[derive(Clone, Debug)] 114 | pub struct RichPresenceBuilder { 115 | inner: RichPresence, 116 | } 117 | 118 | impl RichPresenceBuilder { 119 | pub fn new() -> Self { 120 | RichPresenceBuilder { 121 | inner: RichPresence::default(), 122 | } 123 | } 124 | 125 | pub fn state(mut self, state: &str) -> Self { 126 | self.inner.state = Some(state.to_owned()); 127 | self 128 | } 129 | 130 | pub fn details(mut self, details: &str) -> Self { 131 | self.inner.details = Some(details.to_owned()); 132 | self 133 | } 134 | 135 | pub fn start_time(mut self, start_time: SystemTime) -> Self { 136 | self.inner.start_time = Some(start_time); 137 | self 138 | } 139 | 140 | pub fn end_time(mut self, end_time: SystemTime) -> Self { 141 | self.inner.end_time = Some(end_time); 142 | self 143 | } 144 | 145 | pub fn large_image_key(mut self, large_image_key: &str) -> Self { 146 | self.inner.large_image_key = Some(large_image_key.to_owned()); 147 | self 148 | } 149 | 150 | pub fn large_image_text(mut self, large_image_text: &str) -> Self { 151 | self.inner.large_image_text = Some(large_image_text.to_owned()); 152 | self 153 | } 154 | 155 | pub fn small_image_key(mut self, small_image_key: &str) -> Self { 156 | self.inner.small_image_key = Some(small_image_key.to_owned()); 157 | self 158 | } 159 | 160 | pub fn small_image_text(mut self, small_image_text: &str) -> Self { 161 | self.inner.small_image_text = Some(small_image_text.to_owned()); 162 | self 163 | } 164 | 165 | pub fn party_id(mut self, party_id: &str) -> Self { 166 | self.inner.party_id = Some(party_id.to_owned()); 167 | self 168 | } 169 | 170 | pub fn party_size(mut self, party_size: u32) -> Self { 171 | self.inner.party_size = Some(party_size); 172 | self 173 | } 174 | 175 | pub fn party_max(mut self, party_max: u32) -> Self { 176 | self.inner.party_max = Some(party_max); 177 | self 178 | } 179 | 180 | pub fn spectate_secret(mut self, spectate_secret: &str) -> Self { 181 | self.inner.spectate_secret = Some(spectate_secret.to_owned()); 182 | self 183 | } 184 | 185 | pub fn join_secret(mut self, join_secret: &str) -> Self { 186 | self.inner.join_secret = Some(join_secret.to_owned()); 187 | self 188 | } 189 | 190 | pub fn build(self) -> RichPresence { 191 | self.inner 192 | } 193 | } 194 | --------------------------------------------------------------------------------