-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie-knight.js
146 lines (136 loc) · 5.52 KB
/
movie-knight.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
//Dependencies
const botSettings = require("./botsettings.json")
const Discord = require("discord.js")
const axios = require("axios")
const fs = require("fs")
const mongoose = require("mongoose")
const Guild = require("./models/Guild.js")
//So we don't have to type Discord.Client(); every time
const bot = new Discord.Client();
//Connect to database
mongoose.connect("mongodb+srv://" + botSettings.dbUsername + ":" + botSettings.dbPassword + "@movieknight.xebz3fz.mongodb.net/?retryWrites=true&w=majority")
//So we know the bot is working
bot.on("ready", () => {
console.log("Movie Knight, at your service!")
Guild.find({}, function (err, guild) {
let guildList = bot.guilds.map(guilds => {
let guildListId = guilds.id
let guildListName = guilds.name
let guildListIcon = guilds.iconURL
let guildListMembers = guilds.members.keyArray()
guildObj= {id: guildListId, name: guildListName, icon: guildListIcon, members: guildListMembers}
return guildObj
});
for (i = 0; i < guildList.length; i++) {
var index = -1;
for (j = 0; j < guild.length; j++) {
if (guild[j].guild_id === guildList[i].id) index = i;
}
if (index < 0) {
const newGuild = new Guild({
guild_name: guildList[i].name,
guild_id: guildList[i].id,
guild_icon: guildList[i].icon,
guild_prefix: "~",
owner_id: guild.ownerID,
moderator_role_id: "",
movie_night_role_id: "",
movie_night_channel_id: "",
movie_cooldown: "",
request_list: [],
user_list: guildList[i].members
});
newGuild.save(function (err) {
if (err) throw err
console.log("Guild created!")
});
}
}
console.log("Existing guild database check complete!")
});
});
bot.on('error', console.error);
//Bot creates database document when joining server and gives setup instructions
bot.on("guildCreate", guild => {
const newGuild = new Guild({
guild_name: guild.name,
guild_id: guild.id,
guild_icon: guild.iconURL,
guild_prefix: "~",
owner_id: guild.ownerID,
moderator_role_id: "",
movie_night_role_id: "",
movie_night_channel_id: "",
movie_cooldown: "",
request_list: [],
user_list: guild.members.keyArray()
});
newGuild.save(function (err) {
if (err) throw err
console.log("Guild created!")
});
let defaultChannel = "";
guild.channels.forEach((channel) => {
if (channel.type == "text" && defaultChannel == "") {
if (channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
});
defaultChannel.send({
"embed": {
"title": "**Movie Knight is here!**",
"description": "Movie Knight is a Discord community movie night bot that handles movie night roles, displays information on movies, creates a community movie request list, and creates polls from the community list for users to vote on a movie to be viewed!\n\nTo get set up Movie Knight, type `~setup` (Setup is restricted to owners. An admin or bot testing channel is recommended for the setup process!) \n\n To see a list of commands and examples, type `~help`\n\n For more information, screenshots, and more, visit Movie Knight's [website](https://ufufuru.github.io/)!",
"color": 10973164
}
});
});
//Deletes database document when leaving server
bot.on("guildDelete", guild => {
Guild.findOneAndRemove({guild_id: guild.id}, function(err) {
if (err) throw (err)
console.log("Guild deleted!")
});
});
//Updates database document whenever a server updates it's name/picture/members
bot.on("guildUpdate", (oldGuild, newGuild) => {
Guild.findOneAndUpdate({guild_id: oldGuild.id}, {"$set": {"guild_name": newGuild.name, "guild_icon": newGuild.iconURL}}, function(err) {
if (err) throw (err);
});
})
;
bot.on("guildMemberAdd", (member) => {
Guild.findOneAndUpdate({guild_id: member.guild.id}, {"$push": {"user_list": member.id}}, function(err) {
if (err) throw (err);
console.log("Member added!")
});
})
bot.on("guildMemberRemove", (member) => {
Guild.findOneAndUpdate({guild_id: member.guild.id}, {"$pull": {"user_list": member.id}}, function(err) {
if (err) throw (err);
console.log("Member deleted!")
});
})
;
//Command handler
bot.on("message", message => {
//Prefix+command structure
Guild.findOne({guild_id: message.guild.id}, function(err, guild) {
if (err) throw err
const guildPrefix = guild.guild_prefix
const args = message.content.slice(guildPrefix.length).trim().split(/ +/g)
const command = args.shift().toLowerCase()
//If bot is the author or the prefix isn't the first character, return
if (message.author.bot) return
if (message.content.indexOf(guildPrefix) !== 0) return
//if (message.channel.id !== "MOVIE CHANNEL ID") return
//Check for commands
try {
let commandFile = require(`./commands/${command}.js`);
commandFile.run(bot, message, args);
} catch (err) {
console.error(err);
}
});
});
bot.login(botSettings.token)