Skip to content

Commit f24211c

Browse files
committed
add feathercoin
1 parent 5866fd5 commit f24211c

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

bot/modules/featherTipper.js

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
'use strict';
2+
3+
const bitcoin = require('bitcoin');
4+
5+
let Regex = require('regex'),
6+
config = require('config'),
7+
spamchannels = config.get('moderation').botspamchannels;
8+
let config = config.get('ftcd');
9+
const ftc = new bitcoin.Client(config);
10+
11+
exports.commands = ['tipftc'];
12+
exports.tipftc = {
13+
usage: '<subcommand>',
14+
description:
15+
'**!tipftc** : Displays This Message\n **!tipftc balance** : get your balance\n **!tipftc deposit** : get address for your deposits\n **!tipftc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipftc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipftc private <user> <amount>** : put private before Mentioning a user to tip them privately.',
16+
process: async function(bot, msg, suffix) {
17+
let tipper = msg.author.id.replace('!', ''),
18+
words = msg.content
19+
.trim()
20+
.split(' ')
21+
.filter(function(n) {
22+
return n !== '';
23+
}),
24+
subcommand = words.length >= 2 ? words[1] : 'help',
25+
helpmsg =
26+
'**!tipftc** : Displays This Message\n **!tipftc balance** : get your balance\n **!tipftc deposit** : get address for your deposits\n **!tipftc withdraw <ADDRESS> <AMOUNT>** : withdraw coins to specified address\n **!tipftc <@user> <amount>** :mention a user with @ and then the amount to tip them\n **!tipftc private <user> <amount>** : put private before Mentioning a user to tip them privately.\n **<> : Replace with appropriate value.**',
27+
channelwarning = 'Please use <#bot-spam> or DMs to talk to bots.';
28+
switch (subcommand) {
29+
case 'help':
30+
privateorSpamChannel(msg, channelwarning, doHelp, [helpmsg]);
31+
break;
32+
case 'balance':
33+
doBalance(msg, tipper);
34+
break;
35+
case 'deposit':
36+
privateorSpamChannel(msg, channelwarning, doDeposit, [tipper]);
37+
break;
38+
case 'withdraw':
39+
privateorSpamChannel(msg, channelwarning, doWithdraw, [tipper, words, helpmsg]);
40+
break;
41+
default:
42+
doTip(bot, msg, tipper, words, helpmsg);
43+
}
44+
}
45+
};
46+
47+
function privateorSpamChannel(message, wrongchannelmsg, fn, args) {
48+
if (!inPrivateorSpamChannel(message)) {
49+
message.reply(wrongchannelmsg);
50+
return;
51+
}
52+
fn.apply(null, [message, ...args]);
53+
}
54+
55+
function doHelp(message, helpmsg) {
56+
message.author.send(helpmsg);
57+
}
58+
59+
function doBalance(message, tipper) {
60+
ftc.getBalance(tipper, 1, function(err, balance) {
61+
if (err) {
62+
message.reply('Error getting Feathercoin (FTC) balance.').then(message => message.delete(10000));
63+
} else {
64+
message.reply('You have **' + balance + '** Feathercoin (FTC)');
65+
}
66+
});
67+
}
68+
69+
function doDeposit(message, tipper) {
70+
getAddress(tipper, function(err, address) {
71+
if (err) {
72+
message.reply('Error getting your Feathercoin (FTC) deposit address.').then(message => message.delete(10000));
73+
} else {
74+
message.reply('Your Feathercoin (FTC) address is ' + address);
75+
}
76+
});
77+
}
78+
79+
function doWithdraw(message, tipper, words, helpmsg) {
80+
if (words.length < 4) {
81+
doHelp(message, helpmsg);
82+
return;
83+
}
84+
85+
var address = words[2],
86+
amount = getValidatedAmount(words[3]);
87+
88+
if (amount === null) {
89+
message.reply("I don't know how to withdraw that much Feathercoin (FTC)...").then(message => message.delete(10000));
90+
return;
91+
}
92+
93+
ftc.sendFrom(tipper, address, Number(amount), function(err, txId) {
94+
if (err) {
95+
message.reply(err.message).then(message => message.delete(10000));
96+
} else {
97+
message.reply('You withdrew ' + amount + ' Feathercoin (FTC) coins to ' + address + '\n' + txLink(txId) + '\n');
98+
}
99+
});
100+
}
101+
102+
function doTip(bot, message, tipper, words, helpmsg) {
103+
if (words.length < 3 || !words) {
104+
doHelp(message, helpmsg);
105+
return;
106+
}
107+
var prv = false;
108+
var amountOffset = 2;
109+
if (words.length >= 4 && words[1] === 'private') {
110+
prv = true;
111+
amountOffset = 3;
112+
}
113+
114+
let amount = getValidatedAmount(words[amountOffset]);
115+
116+
if (amount === null) {
117+
message.reply("I don't know how to tip that much Feathercoin (FTC)...").then(message => message.delete(10000));
118+
return;
119+
}
120+
if (!message.mentions.users.first()){
121+
message
122+
.reply('Sorry, I could not find a user in your tip...')
123+
.then(message => message.delete(10000));
124+
return;
125+
}
126+
if (message.mentions.users.first().id) {
127+
sendFTC(bot, message, tipper, message.mentions.users.first().id.replace('!', ''), amount, prv);
128+
} else {
129+
message.reply('Sorry, I could not find a user in your tip...').then(message => message.delete(10000));
130+
}
131+
}
132+
133+
function sendFTC(bot, message, tipper, recipient, amount, privacyFlag) {
134+
getAddress(recipient.toString(), function(err, address) {
135+
if (err) {
136+
message.reply(err.message).then(message => message.delete(10000));
137+
} else {
138+
ftc.sendFrom(tipper, address, Number(amount), 1, null, null, function(err, txId) {
139+
if (err) {
140+
message.reply(err.message).then(message => message.delete(10000));
141+
} else {
142+
if (privacyFlag) {
143+
let userProfile = message.guild.members.find('id', recipient);
144+
var iimessage =
145+
' You got privately tipped ' +
146+
amount +
147+
' Feathercoin (FTC)\n' +
148+
txLink(txId) +
149+
'\n' +
150+
'DM me `!tipftc` for ftcTipper instructions.';
151+
userProfile.user.send(iimessage);
152+
var imessage =
153+
' You privately tipped ' +
154+
userProfile.user.username +
155+
' ' +
156+
amount +
157+
' Feathercoin (FTC)\n' +
158+
txLink(txId) +
159+
'\n' +
160+
'DM me `!tipftc` for ftcTipper instructions.';
161+
message.author.send(imessage);
162+
163+
if (
164+
message.content.startsWith('!tipftc private ')
165+
) {
166+
message.delete(1000); //Supposed to delete message
167+
}
168+
} else {
169+
var iiimessage =
170+
' tipped <@' +
171+
recipient +
172+
'> ' +
173+
amount +
174+
' Feathercoin (FTC)\n' +
175+
txLink(txId) +
176+
'\n' +
177+
'DM me `!tipftc` for ftcTipper instructions.';
178+
message.reply(iiimessage);
179+
}
180+
}
181+
});
182+
}
183+
});
184+
}
185+
186+
function getAddress(userId, cb) {
187+
ftc.getAddressesByAccount(userId, function(err, addresses) {
188+
if (err) {
189+
cb(err);
190+
} else if (addresses.length > 0) {
191+
cb(null, addresses[0]);
192+
} else {
193+
ftc.getNewAddress(userId, function(err, address) {
194+
if (err) {
195+
cb(err);
196+
} else {
197+
cb(null, address);
198+
}
199+
});
200+
}
201+
});
202+
}
203+
204+
function inPrivateorSpamChannel(msg) {
205+
if (msg.channel.type == 'dm' || isSpam(msg)) {
206+
return true;
207+
} else {
208+
return false;
209+
}
210+
}
211+
212+
function isSpam(msg) {
213+
return spamchannels.includes(msg.channel.id);
214+
};
215+
216+
217+
function getValidatedAmount(amount) {
218+
amount = amount.trim();
219+
if (amount.toLowerCase().endsWith('ftc')) {
220+
amount = amount.substring(0, amount.length - 3);
221+
}
222+
return amount.match(/^[0-9]+(\.[0-9]+)?$/) ? amount : null;
223+
}
224+
225+
function txLink(txId) {
226+
return 'https://explorer.feathercoin.com/tx/' + txId;
227+
}

0 commit comments

Comments
 (0)