WEBVTT
Kind: captions
Language: en

00:00:00.000 --> 00:00:04.120 line:95%
 ♪  ♪ 

00:00:04.160 --> 00:00:06.560 line:95%
 -How can you control 
 your home.. from Discord? 

00:00:06.600 --> 00:00:09.280 line:95%
 That’s exactly what I’ll 
 be doing in this video, 

00:00:09.280 --> 00:00:11.400 line:95%
 showing how to make a 
 discord bot that can control 

00:00:11.440 --> 00:00:15.040 line:95%
 your home. It’s a cool use 
 of APIs, webhooks, and more. 

00:00:15.080 --> 00:00:16.800 line:95%
 To make this bot, 
 you’ll need a couple 

00:00:16.840 --> 00:00:19.080 line:95%
 of things. One of 
 them is.. discord! 

00:00:19.120 --> 00:00:20.200 line:95%
 You need to have 
 an account, but you 

00:00:20.200 --> 00:00:23.480 line:95%
 don’t have to have the desktop version. 
 You can just do it all in the web browser. 

00:00:23.520 --> 00:00:25.920 line:95%
 Another thing you’ll 
 need is an If This Then 

00:00:25.960 --> 00:00:30.240 line:95%
 That account, or IFTTT for 
 short. This is the website 

00:00:30.240 --> 00:00:32.560 line:95%
 that's gonna interact 
 with everything. You’ll also 

00:00:32.560 --> 00:00:36.520 line:95%
 need an IDE, or integrated 
 development environment. 

00:00:36.560 --> 00:00:38.440 line:95%
 This is the part that 
 you’ll write the code in 

00:00:38.480 --> 00:00:40.680 line:95%
 and it does a lot of the 
 backend stuff for you. 

00:00:40.720 --> 00:00:44.160 line:95%
 I’m using Windows, so I’m going to be 
 using the Windows version of PyCharm. 

00:00:44.160 --> 00:00:46.240 line:95%
 You can find a link in 
 the description. It’s main 

00:00:46.240 --> 00:00:48.320 line:95%
 focus is python, which 
 is the language I’m going 

00:00:48.320 --> 00:00:50.640 line:95%
 to code the bot in. The 
 last thing you’ll need is 

00:00:50.640 --> 00:00:52.960 line:95%
 a program called Anaconda, 
 which helps you install 

00:00:52.960 --> 00:00:56.080 line:95%
 python and manage 
 packages. Go ahead and install 

00:00:56.120 --> 00:00:59.320 line:95%
 PyCharm and Anaconda, and 
 make sure you have the accounts for 

00:00:59.360 --> 00:01:02.400 line:95%
 Discord and IFTTT. Then 
 you can open up PyCharm. 

00:01:02.400 --> 00:01:04.080 line:95%
 When you open it [PyCharm] up, you’ll see 

00:01:04.080 --> 00:01:05.720 line:95%
 three options. Choose 
 the one to start a new 

00:01:05.720 --> 00:01:08.280 line:95%
 project, and it'll give 
 you some more options. 

00:01:08.320 --> 00:01:10.960 line:95%
 At the top, you can see the 
 location that the project files 

00:01:10.960 --> 00:01:15.440 line:95%
 are stored at. The end piece is where you can 
 change the name, and I’m just setting mine to be 

00:01:15.440 --> 00:01:20.320 line:95%
 homeControl. The next thing is the environment. 
 An environment allows you to install different 

00:01:20.320 --> 00:01:23.240 line:95%
 packages or whatever 
 to just this project. 

00:01:23.280 --> 00:01:26.040 line:95%
 So if you have other projects that need different packages, 

00:01:26.040 --> 00:01:29.120 line:95%
 the ones you install in this 
 environment will not be in the other ones. 

00:01:29.160 --> 00:01:32.320 line:95%
 Choose “New Environment 
 Using” and in the drop down, you 

00:01:32.320 --> 00:01:36.640 line:95%
 should see Conda. You can leave all of the other 
 settings the same and press the create button. 

