Attendance Bot Tutorial
Attendance Bot Tutorial
Hannah Chung
Lena Kwan
Prerequisites 2
Steps 3
Create a Discord Server 3
Implement Discord Bot (Discord end) 4
Create a Discord Bot 6
Creating a Server Instance 9
Create DB (DynamoDB) 13
Create Lambda Function POST 14
Create Lambda Function GET from DDB 17
Create API Gateway 19
Store Attendance 23
Get Attendance -Lambda Function 25
Connect Bot to DynamoDB Using Lambda 28
Create FrontEnd (S3) 31
Route to a domain (Route53) 33
References 37
1
Prerequisites
● Node JS installed (v8.0.0 and up).
2
Steps
2) After downloading it, install the app and log in using the account you created
above.
4) Click Create My Own and create your own with a name you want and a
5) Once you are done, you’ll be able to see the following screen
3
2. Implement Discord Bot (Discord end)
1) Go back to your Discord desktop app. Click New Application and enter the
2) Enter the general information about your discord bot. This is where you can
set an image for the application and copy the client ID and secrets
3) Add the bot to your server. To do this you will need to click on OAuth2, click
4
4) Paste the URL into a browser and select the server that you would like to add
5
7) Grab the client secret token from here! We will need it at the end of Part 3!
1) Let’s create a Discord Bot using AWS Cloud9. Go to AWS Cloud9 and create
2) Once this is launched, open the IDE. From the AWS Cloud9 menu, select
credentials”
6
3) Open the bash shell, run “aws configure” and enter your credentials.
4) First, check the node version in your Cloud9. It should be v10.23.0. If it does
not have node installed or has a lower version of node, you have to install it or
mkdir discord-greeter-bot
npm init -y
7
Then you’ll see a package.json file that is just created. This is a manifest file,
for Node based projects where you can state dependencies to install.
8) Now create a file called .env by typing in touch .env and put the following
line in the file by typing in vi .env. Do you remember that I said we need a
bot token later in this part? This is where it is! We’ll put the bot token here.
DISCORD_TOKEN=YOUR-BOT-TOKEN
8
8) Let’s create our bot script now. Go back to the project folder and create a file
Client.on('ready', () => {
console.log(`Now you logged in as
${Client.user.tag}`);
});
1) In this part, we will host the Discord bot on EC2 instance. Go to EC2 instance service
9
2) Select ‘Ubuntu Server 18.04 LTS (HVM), SSD Volume Type -
ami-07ebfd5b3428b6f4d’ AMI.
4) Click ‘Launch’ at the next page and wait for it to be launched. This may take a few
minutes or more.
5) Let’s name this instance DiscordBot! Check the checkbox of DiscordBot and click
6) Go to Cloud9, open the bash shell and ssh into ec2 using the commands from above.
7) Once you are connected to the ec2 instance, use the following commands
10
sudo apt-get install npm
8) Now exit out of the ec2 instance and try the following commands to zip the project
9) Ssh back into your ec2 instance and check if it’s transferred well.
unzip mine.zip
cd discord-greeter-bot
npm install
12) Change your bot.js to the following codes. You have to replace you_bot_id to your
bot’s id.
// Run dotenv
require('dotenv').config();
Client.on('ready', () => {
console.log(`Now you logged in as ${Client.user.tag}`);
});
11
Client.on('message', async msg => {
let bot_called = '';
if (msg.content.startsWith('<@&you_bot_id>')){
';
bot_called = '<@&you_bot_id>
} else if (msg.content.startsWith('<@!you_bot_id>')){
bot_called = '<@!you_bot_id> ';
} else {
return;
}
if (command == 'hi') {
return msg.reply('hello');
}
else if (command.startsWith('link me to ')) {
return msg.reply('Your photo is now linked to ' +
command.split('link me to ')[1]);
}
else if (command == 'attendance'){
if (msg.member.voice.channel) {
let idList =
Array.from(msg.member.voice.channel.guild.voiceStates.cache
.keys());
let attendance = [];
for (let i = 0; i < idList.length; i++){
attendance.push(Client.users.cache.get(idList[i]).username
+ '#' + Client.users.cache.get(idList[i]).discriminator);
}
return msg.channel.send('Attendance: ' + attendance);
} else {
return msg.reply('Please stay in the channel where
you want to take attendances.');
}
}
});
13) Upload the updated discord-greeter-bot project to your ec2 following how we did
14) Play around your bot and find what the bot can do! The hint is in the new codes.
12
5. Create DB (DynamoDB)
1) Navigate to DynamoDB
3) Name the table and add userID as the primary key. We will be using the
discord IDs of users to identify them as they are unique. Click Create.
4) Create an item with a test user and a test link. I used my own discord ID for
the purpose of this lab. For the test link I picked a random image url off of
13
6. Create Lambda Function POST
1) Create Lambda Function without a template, accept defaults and make sure
2) Create the Role with default execution role. Click Create Function
14
4) Click Attach Policies, add DynamoDBFullAccess
5) Go back to configuration.
6) Attach the following code to the function code, if you want to, you can also
15
7) Configure the Test Event and click Test.
16
9) Refresh and check your dynamoDB, the test event should be there.
1) Create Lambda Function without a template, accept defaults and make sure
2) You can use the same role as the previous Lambda function. Click Create
Function.
17
4) Enter the following code into index.js
5) Configure the Test Event with the same ID that you used for the previous
6) Click Test, if successful you will see the url in the CloudWatch Logs.
18
8. Create API Gateway
1) Go to aws API Gateway, scroll down to REST API and click Build
2) Select NewAPI and fill in the API name and Description. Click Create API
4) Create a new lambda function to retrieve all the information from the
database.
19
5) Make sure to use the same role as the previous lambda functions so that it
8) Test the lambda function. It should return an Item from the DynamoDB table
created earlier.
9) Create a get method using the lambda function that we created earlier above.
20
10) Configure the integration request. Under Mapping Template add
Request Passthrough.
21
11) Click on Method Request. From here you want to add a URL Query String
12) Click the Test button with the lightning bolt. Under Query Strings, input
It will return a response body with the contents of the Item from DynamoDB.
22
15) Generate the SDK from the testing stage.
16) Open up apigClient.js to copy the line that lets you retrieve information from
the API.
9. Store Attendance
23
2) The new function will need to have the same permission role as the previous lambda
functions. Fill in the information like the following and click Create Function
4) Paste the code for the lambda function into the handler function. We are parsing the
date time so that it will show the date as “Day of Week - Month - Day - Year - Time”
on the lambda side. When the bot checks attendance it will save the date and time as
the key.
24
5) Click Deploy and then click Test. The lambda function should display “Success” in
6) Check the DynamoDB, the users and timestamps should now be saved.
2) Create the Function with an existing permission role. This permission role
should be the same one as the previous functions. (Full Access to DDB)
25
3) Click Create Function
4) Configure a test event, the values can be the default, name it test.
6) Click Test
26
11) Click Integration Request. Click mapping templates. Add application/json to
12) Click Test with the Lightning bolt. It should show all the attendances done.
27
13) Redeploy the API
28
3) Update your bot.js like below. Bolded lines are newly added here. Don’t forget
to change your_bot_id!
// Run dotenv
require('dotenv').config();
var AWS = require('aws-sdk');
AWS.config.apiVersions = {lambda: '2015-03-31'};
Client.on('ready', () => {
console.log(`Now you logged in as ${Client.user.tag}`);
});
Client.on('message',msg => {
let bot_called = '';
if (msg.content.startsWith('<@&you_bot_id>')){
bot_called = '<@&you_bot_id> ';
')){
} else if (msg.content.startsWith('<@!you_bot_id>
bot_called = '<@!you_bot_id6> ';
} else {
return;
}
if (command == 'hi') {
return msg.reply('hello');
}
else if (command.startsWith('link me to ')) {
lambda.invoke({
FunctionName: 'storeUrl',
Payload: '{"Id": "' + msg.author.username + '#' +
msg.author.discriminator +'","Url":"' + command.split('link
me to ')[1] + '"}'
}, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(JSON.parse(data.Payload));
}
});
29
.keys());
let attendance = [];
for (let i = 0; i < idList.length; i++){
attendance.push(Client.users.cache.get(idList[i]).username
+ '#' + Client.users.cache.get(idList[i]).discriminator);
}
lambda.invoke({
FunctionName: 'storeAttendance',
Payload: '{"users": ' + JSON.stringify(attendance) +
'}'
}, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(JSON.parse(data.Payload));
}
});
30
12. Create FrontEnd (S3)
1) Create an S3 bucket and name it discord users, we will be using this to host
2) Make a front end page (HTML file) and use the apiGatewayClient call from
31
5) Enable Static website hosting.
32
13. Route to a domain (Route53)
4) Put your domain in the Domain name, leave other default settings and click Create.
33
5) Then the zone is created and you would see the following screen.
7) Click on Create Record, leave the Routing Policy as Simple Routing and click on
Next
34
8) Click on Define simple record
35
9) You can leave the Record name empty or put discord as I did. For Value/Route traffic
to, choose Alias to S3 website endpoint. Choose the region where you made the S3
bucket and choose the bucket. Then, click on Define simple record.
36
References
https://medium.com/davao-js/2019-tutorial-creating-your-first-simple-discord-bot-47fc836a17
0b
https://discordjs.guide/
37