5 Ways to Share Code in Discord

5 Ways to Share Code in Discord Featured Image

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:

Sharing Python Code via Copy and Paste in Discord

There’s nothing wrong with sharing code like this, but you should know that there are some issues. First off, Discord partially implements MarkdownOpens in a new tab., 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:

Sharing Python Code as a Screenshot in Discord

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:

Sharing Python Code as a Fenced Code Block in Discord

One thing I’ll warn about is that Discord includes a 2000 character limit in its messagesOpens in a new tab., 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.

Sharing Python Code as a File Attachment in Discord

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:

Sharing Python Code as an Expanded File Attachment in Discord

Again, I’ll mention that Discord has a file size limit of 8 MBOpens in a new tab.. 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 linkOpens in a new tab.. When you drop something like this into Discord, expect to see something like the following:

Sharing Python Code as a Link in Discord

I think a common place to share one-off code snippets is GistOpens in a new tab.. 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:

With that said, that’s all I got! If you liked this article, feel free to show your support. Otherwise, take care!

Coding Tangents (43 Articles)—Series Navigation

As a lifelong learner and aspiring teacher, I find that not all subjects carry the same weight. As a result, some topics can fall through the cracks due to time constraints or other commitments. Personally, I find these lost artifacts to be quite fun to discuss. That’s why I’ve decided to launch a whole series to do just that. Welcome to Coding Tangents, a collection of articles that tackle the edge case topics of software development.

In this series, I’ll be tackling topics that I feel many of my own students have been curious about but never really got the chance to explore. In many cases, these are subjects that I think deserve more exposure in the classroom. For instance, did you ever receive a formal explanation of access modifiers? How about package management? Version control?

In some cases, students are forced to learn these subjects on their own. Naturally, this forms a breeding ground for misconceptions which are made popular in online forums like Stack Overflow and Reddit. With this series, I’m hoping to get back to the basics where these subjects can be tackled in their entirety.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts