-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (117 loc) · 4.9 KB
/
index.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
// Discord.js algosdk and .env packages installed via NPM
const { Client, Intents, GuildMember, Guild } = require('discord.js');
const algosdk = require('algosdk');
const keepAlive = require("./server")
require('dotenv').config()
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
});
// When bot is online
client.on("ready", () => {
console.log(`${client.user.tag}`);
})
//Algo params
const port = "";
const token = {
"x-api-key": process.env.API // fill in yours
};
//Indexer connection
const BASE_SERVER = "https://mainnet-algorand.api.purestake.io/idx2";
let algoIndexer = new algosdk.Indexer(token, BASE_SERVER, port);
//Storing created assets and assets present in the holder's address
let assetidList = [];
let holderAssets = [];
let optinCheck = false;
let holderNfts = [];
let optinAsset = '726327745'
// Parameters to connect to Algorand blockchain via indexer
async function getInfo(collectorAccount) {
assetidList = [];
holderAssets = [];
optinCheck = false;
holderNfts = [];
//The creator accout/accounts
let account = [ `72BIB7C53AU3EFUJ5SFNGANWHVMZCGH2RDDBLXYS3Q3KH6NIB5MCLMWNTI`,`PTPBICPBDFFJRHWFEQKV33NDRKMWOY7FA24CJIQFWBNP6V4E2TDQVDF37Q`, `AD5J43O3N6UPEUFYOZHT6WBUXDOK66MMGL3JHQV77Y2EAEZJVLRCINWYBI`];
//Getting list of asset ids from creator account
const creatorAssetids = async function(algoIndexer, account) {
for (let i = 0; i < account.length; i++) {
let nextToken = '';
let accountInfo;
//A do while loop to get full list of asset ids
do{
if(nextToken == '') {
accountInfo = await algoIndexer.lookupAccountCreatedAssets(account[i]).do();
nextToken = accountInfo['next-token']
addAssets(accountInfo)
} else{
accountInfo = await algoIndexer.lookupAccountCreatedAssets(account[i]).nextToken(nextToken).do();
nextToken = accountInfo['next-token']
addAssets(accountInfo)
}
} while (accountInfo['assets'].length > 0)
//Function for adding asset ids to the array.
function addAssets(accountInfo){
for (let idx = 0; idx < accountInfo['assets'].length; idx++) {
let scrutinizedAsset = accountInfo['assets'][idx];
let assetId = scrutinizedAsset['index'];
assetidList.push(assetId)
}
}
}
}
//checking the assets available with the collector
const collectorAssetids = async function(algoIndexer, collectorAccount) {
let accountInfo = await algoIndexer.lookupAccountAssets(collectorAccount).do();
for (let idx = 0; idx < accountInfo['assets'].length; idx++) {
let scrutinizedAsset = accountInfo['assets'][idx];
if(scrutinizedAsset['asset-id'] == optinAsset){
optinCheck = true;
}
if(scrutinizedAsset['amount'] > 0){
let assetId = scrutinizedAsset['asset-id'];
holderAssets.push(assetId)
}
}
}
//comparing the assets to findout if he is a holder
async function holdercheck (){
for(let i=0; i< holderAssets.length; i++){
for(let j=0; j< assetidList.length; j++){
if(holderAssets[i] == assetidList[j]){
holderNfts.push(holderAssets[i]) ;
}
}
}
}
async function optIn() {
if(optinCheck == true){
return true
}
return false;
}
//Function calls to run the process
await creatorAssetids(algoIndexer, account)
await collectorAssetids(algoIndexer, collectorAccount)
await holdercheck()
return await optIn()
}
//Looking for messages from the discord and respond
client.on('messageCreate', async (message) => {
if (message.content.startsWith(`!holder`)){
let address = message.content.substring(8)
let verification = await(getInfo(address))
if(verification == true && holderNfts.length > 0){
message.member.roles.add('938661361884475392');
message.channel.send('Verification complete.\n Added role Ice World Citizen');
}else if(verification == false && optinCheck == false){
message.reply('Please opt-in using `https://www.randgallery.com/algo-collection/?address=726327745` \n Wait for 10 seconds and try `!holder` command.');
}else{
message.reply('We could not detect the NFT in your wallet. Please contact admin.')
}
}
})
keepAlive()
client.login(process.env.DISCORD);