This repository was archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Updated README.md with instructions on making a tokenSafe main.swift -L33t #66
Open
fuguesoft
wants to merge
1
commit into
Azoy:rewrite
Choose a base branch
from
fuguesoft:rewrite
base: rewrite
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,153 @@ | ||
| # Sword - A Discord library for Swift | ||
|
|
||
| As the effort of Sword is gearing towards 1.0, I've decided the push forward the development of the rewrite branch as the main branch. | ||
| As the effort of Sword is gearing towards 1.0, I've decided the push forward the development of the rewrite branch as the main branch. | ||
|
|
||
| # Sword - A Discord Library for Swift | ||
|
|
||
| [](https://swift.org) [](https://travis-ci.org/Azoy/Sword) [](https://github.com/Azoy/Sword/releases) | ||
|
|
||
| ## Requirements | ||
| 1. macOS, Linux, iOS, watchOS, tvOS (no voice for iOS, watchOS, or tvOS) | ||
| 2. Swift 4.0 | ||
| 3. libsodium (if on macOS or Linux) | ||
|
|
||
| ## Installing libsodium | ||
| ### macOS | ||
| Installing libsodium is really easy on mac as long as you have [homebrew](https://brew.sh). After that is installed, all you have to do is `brew install libsodium`. That's it! | ||
|
|
||
| ### Linux | ||
| This depends on the version of Ubuntu you are running, so I made a nice table here: | ||
|
|
||
| | Ubuntu 14.04 | Ubuntu 16.04 | | ||
| |:-----------------------------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------:| | ||
| | `sudo -E add-apt-repository -y ppa:chris-lea/libsodium && sudo apt-get update && sudo apt-get install -y libsodium-dev` | `sudo apt-get update && sudo apt-get install -y libsodium-dev` | | ||
|
|
||
| It's easier to copy and paste that command right into shell, and follow any on screen instructions if needed so. | ||
|
|
||
| ## Adding Sword | ||
| ### Swift Package Manager | ||
| In order to add Sword as a dependency, you must first create a Swift executable in a designated folder, like so `swift package init --type executable`. Then in the newly created Package.swift, open it and add Sword as a dependency | ||
|
|
||
| ```swift | ||
| // swift-tools-version: 4.0 | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "YOUR_BOT_NAME_HERE", | ||
| dependencies: [ | ||
| .package(url: "https://github.com/Azoy/Sword", .branch("master")) | ||
| ], | ||
| targets: [ | ||
| .target( | ||
| name: "YOUR_BOT_NAME_HERE", | ||
| dependencies: ["Sword"] | ||
| ) | ||
| ] | ||
| ) | ||
| ``` | ||
|
|
||
| Before you run the bot, you'll want to take steps to protect your "token" aka your bot's unique key to login to Discord. If this key is leaked, people can cause irreparable damage to any servers the bot has joined. | ||
| To prevent this from happening, we will use a .json file to store our token and tell the bot to access it. | ||
|
|
||
| First locate your bot's executable directory. This should be the same as your build directory in a folder like /Debug. If you're not sure where it is, use the commented lines below to find it or ask the dev where this is. | ||
|
|
||
| Once inside, create a file called config.json. | ||
|
|
||
| Next, write this in the file:```json | ||
| { | ||
| "token": "YOUR_BOT'S_UNIQUE_TOKEN_HERE", | ||
| "cmdPrefix": "YOUR_DESIRED_COMMAND_PREFIX_HERE", | ||
| }``` | ||
|
|
||
| After that, open Sources/main.swift and remove everything and replace it with the example below. | ||
|
|
||
| ```swift | ||
| import Sword | ||
| import Foundation | ||
|
|
||
| //DEFINING A STRUCT TO INTERPRET CONFIG.JSON FILE | ||
|
|
||
| struct Config: Codable { | ||
| let token: String | ||
| let cmdPrefix: String | ||
| } | ||
| //ERROR HANDLING | ||
| enum ConfigGetError: Error { | ||
| case cannotMakeURL | ||
| } | ||
|
|
||
| //GETTING THE BOT TOKEN AND CMD PREFIX FROM CONFIG.JSON | ||
|
|
||
| func getBotConfig() throws -> Config { | ||
|
|
||
| //COMMENTED LINES TO PRINT YOUR EXECUTABLE'S DIRECTORY | ||
| // print(Bundle.main.executablePath!) | ||
| // print(Bundle.main.bundlePath) | ||
| guard let configFileURL = Bundle.main.url(forResource: "config", withExtension: "json") else { | ||
| print("Can't make URL.") | ||
|
|
||
| throw ConfigGetError.cannotMakeURL | ||
| } | ||
| // print(configFileURL) | ||
| let configFileData = try Data(contentsOf: configFileURL) | ||
| return try JSONDecoder().decode(Config.self, from: configFileData) | ||
| } | ||
|
|
||
|
|
||
| //GETTING THE BOT ONLINE | ||
|
|
||
| let config = try getBotConfig() | ||
| let myCommandPrefix = config.cmdPrefix | ||
| let options = ShieldOptions( | ||
| prefixes: ["\(cmdPrefix)"] | ||
| ) | ||
| let myBot = Shield(token: config.token, shieldOptions: options) | ||
|
|
||
| myBot.editStatus(to: "online", playing: "with Sword!") | ||
|
|
||
|
|
||
| //BASIC TEST COMMANDS | ||
|
|
||
| myBot.register("ping", message: "Pong!") | ||
|
|
||
| myBot.register("echo") { msg, args in | ||
| msg.reply(with: args.joined(separator: " ")) | ||
| } | ||
|
|
||
| myBot.connect() | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ### CocoaPods | ||
| Adding Sword to your iOS, watchOS, or tvOS application is easier than ever with CocoaPods. All you have to do is add Sword as a dependency to your Podfile, something like this: | ||
|
|
||
| ```ruby | ||
| target 'yourappnamehere' do | ||
| use_frameworks! | ||
| pod 'Sword' | ||
| end | ||
| ``` | ||
|
|
||
| Then all you have to do is `pod install` and you're ready to go. | ||
|
|
||
| ## Running the bot (SPM) | ||
| Build the libraries with `swift build`, then type `swift run` | ||
|
|
||
| ## Running the bot in Xcode (SPM) | ||
| To run the bot in Xcode, you first have to compile the libraries with `swift build`. Then to build the xcode project, type `swift package generate-xcodeproj`. Finally, type `open yourswiftexecutablehere.xcodeproj`, look at the top and follow the steps below | ||
|
|
||
|  | ||
|
|
||
|  | ||
|
|
||
|  | ||
|
|
||
| Then click the play button! | ||
|
|
||
| ## Links | ||
| [Documentation](http://sword.azoy.me) - (created with [Jazzy](https://github.com/Realm/Jazzy)) | ||
|
|
||
| Join the [API Channel](https://discord.gg/99a3xNk) to ask questions! | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This JSON doesn't appear to be formatted in the final preview.