<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DanDev95</title>
    <description>The latest articles on DEV Community by DanDev95 (@dandev95).</description>
    <link>https://dev.to/dandev95</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F550336%2Fd9f684da-de63-429d-ab1a-7b0066c9722c.png</url>
      <title>DEV Community: DanDev95</title>
      <link>https://dev.to/dandev95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dandev95"/>
    <language>en</language>
    <item>
      <title>Discord's new "member since" feature</title>
      <dc:creator>DanDev95</dc:creator>
      <pubDate>Thu, 22 Sep 2022 11:28:45 +0000</pubDate>
      <link>https://dev.to/dandev95/discords-new-member-since-feature-576l</link>
      <guid>https://dev.to/dandev95/discords-new-member-since-feature-576l</guid>
      <description>&lt;p&gt;Discord recently rolled out a new feature to their stable build. The way it works is however a bit strange in my opinion, and shouldn't be on the stable build already. Because of this, it has left users confused. It isn't completely obvious how to enable this feature for your own profile.&lt;/p&gt;

&lt;p&gt;To enable this new feature publicly on your own Discord profile, one must go through a few steps. Continue reading to learn how to.&lt;/p&gt;

&lt;p&gt;If you want to watch a video about the topic instead, watch this tutorial&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/QCv5TVvu1P0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  How to enable "member since" on your Discord profile
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hPyeVzsm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pvch0bcszq9s8x8bhum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hPyeVzsm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pvch0bcszq9s8x8bhum.png" alt="Image description" width="880" height="433"&gt;&lt;/a&gt;&lt;br&gt;
This is the section you will need to navigate into. Here, you are able to add connections to your Discord account. Examples of such are Spotify, Github &amp;amp; YouTube. Looking through this list, you won't be able to find any setting that looks like it'll toggle the "member since" feature on your profile.&lt;/p&gt;

&lt;p&gt;What you need to do is to add a new connection. It doesn't matter which one it is. But what does matter is that it has to be displayed on your profile, and thus cannot be hidden.&lt;/p&gt;

&lt;p&gt;I've selected YouTube and authorised Discord to view my YouTube channel. After this, my profile now looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZATmhbp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oi5rd00slg6y8u58p02w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZATmhbp7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oi5rd00slg6y8u58p02w.png" alt="Image description" width="597" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Add a user to a guild with Discord OAuth2 in Python using Requests</title>
      <dc:creator>DanDev95</dc:creator>
      <pubDate>Thu, 05 Aug 2021 10:09:27 +0000</pubDate>
      <link>https://dev.to/dandev95/add-a-user-to-a-guild-with-discord-oauth2-in-python-using-requests-595f</link>
      <guid>https://dev.to/dandev95/add-a-user-to-a-guild-with-discord-oauth2-in-python-using-requests-595f</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/TvsjIvT4jCI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;With the lack of tutorials regarding Discord OAuth2 and how it works, I decided to write my own guide as well as record a video along with it. This article will show how Discord's OAuth2 can be used in order to add a Discord user to a guild. We will be using Python and the requests library for this guide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing reqests
&lt;/h2&gt;

&lt;p&gt;Since requests is the only library we will be using, we have to import it at the top of our code.&lt;br&gt;
&lt;code&gt;import requests&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up our variables
&lt;/h2&gt;

&lt;p&gt;We are going to setup 4 variables that will be used later in our code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

