· 6 years ago · Aug 26, 2019, 07:44 PM
1/*******************************************************************************
2 © Cherry P. Arrot // Birbatastic 2019 All Rights Reserved.
3
4 Please do not publish, sell, distribute or claim ownership of these codes.
5*******************************************************************************/
6
7// Initiate database
8const sql = require('sqlite');
9sql.open("./data.sqlite");
10
11const mChannel = message.guild.channels.find(`name`, "strikes")
12 if (!mChannel) return message.channel.send("**:x: Error:** Did not find a The log channel");
13
14// Define Classes
15const { Embed } = require('./classes/Embed');
16
17module.exports.run = async (bot, message, args) => {
18 // Define variables
19 var w_user = message.mentions.users.first();
20 var reason = args.slice(1).join(" ");
21 var m_user = message.author;
22 var m_member = message.member;
23
24 // Define Warn IDs
25 const warnIDParts = [
26 'a',
27 'b',
28 'c',
29 'd',
30 'e',
31 'f',
32 'g',
33 'h',
34 'i',
35 'j',
36 'k',
37 'l',
38 'm',
39 'n',
40 'o',
41 'p',
42 'q',
43 'r',
44 's',
45 't',
46 'u',
47 'v',
48 'w',
49 'x',
50 'y',
51 'z',
52 'A',
53 'B',
54 'C',
55 'D',
56 'E',
57 'F',
58 'G',
59 'H',
60 'I',
61 'J',
62 'K',
63 'L',
64 'M',
65 'N',
66 'O',
67 'P',
68 'Q',
69 'R',
70 'S',
71 'T',
72 'U',
73 'V',
74 'W',
75 'X',
76 'Y',
77 'Z',
78 '1',
79 '2',
80 '3',
81 '4',
82 '5',
83 '6',
84 '7',
85 '8',
86 '9',
87 '0'];
88
89 var randID = warnIDParts[Math.floor(Math.random() * warnIDParts.length)];
90
91 var warnID = `${randID}${randID}${randID}${randID}${randID}.${randID}${randID}${randID}${randID}${randID}${randID}${randID}${randID}${randID}${randID}_${randID}${randID}${randID}`;
92
93 // Check if user has permission
94 // If user does not have permission, send message
95 if(!m_member.permissions.has('MANAGE_GUILD') && !m_member.permissions.has('ADMINISTRATOR')) {
96 message.channel.send(new Embed(bot, "Error!", "You are missing 1 or more permissions required to use this command.", [], {
97 color: "#ff0000",
98 thumbnail: w_user.avatarURL
99 }));
100 } else {
101 // Check if a user is tagged
102 // If a user is not tagged, send message
103 if(!w_user) {
104 message.channel.send(new Embed(bot, "Error!", "You must tag a user to warn them!", [], {
105 color: "#ff0000",
106 thumbnail: m_user.avatarURL
107 }));
108 } else if (!reason) { // If a user is tagged but without reason, send message
109 message.channel.send(new Embed(bot, "Error!", "You must specify a reason to warn this user!", [], {
110 color: "#ff0000",
111 thumbnail: m_user.avatarURL
112 }));
113 } else { // If all is given, run command
114 // Check if ID already exists in database
115 sql.get(`SELECT * FROM warns WHERE id="${warnID}"`).then(row => {
116 if(!row) { // If ID is NOT in database, add to database
117 sql.run(`INSERT INTO warns (id, w_username, w_id, m_username, m_id, reason) VALUES ("${warnID}", "${w_user.username}", "${w_user.id}", "${m_user.username}", "${m_user.id}", "${reason}")`);
118
119 // Let mod know the command has run successfully.
120 message.channel.send(new Embed(bot, "Success!", `The user **${w_user.tag}** has been warned successfully by **${m_user.tag}** with reason **${reason}**`, [], {
121 thumbnail: w_user.avatarURL
122 }));
123 // Let mod know the command has run successfully.
124 mChannel.channel.send(new Embed(bot, "Success!", `The user **${w_user.tag}** has been warned successfully by **${m_user.tag}** with reason **${reason}**`, [], {
125 thumbnail: w_user.avatarURL
126 }));
127 }
128 }).catch(() => { // If database table does not exist
129 // Create database table with name "warns"
130 sql.run("CREATE TABLE IF NOT EXISTS warns (id TEXT, w_username TEXT, w_id TEXT, m_username TEXT, m_id TEXT, reason TEXT)").then(() => {
131 // Add to database
132 sql.run(`INSERT INTO warns (id, w_username, w_id, m_username, m_id, reason) VALUES ("${warnID}", "${w_user.username}", "${w_user.id}", "${m_user.username}", "${m_user.id}", "${reason}")`);
133
134 // Let mod know the command has run successfully.
135 message.channel.send(new Embed(bot, "Success!", `The user **${w_user.tag}** has been warned successfully by **${m_user.tag}** with reason **${reason}**`, [], {
136 thumbnail: w_user.avatarURL
137 }));
138
139 // Let Developer know database table was created
140 console.log(`[BACKSPIN_SQL] Database table "warns" created successfully.`);
141 })
142 });
143 }
144 }
145
146}
147
148module.exports.help = {
149 name: "warn"
150};