Skip to content

Commit c9b4fab

Browse files
Arnav-KhareArnav Kharesidntrivedi012
authored
Add discord bot (#45)
* Added discord-bot * Added gitignore file * Append * Optimised time to fetch xkcd comic. * Update package.json * Update discord-bot/bot.js * Added Procfile Co-authored-by: Arnav Khare <[email protected]> Co-authored-by: Siddhant N Trivedi <[email protected]>
1 parent 9e19052 commit c9b4fab

File tree

6 files changed

+297
-1
lines changed

6 files changed

+297
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
logs
22
.exe
33
telegram-bot/.env
4-
telegram-bot/main
4+
telegram-bot/main
5+
discord-bot/.env
6+
discord-bot/node_modules

discord-bot/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Worker: npm start

discord-bot/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Discord Bot
2+
A chat bot for OSDC-Discord Server, built on top of discord.js.
3+
4+
## Setting up the bot
5+
6+
This bot is built on [node.js](https://nodejs.org/en/). You will need at least node 12.xx.Check the version on your local machine by running 'node -v' on the terminal
7+
8+
### GET THE DISCORD-API TOKEN
9+
10+
1. Go to [Discord Developer Portal](https://discord.com/developers/applications) and login with your Discord Account.
11+
2. Create a New Application.
12+
3. Click on Add Bot in the Bot section.
13+
4. You’ll get your Bot API token under the token title
14+
5. Copy it and save it in a file named as /.env/ in your project folder.
15+
16+
### ADD BOT TO YOUR TEST SERVER
17+
18+
1. Go to OAuth2 section in your application
19+
2. Select bot in the scopes menu and Administrator in bot permission menu.
20+
3. A Link will be generated in the scope menu, copy it and paste it in your browser URL tab.
21+
4. Select your test server in the drop down box...
22+
23+
24+
### Build & Execute the bot
25+
26+
1. Export the Discord API Token by running export TOKEN_OSDC=<your-token> in the bots directory using terminal.
27+
2. Run following commands from terminal
28+
```bash
29+
npm install
30+
npm start
31+
```

discord-bot/bot.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
require('dotenv').config()
2+
const axios = require('axios');
3+
4+
const Discord = require('discord.js')
5+
const bot = new Discord.Client()
6+
const token = process.env.TOKEN_OSDC
7+
8+
bot.on('ready', () => {
9+
console.log('The bot is online!!!!')
10+
})
11+
12+
13+
//Greeting message for a New user!!!
14+
15+
bot.on('guildMemberAdd',member => {
16+
const channel = member.guild.channels.cache.find(ch => ch.name === 'member-log');
17+
if(!channel) return ;
18+
message.channel.send(`Hello ${message.author} Welcome to OSDC Discord Server..Please introduce yourself!!`)
19+
})
20+
21+
//help command
22+
23+
bot.on('message',message => {
24+
if(message.content === '!help')
25+
{
26+
const commandEmbedded = new Discord.MessageEmbed()
27+
.setTitle('Here are the list of commands ')
28+
.addFields(
29+
{name : '!help', value: "To view the list of commands " ,inline:false},
30+
{name : '!website', value: "Visit our Website" ,inline:false},
31+
{name : '!facebook', value: "Follow us on Facebook " ,inline:false},
32+
{name : '!twitter', value: "Check us out on Twitter " ,inline:false},
33+
{name : '!github', value: "Visit our Github Repository " ,inline:false},
34+
{name : '!telegram', value: "Join our telegram channel " ,inline:false},
35+
{name : '!xkcd', value: "To get an xkcd comic " ,inline:false},
36+
{name : '!irc', value: "Find us on IRC :) " ,inline:false},
37+
{name : '!blog', value: "Get the link of OSDC blog" ,inline:false},
38+
{name : '!instagram', value: "Follow us on instagram :) " ,inline:false},
39+
40+
)
41+
message.channel.send(commandEmbedded)
42+
}
43+
})
44+
45+
//xkcd comic command
46+
//It first randomly generate a number between 100 to 2000 and then sends the http request with the generated number to fetch comic
47+
48+
49+
bot.on('message',message => {
50+
if(message.content === '!xkcd')
51+
{
52+
let comicNo = Math.floor(Math.random() * (2000 - 100 + 1) + 100)
53+
54+
axios.get(`http://xkcd.com/${comicNo}/info.0.json`).then(resp => {
55+
56+
message.channel.send(resp.data.img)
57+
});
58+
59+
}
60+
61+
})
62+
63+
//Social media commands
64+
65+
bot.on('message',message => {
66+
if(message.content === '!website')
67+
{
68+
const website = new Discord.MessageEmbed().setTitle('Visit our Website').setURL('https://osdc.netlify.app/')
69+
message.channel.send(website)
70+
}
71+
if(message.content === '!twitter')
72+
{
73+
const twitter = new Discord.MessageEmbed().setTitle('Check us out on Twitter').setURL('https://twitter.com/osdcjiit')
74+
message.channel.send(twitter)
75+
}
76+
if(message.content === '!facebook')
77+
{
78+
const facebook = new Discord.MessageEmbed().setTitle('Follow us on Facebook').setURL('https://www.facebook.com/JIIT-OSDC-169171359799320/')
79+
message.channel.send(facebook)
80+
}
81+
if(message.content === '!github')
82+
{
83+
const github = new Discord.MessageEmbed().setTitle('Take a look at our cool projects').setURL('https://github.com/osdc')
84+
message.channel.send(github)
85+
}
86+
if(message.content === '!telegram')
87+
{
88+
const telegram = new Discord.MessageEmbed().setTitle(' Join our Telegram Channel').setURL('https://t.me/jiitosdc')
89+
message.channel.send(telegram)
90+
}
91+
if(message.content === '!irc')
92+
{
93+
const irc = new Discord.MessageEmbed().setTitle(' Join us on IRC server of Freenode at #jiit-lug').setURL('https://github.com/osdc/community-committee/wiki/IRC')
94+
message.channel.send(irc)
95+
}
96+
if(message.content === '!blog')
97+
{
98+
const blog = new Discord.MessageEmbed().setTitle('Blogs written by the folks at the Open Source Developers Community').setURL('https://osdcblog.netlify.com/')
99+
message.channel.send(blog)
100+
}
101+
102+
if(message.content === '!instagram')
103+
{
104+
message.channel.send('https://tenor.com/view/dont-do-that-avengers-black-panther-we-dont-do-that-here-gif-12042935')
105+
}
106+
107+
})
108+
109+
110+
bot.login(token)

discord-bot/package-lock.json

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

discord-bot/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "osdc-discord-bot",
3+
"version": "1.0.0",
4+
"description": "A simple bot for OSDC Discord Server",
5+
"main": "bot.js",
6+
"scripts": {
7+
"start": "node bot.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"licenses": [
12+
{
13+
"type": "MIT",
14+
"url": "https://github.com/osdc/bots/blob/master/LICENSE.md"
15+
}
16+
],
17+
"dependencies": {
18+
"axios": "^0.21.0",
19+
"discord.js": "^12.4.0",
20+
"dotenv": "^8.2.0"
21+
}
22+
}

0 commit comments

Comments
 (0)