-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (142 loc) · 7.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const {Client, RichEmbed, MessageFlags} = require('discord.js');// require the discord.js module
const Discord = require('discord.js');
const client = new Client();
const fs = require('fs');
const fetch = require('node-fetch');
var config;
var settings;
var version;
var token;
var uptime = 0;
function save(){
fs.writeFile('settings.json', JSON.stringify(settings), function(err){
if(err){throw err;}
console.log('Saved settings.');
});
}
var uptimeINT = setInterval(function(){
uptime++;
}, 60000)
fs.readFile('config.json', function(err, data){
if(err){throw err;}
config = JSON.parse(data);
token = config.token;
version = config.version;
console.log('Read config.');
fs.readFile('settings.json', function(err, data){
if(err){throw err;}
settings = JSON.parse(data);
client.login(token);
console.log('Read settings.');
});
});
client.once('ready', () => {
console.log('Ready!');
});
client.on("guildCreate", (guild) => {
console.log(`Joined new guild: ${guild.name}`);
fs.readFile('settings.json', function(err, data){
if(err){throw err;}
settings = JSON.parse(data);
var obj = {};
obj["prefix"] = ".";
obj["name"] = guild.name;
obj["ownerID"] = guild.ownerID;
obj["role"] = "@everyone";
obj["adminRole"] = "Admin";
settings[guild.id] = obj;
console.log(settings);
fs.writeFile('settings.json', JSON.stringify(settings), function(err){if(err){throw err;}});
console.log('Added new server to settings.')
});
});
client.on('message', message => {
if(message.author.bot == false && message.channel.type != "dm"){//checking for channel type and bots
var server = message.guild.id.toString();
if(settings[server]['role'] == 'everyone'){
settings[server]['role'] = '@everyone';
}
if(message.member.hasPermission(['SEND_MESSAGES'])){//checking for user role
if(message.author.id == '571462642653331471' && message.content == '-close'){//shut down bot (sudo command)
save();
client.destroy();
console.log('Successfully closed.');
}
else if(message.author.id == '571462642653331471' && message.content == '-save'){//save settings (sudo command)
save();
message.channel.send('Saved server settings.');
}
else if(message.content == settings[server]['prefix']+'about'){//about
const aboutEmbed = new Discord.MessageEmbed()
aboutEmbed.setColor('#fac036')
aboutEmbed.setTitle('About\n')
aboutEmbed.setDescription('A Discord bot designed to give helpful advice.\nIn ' + client.guilds.cache.size + ' servers.\nVersion: ' + version + '\n\n**Credits** \nCredit to <@571462642653331471> for creating the bot.')
message.channel.send(aboutEmbed);
}
else if(message.content == settings[server]['prefix']+'help'){//help
const helpEmbed = new Discord.MessageEmbed()
helpEmbed.setColor('#fac036')
helpEmbed.setTitle('Help\n')
helpEmbed.setDescription('**' + settings[server]['role'] + ' commands:**\n``' + settings[server]['prefix'] + 'tip`` Recive a peice of advice.\n``' + settings[server]['prefix'] + 'help`` Brings you to this menu.\n``' + settings[server]['prefix'] + 'about`` Shows details about Protip.\n``' + settings[server]['prefix'] + 'source`` View the source code.\n``' + settings[server]['prefix'] + 'bug`` Report a bug/error.\n``' + settings[server]['prefix'] + 'suggest`` Suggest a feature.\n``' + settings[server]['prefix'] + 'invite`` Invite Protip to your server.\n``' + settings[server]['prefix'] + 'github`` View Protip\'s github repository.\n``' + settings[server]['prefix'] + 'website`` Visit the official Protip website.\n``' + settings[server]['prefix'] + 'uptime`` See how long Protip has been online\n\n**' + settings[server]['adminRole'] + ' commands:**\n``' + settings[server]['prefix'] + 'set prefix (new prefix)`` Set a new command prefix for Protip.')
message.channel.send(helpEmbed);
}
else if(message.content == settings[server]['prefix']+'invite'){//invite link
message.channel.send('If you would like to add Protip to your server please go to this link:\nhttps://discord.com/oauth2/authorize?client_id=792875120116891709&scope=bot&permissions=248832');
}
else if(message.content == settings[server]['prefix']+'bug'){//bug report
message.channel.send('If you have found a bug please fill out this form:\nhttps://protip.now.sh/contact');
}
else if(message.content.startsWith(settings[server]['prefix']+'suggest')){//suggestion box
message.channel.send('If you have any suggestions for future updates please fill out this form:\nhttps://protip.now.sh/contact');
}
else if(message.content.startsWith(settings[server]['prefix']+'uptime')){//uptime
var calcUptime = Math.floor(uptime/60).toString();
message.channel.send(`Protip has been online for ${calcUptime} hours.`);
}
else if(message.content == settings[server]['prefix']+'tip'){//tip
fetch('https://api.adviceslip.com/advice')
.then(resp => {
var factBuffer = resp.body._outBuffer;
var fact = factBuffer.toString('utf-8');
var index1 = fact.indexOf('advice');
var index2 = fact.indexOf('}}');
fact = fact.slice(index1, index2);
fact = fact.replace('"', "");
index1 = fact.indexOf(':');
index2 = fact.length;
fact = fact.slice(index1+3, index2);
fact = fact.replace('"', "");
message.channel.send(fact);
});
}
else if(message.content.startsWith(settings[server]['prefix']+'eval')){//eval joke
message.channel.send('Nice try!');
}
else if(message.content.startsWith(settings[server]['prefix']+'source')){//view src
message.channel.send('',{
files: ["source.txt"]
})
}
else if(message.content.startsWith(settings[server]['prefix']+'set prefix ') && message.member.hasPermission(['ADMINISTRATOR'])){//set prefix
var newPrefix = message.content.substring(12, message.content.length);
if(newPrefix == settings[server]['prefix']){
message.channel.send('Prefix is already set to: ' + settings[server]['prefix']);
}
else{
settings[server]['prefix'] = message.content.substring(12, message.content.length);
save();
message.channel.send('Changed prefix to: ' + settings[server]['prefix']);
}
}
else if(message.content.startsWith(settings[server]['prefix']+'github')){//github
message.channel.send('Protip\'s Github repository:\nhttps://github.com/IMAD200');
}
else if(message.content.startsWith(settings[server]['prefix']+'website')){//webpage
message.channel.send('https://protip.now.sh/');
}
else if(message.content.startsWith(settings[server]['prefix'])){//no command recognized
message.channel.send('Sorry I didn\'t recognize that command.\nUse ``' + settings[server]['prefix'] + 'help`` to see a list of commands.');
}
}
}
});