API_ENDPOINT = 'https://discord.com/api/v8'
CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URI = "https://google.com"
```
For the {% raw %}`CLIENT_ID` and `CLIENT_SECRET`, we have to get these from [Discord's Developer Portal] 
(https://discord.com/developers/applications).

## Creating our Discord application
We need to create a new application, and give it a name. ![Screenshot 2021-08-05 at 11.40.06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/070lcvkhavbet5hrj6b2.png)

After creating our application, we can click on the OAUTH2 tab to find our `CLIENT_ID` and `CLIENT_SECRET`. Copy these two values and paste them at the variable definitions above.
 ![Screenshot 2021-08-05 at 11.42.12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w42r81w39sz2sb2c2e6o.png)

### Setting up our redirect URI
As you probably noticed, we have a `REDIRECT_URI` too. This is the URI that we will be using to get the code from our user, that we then can use to exchange it for an actual access_token. But we have to add this URI in our Developer Portal. The URI does not matter, it can be anything. But we have to allow it.
![Screenshot 2021-08-05 at 11.48.03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n3c8xyxzligxhjoj9qu.png)

## Writing our `exchange_code`function.
This function will allow us to convert the code we get from the user into an access_token, which we then can use to add the user to a guild.
```py
def exchange_code(code):
  data = {
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'grant_type': 'authorization_code',
    'code': code,
    'redirect_uri': REDIRECT_URI
  }
  headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
  r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
  r.raise_for_status()
  return r.json()
``` 
Here, we send along our application info, and return the json object that we get back from Discord.

## Writing our `add_to_guild`
This function is just an example of what we can do with the newly acquired access_token. For a full list of what you can do, check out the [Discord OAUTH2 Documentation] (https://discord.com/developers/docs/topics/oauth2) 
```py
def add_to_guild(access_token, userID, guildID):
    url = f"{API_ENDPOINT}/guilds/{guildID}/members/{userID}"
    botToken = "ODcyMjMyMjQ5ODYzNTkzOTk0.YQm3lQ.yB3pFepBcy7K1gQVrBxNfYf_Bdk"
    data = {
    "access_token" : access_token,
    }
    headers = {
    "Authorization" : f"Bot {botToken}",
    'Content-Type': 'application/json'
    }
    response = requests.put(url=url, headers=headers, json=data)
    print(response.text)
```
This function takes in the access_token, the guildID and the userID of the user we want to add to the guild. It also requires a bot token, which you can get from the developer portal. 
![Screenshot 2021-08-05 at 11.55.09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cg2pj8fkik6nwnpto9yg.png)
We send along this bot token in our headers, and then print out the response we get from Discord. If everything went well, this should be an object with info about the user.

## Finishing up our code
Now, all there is left to do is to run our functions, in the correct order.
```py
code = exchange_code('')['access_token']
add_to_guild(code, 'USER_ID', 'GUILD_ID')
```
So what do we pass in to the `exchange_code` function? Well this is the part where the user has to go to the authorize link and authorize our application.
`https://discord.com/oauth2/authorize?response_type=code&amp;amp;client_id=157730590492196864&amp;amp;scope=identify+guilds.join&amp;amp;state=15773059ghq9183habn&amp;amp;redirect_uri=https%3A%2F%2Fgoogle.com&amp;amp;prompt=consent`
You have to plug in your own client_id and the redirect URI you decided to use.
![Screenshot 2021-08-05 at 12.01.32](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcftw0bjg3vixuclckdg.png)
After authorizing, the user will be sent to our redirect URI. If you look at the uri, you'll find the code that we are after.
![Screenshot 2021-08-05 at 12.02.40](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mfn4itimodb4hocek62.png)
Copy this code and paste it inside the `exchange_code` function.

## Running our code
Before our code can actually work. We have to invite our Discord bot to the server we wish to add the user to.
`https://discord.com/oauth2/authorize?client_id=123456789012345678&amp;amp;scope=bot+applications.commands`
Again, you have to plug in your own client_id to invite your own bot.

Once it is in the server, everything is ready to work. If you want to try it out, leave the server (transfer ownership to someone else if it is your own), and then run the code with your own code and user id. If you have done everything correctly, the bot will add you back to the guild.

## Done
That is all there is to it. This is my first ever article on dev.to, it was fun writing it and I look forward to writing more in the future!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>discord</category>
      <category>python</category>
      <category>oauth2</category>
    </item>
  </channel>
</rss>