00:01:36.640 --> 00:01:40.080 line:95%
 You’ll see PyCharm create the environment, 
 which could take a couple seconds, but it 

00:01:40.080 --> 00:01:44.560 line:95%
 will eventually load. Then, you’ll be greeted with 
 a few things. One of them is the main area where 

00:01:44.560 --> 00:01:47.240 line:95%
 your code goes. They put 
 in an example project, which 

00:01:47.280 --> 00:01:50.240 line:95%
 you can just select and delete. 
 There might also be some 

00:01:50.240 --> 00:01:53.120 line:95%
 popups or tutorials, and 
 you can close those out too. 

00:01:53.160 --> 00:01:55.760 line:95%
 One more thing is that 
 there might be a little loading 

00:01:55.760 --> 00:01:59.120 line:95%
 bar at the bottom, and 
 this is what's indexing all of the 

00:01:59.160 --> 00:02:01.840 line:95%
 packages, and you should 
 wait until that goes away. 

00:02:01.840 --> 00:02:06.480 line:95%
 Now that that’s done, you can start coding! The 
 way discord can integrate with your code is by 

00:02:06.480 --> 00:02:08.480 line:95%
 their own package that they 
 make. 

00:02:08.520 --> 00:02:11.520 line:95%
 To get it working with your codego 
 to the first line and type “import 

00:02:11.520 --> 00:02:14.760 line:95%
 discord”. You’ll notice 
 a red line underneath. 

00:02:14.760 --> 00:02:16.720 line:95%
 That indicates an error, 
 and this is because the 

00:02:16.720 --> 00:02:19.560 line:95%
 discord package needs to 
 be installed. To do this, 

00:02:19.560 --> 00:02:22.400 line:95%
 you can open 
 up the Anaconda Prompt app 

00:02:22.400 --> 00:02:27.840 line:95%
 which was installed with conda. You can select the 
 project that you are working on by typing activate 

00:02:27.840 --> 00:02:30.080 line:95%
 and then the 
 name of the project. 

00:02:30.120 --> 00:02:33.280 line:95%
 Now in the parenthesis, you'll 
 see than name. Then, you can install 

00:02:33.320 --> 00:02:39.160 line:95%
 the discord package by typing 
 pip or p-i-p install discord. Let 

00:02:39.200 --> 00:02:42.000 line:95%
 that load, and then it should 
 let you type in some more stuff. 

00:02:42.000 --> 00:02:44.400 line:95%
 While you’re at it, you 
 might want to install another 

00:02:44.400 --> 00:02:46.560 line:95%
 package called requests. 
 You’ll need it later. 

00:02:46.560 --> 00:02:49.200 line:95%
 Once those install, you 
 can close out Anaconda 

00:02:49.200 --> 00:02:51.840 line:95%
 and go back to PyCharm. 
 Now the discord thing at 

00:02:51.840 --> 00:02:54.160 line:95%
 the top shouldn't have 
 a red line underneath. 

00:02:54.160 --> 00:02:58.640 line:95%
 And you can also import the requests 
 module that was installed earlier. 

00:02:58.640 --> 00:03:00.880 line:95%
 Then, go ahead and 
 add some blank lines. 

00:03:00.880 --> 00:03:04.880 line:5%
 So, if you’ve used a discord bot before, 
 you’ll know the commands need a prefix. 

00:03:04.880 --> 00:03:09.400 line:95%
 This can be anything, but it indicates that 
 this is going to be a command for the bot. 

00:03:09.440 --> 00:03:14.040 line:95%
 This one is no different. We need a 
 way to tell Discord what the prefix is. 

00:03:14.080 --> 00:03:25.960 line:95%
 You can do this by typing 
 bot = commands.Bot(). 

00:03:25.960 --> 00:03:36.200 line:95%
 In the parenthesis, 
 type command_prefix=’!’. 

00:03:36.240 --> 00:03:38.200 line:95%
 This is going to set the 
 command prefix, and I 

