Skip to content

Instantly share code, notes, and snippets.

@Zzz9194
Last active April 4, 2022 02:05
Show Gist options
  • Save Zzz9194/7c1340b0f45b45827b48c578c9e40f3a to your computer and use it in GitHub Desktop.
Save Zzz9194/7c1340b0f45b45827b48c578c9e40f3a to your computer and use it in GitHub Desktop.
Guide to certain elements of discord.py people may be confused about

Get cool colors for your embeds!

discord.Color and discord.Colour are BOTH acceptable

Discord.py has a bunch of colors pre-stored as classmethods

import discord

# Maybe use a hex value??!!?!?!
sky_blue = discord.Color(0xCCFFFF) # 0x + hex value

# Or grab the RED TO THE GREEN TO THE BLUE values
yellow = discord.Color.from_rgb(r=255,g=255,b=51) # You've got the color yellow! AND ITS HOMEMADE!!!

# Or just like, use intellisense and find something built-in -_-
built_in_red = discord.Color.red()

The only real reason anyone uses discord.Color is to set the color for discord.Embed's

from discord import Embed, Color

my_embed = Embed(
  color = Color.red()
)

And then your embed comes with a nice little colored line at the side

(It's got no other values so it's super tiny and weird)

BAIII

Discord Embeds Done Properly

Creating an embed

To create an embed you simply create a new discord.Embed class

import discord
new_embed = discord.Embed()

Embed object properties

new_embed.title = "My Embed Title" 

new_embed.description = "Embed Description"

new_embed.url = "URL hyperlink that will come in the embed title"

import datetime
new_embed.timestamp = datetime.datetime.now() # Embed timestamp shows up in the footer of your embed

new_embed.color = discord.Color.red() # or discord.Color(your_hex)

Your embed would look like this if you did all that (and gave a valid URL)

Embed One

Class Functions

Fields

You can add up to 25 fields in a single embed before you reach the limit

new_embed.add_field(
  name = "Field Name",
  value = "Field Value",
  inline = True
)
new_embed.add_field(
  name = "Field Name Two",
  value = "Field Value Two",
  inline = True
)

To make your embed follow a single downwards line you just set inline=False instead of True

Your embed will show up like this:

Embed Two

You can also add a field at a certain index (location) within the embed using insert_field_at

And you can edit fields with set_field_at

Footer

Lets set an embed footer...

new_embed.set_footer(
  text = "I am the foot! Zz",
  icon_url = "Some image URL"
)

And now we have an 'footer' in our embed, you will notice that the timestamp is still there, but is separated by a this is just discord standard and can not be changed as of yet

Embed w/ footer

Author, Image and Thumbnail

Now lets add our author, our image AND our thumbnail!

new_embed.set_author(
  name = "Your Humble Author Zz",
  url = "When you click on the author, you can get redirected to whatever URl you put here",
  icon_url = "The image that shows up next to the author name"
)

new_embed.set_thumbnail(url="image url")

new_embed.set_image(url="another image url")

And our magical embed, so many beautiful faces 😍

Embed w/ imgs, thumbs and author

For more detailed and other miniature tasks you may want to use for your embeds refer to the documentation If you want to know how to change your embed color look at the default color library within the discord.py module!

**~ Peace out ✌️ ~**

Sending files on discord via discord.py

fairly easy

While your file is within the current working directory

my_file_name = "names.txt"
my_file = open(my_file_name,mode='rb') # Open in the read in binary mode (rb)

my_disc_file = discord.File(
  fp = my_file,
  filename = "names_file.txt" # This is what it will be called when you send it,
  spoiler = False # Send as a spoiler or not
)

await channel.send(file=my_disc_file) # If sending just one file

await channel.send(
  files = [
    my_disc_file_one,
    my_disc_file_two
  ]
) 

So sending a number of files just define files as a list of discord.Files Otherwise send a single discord.File as file

Setting discord permissions

Creating a permissions object

import discord

perms = discord.Permissions()

Now within the object you can set permissions to either True, False, or None (For neutral)

  • To find the names of the permissions you want to set, look in your discord channel permissions
  • Find the name of the permission you wish to alter E.g. Read Messages
  • Take that, replace the spaces ' ' with underlines '_' and make everything lowercase:
    • Read Messages -> read_messages

There's your permission variable

Now set it as an attirbute of the permissions object to whatever value you want

perms.read_messages = True
perms.manage_messages = False

await channel.edit(
  permissions = perms
)

Change your bot users' activity

Firstly you need to create an activity object

import discord

# You have built in activities like 

my_special_activity = discord.Game(name="PAC MAN") # Returns Playing PAC MAN

# Or you can have different activity types like watching 

my_very_stalky_activity = discord.Activity(
  name=" you",
  type = discord.ActivityType.watching
)
# Comes out as Watching you 👀

You also have activity types like listening and more, you can see them here

In order to actually change the activity of your bot user you can just use change_presence like so:

dnd = discord.Status.dnd

await client.change_presence(
  status = dnd,
  activity = my_very_stalky_activity
)

Thank you for tuning in, till next time uwu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment