· 4 years ago · Jan 12, 2021, 12:52 PM
1const Discord = require('discord.js');
2const client = new Discord.Client();
3
4client.once('ready', () => {
5 console.log('Ready!');
6});
7
8
9const fetch = require('node-fetch');
10
11const TrelloAPI = require('trello-node-api');
12const Trello = new TrelloAPI();
13
14Trello.setApiKey('2abde14943abebe62c7b85750f3dc3dc');
15Trello.setOauthToken('d8cfabf7178b2f62a76b51db95ecbfcd21addf6e7a78f47b6d906dba3fbfd9ce');
16
17const apiKey = '2abde14943abebe62c7b85750f3dc3dc';
18const oauthToken = 'd8cfabf7178b2f62a76b51db95ecbfcd21addf6e7a78f47b6d906dba3fbfd9ce';
19
20const generateURL = () => {
21 return ('https://api.trello.com/1/search?query=description:"' + "code" + '"&partial=true&modelTypes=cards&board_fields=name&card_fields=name&list_fields=name&card_board=true&card_list=true&token=' + oauthToken + '&key=' + apiKey);
22};
23
24
25const generateCard = (code) => {
26 var data = {
27 name: code,
28 desc: 'code',
29 pos: 'top',
30 idList: "5ffd8655c707ff70dab222a0"
31 };
32 return data;
33};
34
35const prefix = "!";
36
37client.on('message', message => {
38 if(message.author.bot) return;
39 if(message.channel.type === "dm") return;
40
41 const args = message.content.slice(prefix.length).trim().split(' ');
42 const command = args.shift().toLowerCase();
43
44 if (message.content.toLowerCase().includes('code')) {
45 const codeEmbed = new Discord.MessageEmbed()
46 .setTitle("Code")
47 .setColor('RED')
48 .setDescription("Once you have been moved into the voice channel, you will be messaged the code!")
49 .setFooter("Make sure your messages are open!");
50
51 message.channel.send(codeEmbed);
52 }
53
54 if(!message.content.startsWith(prefix)) return;
55
56 let messageMember = message.guild.members.get(authorID);
57 var role = message.guild.roles.find(role => role.name === "Magic Mods");
58
59 if(!message.member.hasPermission("ADMINISTRATOR") && !messageMember.roles.has(role) && message.author.id !== "715195498495082627" && message.author.id !== "759103115483545620") return;
60
61
62 if(command === "setcode") {
63
64 const url = generateURL()
65fetch(url)
66.then(res => res.json())
67.then(json => {
68 let codeFound = 0;
69 //console.log(json.cards);
70 const cards = json.cards;
71 if(cards[0] === null) {
72 codeFound++
73 // no codes found
74 return;
75 }
76 if(codeFound !== 0) return;
77 const cardID = cards[0].id;
78 const code = cards[0].name;
79
80 Trello.card.del(cardID);
81});
82
83const card = generateCard(args[0]);
84
85Trello.card.create(card).then(function(response) {
86 // woo
87}).catch(function(error) {
88 console.error(error);
89});
90
91
92 }
93
94});
95
96
97client.on('voiceStateUpdate', (oldState, newState) => {
98 const newUserChannel = newState.channelID;
99 const oldUserChannel = oldState.channelID;
100
101
102
103 // put the channel id you want to log there
104
105 if(newUserChannel === '798501950492639242') {
106 // User Joins a voice channel
107 console.log('join');
108
109 const url = generateURL()
110fetch(url)
111.then(res => res.json())
112.then(json => {
113 let codeFound = 0;
114 //console.log(json.cards);
115 const cards = json.cards;
116 if(cards[0] === null) {
117 codeFound++
118 // no codes found
119 return;
120 }
121 if(codeFound !== 0) return;
122 const cardID = cards[0].id;
123 const code = cards[0].name;
124
125
126 newState.member.send(code)
127 .catch(() => {
128 // Users dm's are closed.
129
130 // Maybe send a message in a channel and tag them?
131 });
132 });
133
134
135
136 } else if(newUserChannel !== '798501950492639242'){
137 // User leaves a voice channel
138 console.log('leave');
139
140 }
141 });