Skip to content

Commit

Permalink
Merge pull request #2 from Atlanta/master
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
Atlanta authored Apr 13, 2020
2 parents fb8bd3c + 1cf2858 commit b1769d8
Show file tree
Hide file tree
Showing 16 changed files with 953 additions and 574 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules/
config.json
config.json
token.json
credentials.json
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ sudo apt-get install build-essential
cd bling-o-tron
npm i
node .
```

# Config reference

Configuration is set in `config.json` :

```json
{
"prefix": "!",
"token": "<discord_bot_token>",
"currency": ":yellow_circle:",
"googleToken": "<google_api_token>",
"spreadsheetId": "<spreadsheet_id>"
}
```
66 changes: 0 additions & 66 deletions commands/add.js

This file was deleted.

6 changes: 5 additions & 1 deletion commands/bank-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const { currency } = require('../config.json');

module.exports = {
name: 'bank-channel',
description: 'Set allowed channels for !bank command on this server.',
description: 'Get/set allowed channels for !bank command on this server.',
args: [
'[add|remove]',
'[#channel-id]'
],
/**
* @param {Discord.Message} message
* @param {string[]} args
Expand Down
66 changes: 44 additions & 22 deletions commands/bank.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const Keyv = require('keyv');
const Discord = require('discord.js');
const { currency } = require('../config.json');
const {google} = require('googleapis');
const { currency, googleToken, spreadsheetId } = require('../config.json');

module.exports = {
name: 'bank',
name: 'bank',
description: 'Shows your personnal bank balance.',
args: [
'[@user]'
],
/**
* @param {Discord.Message} message
* @param {string[]} args
*/
async execute(message, args) {
* @param {Discord.Message} message
* @param {string[]} args
*/
async execute(message, args) {
const db = new Keyv('sqlite://db/' + message.guild.id.toString() + '.sqlite');
const bank = new Keyv('sqlite://db/' + message.guild.id.toString() + '.sqlite', { namespace: 'bank' });

Expand Down Expand Up @@ -37,18 +41,11 @@ module.exports = {
}

const customCurrency = await db.get('config.currency');
let user = 0;

// User wants his account
if (args.length === 0) {
let balance = await bank.get(message.author.id);

if (undefined == balance) {
await bank.set(message.author.id, 0);
balance = 0;
}

console.debug(message.author.tag + ' (' + message.author.id + ') asked for his balance : ' + balance);
message.channel.send('Your balance: ' + balance + (customCurrency || currency));
user = message.author.id;
} else { // Now he wants someone else account
// But only if he has permissions !
if (!message.member.hasPermission('ADMINISTRATOR')) {
Expand All @@ -58,16 +55,41 @@ module.exports = {
let userTag = args[0];
if (!userTag.startsWith('<@!')) {
message.channel.send('Please tag someone !');
return;
}

user = userTag.substring(3, 21);
}

const sheets = google.sheets({version: 'v4', auth: googleToken});
sheets.spreadsheets.values.get({
spreadsheetId,
range: 'Bank!A2:C',
}, (err, res) => {
if (err) {
message.channel.send(`Oops, an error happened. Please retry later. If the problem persist, please contact the support about this!`);
console.error('An error happened while using Google API: ', err);
return;
}
/** @type {string[][]} rows */
const rows = res.data.values;
if (rows.length) {
const row = rows.find(row => {
return row[0] == user;
});

let user = userTag.substring(3, 21);
let balance = await bank.get(user);
if (undefined == balance) balance = 0;
if (!row) {
message.channel.send(`No record was found for this user.`);

console.debug(message.author.tag + ' (' + message.author.id + ') asked for ' + user + ' balance : ' + balance);
message.channel.send(message.guild.member(user).user.tag + '\'s balance: ' + balance.toString() + (customCurrency || currency));
}
},
return;
}

const balance = row[2].replace(/\s/g, '');
message.channel.send(`<@!${user}>'s balance: ${balance}${customCurrency || currency}`);
} else {
message.channel.send(`Oops, an error happened. Please retry later. If the problem persist, please contact the support about this!`);
console.log('No data found in spreadsheet.');
}
});
},
};
5 changes: 4 additions & 1 deletion commands/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const Discord = require('discord.js');

module.exports = {
name: 'currency',
description: 'Set the currency icon for the whole server.',
description: 'Get/set the currency icon for the whole server.',
permission: 'ADMINISTRATOR',
args: [
'[new-currency]'
],
/**
* @param {Discord.Message} message
* @param {string[]} args
Expand Down
50 changes: 50 additions & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const Discord = require('discord.js');

module.exports = {
name: 'help',
description: 'Shows all available commands.',
alias: [
'commands'
],
args: [],
/**
* @param {Discord.Message} message
* @param {string[]} args
*/
execute(message, args) {
const embed = new Discord.MessageEmbed()
.setTitle('Commands')
.setThumbnail(message.client.user.avatarURL())
.setDescription('<required parameter> [optional parameter] ...multiple parameter')
;

const client = message.client;
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(`./${file}`);
client.commands.set(command.name, command);
}

client.commands.forEach(command => {
let aliases = '';

if (command.alias) {
command.alias.forEach(alias => {
aliases += `\`${alias}\` `;
});
}

let args = command.args ? command.args.join(' ') : '';

embed.addField(
'`!' + command.name + (args !== '' ? ' ' + args : '') + '`',
(aliases !== '' ? '*Alias:* ' + aliases + '\n' : '') + command.description
);
});
message.channel.send(embed);
}
};
34 changes: 34 additions & 0 deletions commands/payday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const Keyv = require('keyv');
const Discord = require('discord.js');

module.exports = {
name: 'payday',
description: 'Get/set the next payday date.',
permission: 'ADMINISTRATOR',
args: [
'[next-payday]'
],
/**
* @param {Discord.Message} message
* @param {string[]} args
*/
async execute(message, args) {
const db = new Keyv('sqlite://db/' + message.guild.id.toString() + '.sqlite');

if (args.length < 1) {
const payday = await db.get('config.payday');
message.channel.send('Next payday: ' + payday + '.');

return;
}

if (!message.member.hasPermission(this.permission)) {
return;
}

let payday = args[0];
await db.set('config.payday', payday);

message.channel.send('Next payday set to ' + payday + '.');
},
};
5 changes: 4 additions & 1 deletion commands/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const Discord = require('discord.js');

module.exports = {
name: 'prefix',
description: 'Set commands prefix for this server.',
description: 'Get/set commands prefix for this server.',
args: [
'<new-prefix>'
],
/**
* @param {Discord.Message} message
* @param {string[]} args
Expand Down
66 changes: 0 additions & 66 deletions commands/remove.js

This file was deleted.

Loading

0 comments on commit b1769d8

Please sign in to comment.