Code sharing is an important skill to learn as you grow as a developer. Most folks use version control to share code, but sometimes you just want to chat about a function or two. Why spin up a whole branch, when you can pass off a chunk to a buddy over Discord. Here’s how you do it!
Table of Contents
Code Sharing Tips for Discord
If for whatever reason you have the need to share code with someone on Discord (like I do as an educator), you’re probably wondering how you might go about it. Luckily for you, there are more than a few ways to share code; you just need to pick one! Let’s get into it.
Copy & Paste
Perhaps the most common way to share code, especially for beginners, is to copy and paste the code from whatever editor you’re using directly into a channel. Here’s what that might look like:
There’s nothing wrong with sharing code like this, but you should know that there are some issues. First off, Discord partially implements Markdown, which means that any bit of code that can be interpreted as Markdown will be. In this case, the line if __name__ == "__main__
” is being improperly shown with underlines, so there are definitely better options to avoid these kinds of issues.
Screenshots
What I tend to see about as often as the copy and paste solution is a straight up screenshot. Most folks know how to take screenshots, so it’s not a stretch for them to take one of their editor and drop it in the chat as follows:
Of course, the downside of taking a screenshot is that it’s not easy to extract the text. Sure, AI tools exist now to help, but it’s not ideal. If you need to copy the code for whatever reason, you’re in for a bad time. Fortunately, there are some better options.
Fenced Code Blocks
My preferred method of sharing code is to use Markdown fenced code blocks. Using the same code from above, we can represent it with proper syntax highlighting and formatting as follows:
```python import sys def even_odd(x): return "Even" if x % 2 == 0 else "Odd" def exit_with_error(): print('Usage: please input a number') sys.exit(1) def main(args): try: num = int(args[0]) print(even_odd(num)) except (IndexError, ValueError): exit_with_error() if __name__ == "__main__": main(sys.argv[1:]) ```
In other words, we can get proper formatting by adding three backticks above and below our code. To get syntax highlighting, we just have to add the language name right after the backticks at the top. The result is a beautifully rendered bit of code:
One thing I’ll warn about is that Discord includes a 2000 character limit in its messages, so you can’t post excessively large programs in this way. That said, we have other options.
File Attachments
One nice feature about Discord is that you can share entire files. For instance, the same code that we’ve been adding via text can be added as a file attachment by dragging and dropping the file into any channel we ant.
As you can see, the attachment gives this nice embed that looks really similar to copy and pasting the code. An added benefit is that other folks in the chat can basically close off the clutter and download the file if needed. Here’s what it looks like fully expanded:
Again, I’ll mention that Discord has a file size limit of 8 MB. It’s going to be hard to cross that with a text file, but if you’re posting massive logs or something, be aware of the limit. If you have to share a larger file for whatever reason, there’s at least one more option.
Links
If all else fails, you can always upload your file somewhere else and share a link to it. For instance, the code snippet I’ve shared several times in this article is actually from my Sample Programs repo. You can access it from the this link. When you drop something like this into Discord, expect to see something like the following:
I think a common place to share one-off code snippets is Gist. Otherwise, you can use a number of other cloud-based tools like Box, OneDrive, etc.
Summary
Hopefully at this point, you have a feel for the different ways to share code on Discord. To summarize, here are your options:
- Copy & paste
- Screenshot
- Fenced code blocks
- File attachments
- Links
While you’re here, one thing I might mention is that Markdown is a really useful tool for putting together web-based documents. In fact, I like it so much, I made a tool to automate the creation of them in Python called SnakeMD. Why not take a look? Alternatively, check out some of these related articles:
- How to Convert Markdown to a PDF: 3 Quick Solutions
- How to Generate Markdown in Python Using SnakeMD
- Meet Pymon: A Discord Bot That Can Answer Any Question You Want
With that said, that’s all I got! If you liked this article, feel free to show your support. Otherwise, take care!
Recent Posts
Recently, I was thinking about the old Pavlov's dog story and how we hardly treat our students any different. While reflecting on this idea, I decided to write the whole thing up for others to read....
In the world of programming languages, expressions are an interesting concept that folks tend to implicitly understand but might not be able to define. As a result, I figured I'd take a crack at...