00:03:38.240 --> 00:03:41.480 line:95%
 just chose an exclamation mark. 
 The written dicumentation for this 

00:03:41.520 --> 00:03:44.720 line:95%
 is all in the description, so 
 you might want to check that out. 

00:03:44.760 --> 00:03:48.040 line:95%
 Anyway, you’ll notice underneath 
 commands there's a red underline, 

00:03:48.080 --> 00:03:51.440 line:95%
 and that is because it cannot 
 find that module. That’s because it 

00:03:51.440 --> 00:03:54.880 line:95%
 hasn't been imported! At 
 the top, you can add a line and put 

00:03:54.920 --> 00:03:59.200 line:95%
 from discord.ext (which is the 
 extension of the discord 

00:03:59.200 --> 00:04:04.080 line:95%
 package) and then import commands. 
 Now, the commands 

00:04:04.080 --> 00:04:07.280 line:95%
 the commands prefix is defined! 
 The next step is to make the bot’s 

00:04:07.280 --> 00:04:16.840 line:95%
 first command. Add two blank lines after 
 the commands thing, and type @bot.command(). 

00:04:16.880 --> 00:04:21.840 line:95%
 This tells the program that this is going to 
 be a command. Then, make a new line 

00:04:21.920 --> 00:04:27.880 line:95%
 and type async def and then the 
 name of the command, which I'm just 

00:04:27.920 --> 00:04:28.800 line:95%
 gonna do test. 

00:04:28.840 --> 00:04:34.040 line:95%
 After that, do <i> ctx</i>  inside of 
 parenthesis, and then a colon. 

00:04:34.040 --> 00:04:36.520 line:95%
 Then, ress enter and 
 indent the line by pressing 

00:04:36.560 --> 00:04:38.760 line:95%
 the tab key. Now 
 everything inside of the 

00:04:38.760 --> 00:04:43.400 line:95%
 async define thing is going to run when 
 the command is triggered from discord. 

00:04:43.400 --> 00:04:47.960 line:95%
 We want to make it somehow control a smart 
 home device. One of the ways we can do this 

00:04:47.960 --> 00:04:53.160 line:95%
 is through a webhook. The webhook is basically 
 a URL that you go to in a browser and it will 

00:04:53.160 --> 00:04:58.400 line:95%
 trigger something. The request module that was 
 imported earlier is going to be used for this. 

00:04:58.440 --> 00:05:02.640 line:95%
 Type <i> requests.post</i>  and 
 then a set of parenthesis with 

00:05:02.640 --> 00:05:04.600 line:95%
 single quotes in the 
 middle. And inside of 

00:05:04.600 --> 00:05:07.280 line:95%
 the quotes, we’ll need 
 to get the webhook URL. 

00:05:07.320 --> 00:05:10.320 line:95%
 The service we are going 
 to use is IFTTT which again 

00:05:10.320 --> 00:05:13.240 line:95%
 stands for if this than 
 that. It’s going to allow 

00:05:13.240 --> 00:05:17.480 line:95%
 you to have the discord bot interact with your 
 smart home. Go ahead and make an account and 

00:05:17.480 --> 00:05:22.360 line:95%
 go to the home page. Click “create” and you’ll 
 see the page where you can make what they call 

00:05:22.360 --> 00:05:27.040 line:95%
 applets. If you click on 
 the "if this" part, you can 

00:05:27.040 --> 00:05:30.520 line:95%
 see a list of services and 
 I'm gonna choose webhooks. 

00:05:30.520 --> 00:05:34.200 line:95%
 Remember, discord will 
 trigger this webhook part. 

00:05:34.240 --> 00:05:37.920 line:95%
 Click receive a web 
 request, then click connect. 

00:05:37.960 --> 00:05:42.160 line:95%
 Now you can type the event 
 name, and I’ll just do test. Press 

00:05:42.160 --> 00:05:45.200 line:95%
 the next button, and now for 
 the “then that” part, this is what 

