· 6 years ago · Mar 19, 2020, 10:58 PM
1App.js
2```js
3// Require discord.js package
4const discord = require("discord.js");
5
6// Require hypixel.js package
7const hypixel = require("hypixeljs");
8
9// Require node-fetch package
10const fetch = require("node-fetch");
11
12const fs = require("fs");
13
14const { prefix, token, hypixelapi } = require("./config.json");
15
16// Create a new client using the new keyboard
17const client = new discord.Client();
18
19client.commands = new discord.Collection();
20
21const commandFiles = fs
22 .readdirSync("./commands")
23 .filter(file => file.endsWith(".js"));
24
25for (const file of commandFiles) {
26 const command = require(`./commands/${file}`);
27
28 // set a new item in the Collection
29 // with the key as the command name and the value as the exported module
30 client.commands.set(command.name, command);
31}
32
33// Display a message when the bot comes online
34client.on("ready", () => {
35 console.log(`Logged in as ${client.user.tag}!`);
36 client.user
37 .setPresence({ activity: { name: ";cr;help" }, status: "Online" })
38 .then(console.log)
39 .catch(console.error);
40});
41
42// Check for new messages
43client.on("message", msg => {
44 if (!msg.content.startsWith(prefix) || msg.author.bot) return;
45
46 const args = msg.content.slice(prefix.length).split(/ +/);
47 const command = args.shift().toLowerCase();
48
49 if (command === `verify`) {
50 client.commands.get("verify").execute(msg, args);
51 }
52});
53
54// Log in the bot using your token (password)
55client.login(token);
56
57// Log in to Hypixel API with API Key
58hypixel.login(hypixelapi);
59```
60
61commands/verify.js
62```js
63const discord = require("discord.js");
64
65const fetch = require("node-fetch");
66
67const client = new discord.Client();
68
69const { prefix, token, hypixelapi } = require("../config.json");
70
71module.export = {
72 name: "verify",
73 description: "Verify your Hypixel account with your Discord account.",
74 execute(msg, args) {
75 // Check for new messages
76 client.on("message", msg => {
77 if (!msg.content.startsWith(prefix) || msg.author.bot) return;
78
79 const args = msg.content.slice(prefix.length).split(/ +/);
80 const command = args.shift().toLowerCase();
81
82 if (msg.content.startsWith(prefix) && command === `verify`) {
83 const hypixelplayer = args[0];
84 var embed = new discord.MessageEmbed()
85 .setColor("#A6FF5C")
86 .setDescription(`Trying to link ${hypixelplayer} to your discord!`);
87 msg.channel.send(embed);
88
89 var Verify = function(User) {
90 fetch(
91 `https://api.hypixel.net/player?key=${hypixelapi}&name=${hypixelplayer}`
92 )
93 .then(res => res.json())
94 .then(json => {
95 if (
96 json.player !== null &&
97 json.player.socialMedia &&
98 json.player.socialMedia.links &&
99 json.player.socialMedia.links.DISCORD
100 )
101 console.log(json.player.socialMedia.links.DISCORD);
102 if (User == json.player.socialMedia.links.DISCORD) {
103 console.log("Success");
104 }
105 });
106 };
107
108 var User = msg.author.tag;
109 console.log(User);
110 Verify(User);
111 }
112 });
113 }
114};
115```