-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
170 lines (147 loc) · 5.06 KB
/
main.js
File metadata and controls
170 lines (147 loc) · 5.06 KB
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
160
161
162
163
164
165
166
167
168
169
170
const Discord = require("discord.js");
const config = require("./config.json");
require("dotenv").config();
const mongoose = require("mongoose");
mongoose.connect(`${process.env.MONGO_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const bot = new Discord.Client({
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
const fs = require("fs");
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
const Profile = require("./models/profile.js");
const { Z_ASCII } = require("zlib");
const { url } = require("inspector");
//Welcome Message
bot.on("guildMemberAdd", (member) => {
try {
const generalChannel = member.guild.channels.cache.find(
(channel) => channel.name === "general"
);
generalChannel.send(`Welcome ${member}! Enjoy your stay! 😊`);
} catch (error) {
console.log(error);
}
});
//Goodbye Message
bot.on("guildMemberRemove", (member) => {
try {
const generalChannel = member.guild.channels.cache.find(
(channel) => channel.name === "general"
);
generalChannel.send(`Goodbye ${member} 😔`);
} catch (error) {
console.log(error);
}
});
//Send Message when joining
bot.on("guildCreate", (guild) => {
let channelID;
let channels = guild.channels.cache;
channelLoop: for (let key in channels) {
let c = channels[key];
if (c[1].type === "text") {
channelID = c[0];
break channelLoop;
}
}
let channel = guild.channels.cache.get(guild.systemChannelID || channelID);
channel.send(
"Tuturu! Thanks for inviting me! Type `$cmds` to view all my commands!"
);
});
//Ready bot status
bot.on("ready", async () => {
let botUsername = `${bot.user.username}`;
console.log(botUsername + " is online!");
bot.user.setActivity("tuturu🎶", {
type: "STREAMING",
url: "https://www.youtube.com/watch?v=HkGNeN0LGOE",
});
});
//Command Handler
fs.readdir("./commands/", (err, files) => {
if (err) console.log(err);
let jsfile = files.filter((f) => f.split(".").pop() === "js");
if (jsfile.length <= 0) {
return console.log("[LOGS] Couldn't Find Commands!");
}
jsfile.forEach((f, i) => {
let pull = require(`./commands/${f}`);
bot.commands.set(pull.config.name, pull);
pull.config.aliases.forEach((alias) => {
bot.aliases.set(alias, pull.config.name);
});
});
});
bot.on("message", async (message) => {
if (message.author.bot || message.channel.type === "dm") return;
let prefix = config.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
//If message doesn't start with prefix, return nothing
if (message.content.startsWith(prefix)) {
let commandfile =
bot.commands.get(cmd.slice(prefix.length)) ||
bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)));
//If message is a command, run it
if (commandfile) commandfile.run(bot, message, args);
}
//Rating Attachment
if (cmd === `${prefix}rating`) {
if (message.attachments.size > 0) {
message.react("👍");
message.react("👎");
}
}
//Level and XP starter
Profile.findOne(
{
userID: message.author.id,
serverID: message.guild.id,
},
(err, profile) => {
if (err) console.log(err);
//If there currently isnt any on the database, make a new one
if (!profile) {
const newProfile = new Profile({
userID: message.author.id,
serverID: message.guild.id,
money: 0,
xp: 0,
level: 1,
});
newProfile.save().catch((err) => console.log(err));
}
//Update if there are stuff in the database
else {
// console.log('This is current xp: ' + profile.xp)
// console.log('This is my current level: ' + profile.level)
let generatedXP = Math.floor(Math.random() * 20);
profile.xp += generatedXP;
//Levels.save().catch(err => console.log(err));
let neededXp = 100 * profile.level;
if (profile.xp > neededXp) {
++profile.level;
profile.xp = profile.xp - neededXp;
message.reply(
`You are now level ${profile["level"]} with ${profile.xp} experience!`
);
}
profile.save().catch((err) => console.log(err));
}
}
);
});
//Add Role from Reaction
bot.on("messageReactionAdd", async (reaction, user, message, args, guild) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
});
bot.login(process.env.BOT_TOKEN);