00:05:45.240 --> 00:05:49.960 line:95%
 you want this to actually happen like turn on 
 a light. I have my lamp on a smart outlet, so I’m 

00:05:49.960 --> 00:05:52.120 line:95%
 going to choose the service 
 for that. I want to have 

00:05:52.160 --> 00:05:54.080 line:95%
 the lamp turn off when I 
 run the command. 

00:05:54.080 --> 00:05:59.480 line:95%
 Now you can press continue and 
 finish. The next step is to get the URL. Click on 

00:05:59.480 --> 00:06:05.560 line:95%
 the webhooks logo, then go to documentation. Now, 
 go to where it says event and change it with the 

00:06:05.560 --> 00:06:12.560 line:95%
 name of the trigger, mine being test. Now, you 
 can copy this link and paste it into your code! 

00:06:12.600 --> 00:06:16.800 line:95%
 Now that you have the URL, I would add a 
 way of telling you that the command was ran. 

00:06:16.840 --> 00:06:19.440 line:95%
 You can do this by 
 typing print and then a set 

00:06:19.480 --> 00:06:22.440 line:95%
 of parenthesis and a set 
 of single quotes on a new line, 

00:06:22.440 --> 00:06:27.000 line:95%
 and in the quotes type the message that you 
 want. It will show up on the output console. 

00:06:27.000 --> 00:06:33.080 line:95%
 The last thing you’ll want is the key for the 
 discord bot. Go to discord.com/developers and 

00:06:33.080 --> 00:06:38.440 line:95%
 you’ll see the developer portal. Go up here 
 to applications. These applications represent 

00:06:38.440 --> 00:06:43.720 line:95%
 something, like a bot, that you’ve built. Press 
 new application and type in a name to the popup. 

00:06:43.720 --> 00:06:48.360 line:95%
 I’m going to make mine Home Control. Here is 
 the main information for the application, but 

00:06:48.360 --> 00:06:53.720 line:95%
 you need a bot! Go over to bot on the left, then 
 add bot. Here is what the actual bot will look 

00:06:53.720 --> 00:06:57.960 line:95%
 like and you can add a profile image or change the 
 bot’s username. You can see this token area, and 

00:06:57.960 --> 00:06:59.920 line:95%
 this is what allows 
 your code to talk with the 

00:06:59.920 --> 00:07:02.880 line:95%
 bot. Press copy and 
 then go back to your code, 

00:07:02.880 --> 00:07:08.760 line:95%
 and type: <i> bot.run</i>  and then a set of 
 parenthesis and a set of single quotes, 

00:07:08.800 --> 00:07:11.440 line:95%
 and paste that key [token] into the quotes. 

00:07:11.480 --> 00:07:17.040 line:95%
 By the way, you should not share this 
 or the IFTTT key things with anyone! 

00:07:17.080 --> 00:07:20.120 line:95%
 Okay, now that you have both of 
 those keys pasted into your code, 

00:07:20.120 --> 00:07:23.840 line:95%
 you can press run! It’s this little 
 green play button at the top right. 

00:07:23.880 --> 00:07:27.480 line:95%
 Once you see this console open 
 up, the bot is now running! 

00:07:27.480 --> 00:07:31.320 line:95%
 Okay, now you need to get the bot into 
 the server. If you go back to the discord 

00:07:31.320 --> 00:07:37.360 line:95%
 developer portal and go to the OAuth2 tab, this 
 is where you can invite the bot to a server. 

00:07:37.400 --> 00:07:40.360 line:95%
 Scroll down to scopes 
 and chech bot, then scroll 

00:07:40.360 --> 00:07:43.320 line:95%
 down more to get the 
 permissions for the bot. It 

00:07:43.320 --> 00:07:47.680 line:95%
 doesn’t really need many permissions, but 
 I would allow it to read message history. 

00:07:47.720 --> 00:07:51.560 line:95%
 Something you can add to the bot later is the 
 ability to send messages, so we’ll check that 

00:07:51.600 --> 00:07:54.520 line:95%
 box too. Then, press copy 
 and then you can paste this 

00:07:54.560 --> 00:07:57.320 line:95%
 link into your web browser. 
 You can select a server to 

00:07:57.320 --> 00:08:00.000 line:95%
 allow your bot in to, 
 review the permissions, and 

00:08:00.000 --> 00:08:02.680 line:95%
 do the capcha. Then, 
 the bot will join the server! 

00:08:02.680 --> 00:08:05.000 line:95%
 Now, you can run the test 
 command that we set up. 

00:08:05.040 --> 00:08:07.560 line:95%
 Remember, the prefix 
 was the exclamation mark, 

00:08:07.560 --> 00:08:13.600 line:95%
 and the command name was test. So if I run the 
 test command, the bot should turn off my light! 

00:08:13.640 --> 00:08:18.880 line:95%
 If you go back to pycharm, you can see the thing 
 that I told it to print shows up in the console. 

00:08:18.920 --> 00:08:22.120 line:95%
 So now that the bot is working, you 
 can start adding more functions! 

00:08:22.120 --> 00:08:26.480 line:95%
 The first thing that I’ll add is a way for 
 the bot to respond in the Discord chat. 

00:08:26.520 --> 00:08:30.720 line:95%
 You can do this by adding another 
 line to the command and typing <i> await</i>  

00:08:30.760 --> 00:08:36.320 line:95%
 <i> ctx.send</i>  and then a set of 
 parenthesis and a set of single quotes 

00:08:36.320 --> 00:08:39.440 line:95%
 and in the quotes, put 
 the message you want the bot to say. 

00:08:39.480 --> 00:08:42.880 line:95%
 Now if you stop and rerun 
 by pressing this arrow with 

00:08:42.880 --> 00:08:45.160 line:95%
 the little stop icon at 
 the top right, it starts up 

00:08:45.160 --> 00:08:47.120 line:95%
 the bot with the new 
 code. And if you run the 

00:08:47.120 --> 00:08:49.840 line:95%
 command, the bot will say 
 that message in the chat. 

00:08:49.880 --> 00:08:54.200 line:95%
 If you want to add more commands, you can copy 
 and paste the first command over and over again. 

00:08:54.200 --> 00:08:58.840 line:95%
 Then, change the command name (I’m changing it 
 from test to run). Then, you have to make another 

00:08:58.840 --> 00:09:02.600 line:95%
 IFTTT applet. And for 
 webhooks, make sure you choose 

00:09:02.640 --> 00:09:04.920 line:95%
 a different event name. 
 I’m going to make this one 

00:09:04.920 --> 00:09:11.240 line:95%
 start our vacuum. Then, change the event name in 
 the URL. Mine goes from test to roomba. You can 

00:09:11.240 --> 00:09:17.120 line:95%
 also change what it prints and the message that 
 it sends. Now, you can have multiple commands! 

00:09:17.160 --> 00:09:20.920 line:95%
 So that’s how you can make discord 
 interact with your home. It uses APIs, 

00:09:20.920 --> 00:09:24.800 line:95%
 webhooks, and more! You can do so 
 much with this, like playing music, 

00:09:25.960 --> 00:09:28.800 line:95%
 ♪ [Upbeat Music fades in] ♪ 

00:09:28.880 --> 00:09:30.880 line:95%
 making your lights change color, 

00:09:33.480 --> 00:09:35.200 line:95%
 and turning off other devices. 

00:09:35.240 --> 00:09:37.480 line:95%
 [Fan noise] 

00:09:36.920 --> 00:09:37.600 align:end position:100% line:95%
 [Click] 

00:09:37.480 --> 00:09:38.160 line:95%
 [Fan noise fades out] 

00:09:38.200 --> 00:09:40.560 line:95%
 That’s pretty much it for this 
 video, thank you for watching! 

00:09:40.600 --> 00:09:44.400 line:95%
 ♪ [Upbeat Music] ♪ 

00:09:44.440 --> 00:09:51.720 line:95%
 ♪  ♪ 

