Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce prettier fixing and add shortcut #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .huskyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hooks": {
"pre-commit": "lint-staged"
}
}
6 changes: 6 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"scripts/**/*.js": [
"prettier --write",
"git add"
]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "chewie",
"version": "0.0.0",
"version": "0.1.0",
"private": true,
"author": "Scott Street <[email protected]>",
"description": "A handy bot for doing Colony things",
"scripts": {
"start": "env-cmd .env yarn run hubot --name chewie -a slack",
"dev": "env-cmd .env yarn run hubot --name chewie"
"dev": "env-cmd .env yarn run hubot --name chewie",
"format": "prettier 'scripts/**/*.js' --write"
},
"dependencies": {
"@colony/colony-contract-loader-network": "^1.0.1",
Expand All @@ -32,6 +33,8 @@
"node": "0.10.x"
},
"devDependencies": {
"husky": "^3.0.5",
"lint-staged": "^9.4.0",
"prettier": "^1.13.7"
}
}
97 changes: 54 additions & 43 deletions scripts/colony-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

const { providers, Wallet } = require('ethers')
const { default: EthersAdapter } = require('@colony/colony-js-adapter-ethers')
const { default: NetworkLoader } = require('@colony/colony-contract-loader-network')
const {
default: NetworkLoader
} = require('@colony/colony-contract-loader-network')
const { default: ColonyNetworkClient } = require('@colony/colony-js-client')

module.exports = async (robot) => {
module.exports = async robot => {
const network = 'rinkeby'
const privateKey = process.env.HUBOT_ETHEREUM_PRIVATE_KEY || '0x0123456789012345678901234567890123456789012345678901234567890123'
const privateKey =
process.env.HUBOT_ETHEREUM_PRIVATE_KEY ||
'0x0123456789012345678901234567890123456789012345678901234567890123'

const loader = new NetworkLoader({ network })
const provider = new providers.EtherscanProvider(network)
Expand All @@ -23,7 +27,7 @@ module.exports = async (robot) => {
const adapter = new EthersAdapter({
loader,
provider,
wallet,
wallet
})

const networkClient = new ColonyNetworkClient({ adapter })
Expand All @@ -32,72 +36,79 @@ module.exports = async (robot) => {
const metaColonyClient = await networkClient.getMetaColonyClient()

robot.hear(/!colony ([0-9]*)$/i, async msg => {
msg.send('Gathering data...');
const { address } = await networkClient.getColony.call({ id: parseInt(msg.match[1], 10) })
msg.send('Gathering data...')
const { address } = await networkClient.getColony.call({
id: parseInt(msg.match[1], 10)
})
if (!address) {
return msg.send("No such colony");
return msg.send('No such colony')
}
const colonyClient = await networkClient.getColonyClientByAddress(address)
const { count } = await colonyClient.getTaskCount.call();
let res = await colonyClient.getToken.call();
const tokenAddress = res.address;
let tokenName;
let tokenSymbol;
const { count } = await colonyClient.getTaskCount.call()
let res = await colonyClient.getToken.call()
const tokenAddress = res.address
let tokenName
let tokenSymbol
try {
res = await colonyClient.token.getTokenInfo.call();
tokenName = res.name;
tokenSymbol = res.symbol;
} catch (err){
res = await colonyClient.token.getTokenInfo.call()
tokenName = res.name
tokenSymbol = res.symbol
} catch (err) {
// No such properties on the token - possible if not ERC20 compliant, as BYOT allows
}

msg.send(`Address: ${address} \nTask count: ${count}\nToken Address: ${tokenAddress}\nToken Name: ${tokenName} (${tokenSymbol})`)
msg.send(
`Address: ${address} \nTask count: ${count}\nToken Address: ${tokenAddress}\nToken Name: ${tokenName} (${tokenSymbol})`
)
})


robot.hear(/!colony ([0-9]*) task ([0-9]*)$/i, async msg => {
msg.send('Gathering data...')
const { address } = await networkClient.getColony.call({ id: parseInt(msg.match[1], 10) })
const taskId = parseInt(msg.match[2], 10);
const { address } = await networkClient.getColony.call({
id: parseInt(msg.match[1], 10)
})
const taskId = parseInt(msg.match[2], 10)
if (!address) {
return msg.send("No such colony");
return msg.send('No such colony')
}
const colonyClient = await networkClient.getColonyClientByAddress(address);
const task = await colonyClient.getTask.call({taskId});
let output = "";
if (task.specificationHash){
output += "Specification: https://gateway.ipfs.io/ipfs/QmTDMoVqvyBkNMRhzvukTDznntByUNDwyNdSfV8dZ3VKRC/\n"
const colonyClient = await networkClient.getColonyClientByAddress(address)
const task = await colonyClient.getTask.call({ taskId })
let output = ''
if (task.specificationHash) {
output +=
'Specification: https://gateway.ipfs.io/ipfs/QmTDMoVqvyBkNMRhzvukTDznntByUNDwyNdSfV8dZ3VKRC/\n'
} else {
output += "Specification: None\n"
output += 'Specification: None\n'
}

if (task.deliverableHash){
output += "Deliverable: https://gateway.ipfs.io/ipfs/QmTDMoVqvyBkNMRhzvukTDznntByUNDwyNdSfV8dZ3VKRC/\n"
if (task.deliverableHash) {
output +=
'Deliverable: https://gateway.ipfs.io/ipfs/QmTDMoVqvyBkNMRhzvukTDznntByUNDwyNdSfV8dZ3VKRC/\n'
} else {
output += "Deliverable: None\n"
output += 'Deliverable: None\n'
}
output += `Finalized: ${task.finalized ? "Yes" : "No"}\n`
output += `Cancelled: ${task.cancelled ? "Yes" : "No"}`
output += `Finalized: ${task.finalized ? 'Yes' : 'No'}\n`
output += `Cancelled: ${task.cancelled ? 'Yes' : 'No'}`
output += `\nDue date: `
if (task.dueDate){
if (task.dueDate) {
output += ` ${new Date(task.dueDate * 1000).toISOString()}`
} else {
output += ` none`
}
output += `\nDeliverable date: `;
if (task.deliverableDate){
output += `\nDeliverable date: `
if (task.deliverableDate) {
output += `${new Date(task.deliverableDate * 1000).toISOString()}`
} else {
output += ` none`;
output += ` none`
}

let res = await colonyClient.getTaskRole.call({taskId, role: 'MANAGER'} )
output += `\nManager: ${ res.address }`
res = await colonyClient.getTaskRole.call({taskId, role: 'EVALUATOR'} )
output += `\nEvaluator: ${ res.address }`
res = await colonyClient.getTaskRole.call({taskId, role: 'WORKER'} )
output += `\nWorker: ${ res.address }`
let res = await colonyClient.getTaskRole.call({ taskId, role: 'MANAGER' })
output += `\nManager: ${res.address}`
res = await colonyClient.getTaskRole.call({ taskId, role: 'EVALUATOR' })
output += `\nEvaluator: ${res.address}`
res = await colonyClient.getTaskRole.call({ taskId, role: 'WORKER' })
output += `\nWorker: ${res.address}`

msg.send(output);
msg.send(output)
})
}
140 changes: 67 additions & 73 deletions scripts/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,137 +17,131 @@
// Author:
// JamesLefrere

const CronJob = require('cron').CronJob;
const CronJob = require('cron').CronJob

const getBrain = require('./utils/brain');
const getBrain = require('./utils/brain')

const {
getOffsetDate,
parseNaturalDate,
} = require('./utils/dates');
const { getOffsetDate, parseNaturalDate } = require('./utils/dates')

const { isPrivateSlackMessage } = require('./utils/channels');
const { isPrivateSlackMessage } = require('./utils/channels')

const BRAIN_PREFIX = 'countdown';
const COUNTDOWNS = 'countdowns';
const BRAIN_PREFIX = 'countdown'
const COUNTDOWNS = 'countdowns'

const {
addToMap,
getFromMap,
getMap,
removeFromMap,
} = getBrain(BRAIN_PREFIX);
const { addToMap, getFromMap, getMap, removeFromMap } = getBrain(BRAIN_PREFIX)

const getSass = hours => {
if (hours > 24 * 7 * 4) return 'An ocean of time.';
if (hours > 24 * 7 * 3.5) return 'Ages, mate.';
if (hours > 24 * 7 * 3) return `It's getting closer, but it'll be fine.`;
if (hours > 24 * 7 * 2.5) return 'Watch out for this one.';
if (hours > 24 * 7 * 2) return 'Did you try working faster?';
if (hours > 24 * 7 * 1.5) return `That's concerning.`;
if (hours > 24 * 7) return `That's quite soon if you think about it.`;
if (hours > 24 * 6) return `Well that doesn't sound right...`;
if (hours > 24 * 5) return 'A week?! A mere working week?!';
if (hours > 24 * 4) return 'Shit, we can do it!';
if (hours > 24 * 3) return 'A'.repeat(20);
if (hours > 24 * 2) return `I'll give you 1 ETH if you finish it today.`;
if (hours > 24) return `*${'A'.repeat(200)}*`;
return '*PANIC MODE ENGAGE!!!* gogogogogogogogogogogogogo54321111111glhf';
};

const getKey = title => title.replace(/\s/g, '');
if (hours > 24 * 7 * 4) return 'An ocean of time.'
if (hours > 24 * 7 * 3.5) return 'Ages, mate.'
if (hours > 24 * 7 * 3) return `It's getting closer, but it'll be fine.`
if (hours > 24 * 7 * 2.5) return 'Watch out for this one.'
if (hours > 24 * 7 * 2) return 'Did you try working faster?'
if (hours > 24 * 7 * 1.5) return `That's concerning.`
if (hours > 24 * 7) return `That's quite soon if you think about it.`
if (hours > 24 * 6) return `Well that doesn't sound right...`
if (hours > 24 * 5) return 'A week?! A mere working week?!'
if (hours > 24 * 4) return 'Shit, we can do it!'
if (hours > 24 * 3) return 'A'.repeat(20)
if (hours > 24 * 2) return `I'll give you 1 ETH if you finish it today.`
if (hours > 24) return `*${'A'.repeat(200)}*`
return '*PANIC MODE ENGAGE!!!* gogogogogogogogogogogogogo54321111111glhf'
}

const getKey = title => title.replace(/\s/g, '')

const processCountdowns = robot => {
const { brain } = robot;
const currentDate = getOffsetDate(-11);
const { brain } = robot
const currentDate = getOffsetDate(-11)

Object.entries(getMap(COUNTDOWNS, brain)).forEach(
([key, { title, dueDate, room }]) => {
const diff = new Date(dueDate).getTime() - new Date(currentDate).getTime();
const diff = new Date(dueDate).getTime() - new Date(currentDate).getTime()

if (diff < 0) {
robot.messageRoom(room, `${title}: due date elapsed!`);
return removeFromMap(COUNTDOWNS, key, brain);
robot.messageRoom(room, `${title}: due date elapsed!`)
return removeFromMap(COUNTDOWNS, key, brain)
}

const hours = (diff / (1000 * 60 * 60)).toFixed(2);
const hours = (diff / (1000 * 60 * 60)).toFixed(2)

robot.messageRoom(
room,
`${title}: ${hours} hours remaining. ${getSass(hours)}`
);
});
};
)
}
)
}

const setupCronJob = robot => {
const job = new CronJob({
// Every weekday 23:45h
cronTime: '00 45 23 * * *',
onTick: () => {
processCountdowns(robot);
processCountdowns(robot)
},
start: false,
// Last time zone of the day (UTC-11)
timeZone: 'Pacific/Niue'
});
job.start();
};
})
job.start()
}

module.exports = robot => {
const { brain } = robot;
setupCronJob(robot);
const { brain } = robot
setupCronJob(robot)

robot.hear(/^countdown add '(.+)' (.+)$/, res => {
const { message: { user, room }, match } = res;
const {
message: { user, room },
match
} = res

if (isPrivateSlackMessage(res)) {
return res.send('Countdowns can only be added in a channel.');
return res.send('Countdowns can only be added in a channel.')
}

if (user.slack.tz_offset == null) {
return res.send('Please set your time zone in slack first')
}

const title = match[1];
const dueDate = parseNaturalDate(match[2], user);
const title = match[1]
const dueDate = parseNaturalDate(match[2], user)

const key = getKey(title);
const existing = getFromMap(COUNTDOWNS, key, brain);
const key = getKey(title)
const existing = getFromMap(COUNTDOWNS, key, brain)

if (existing) {
return res.send('Oops, a countdown with that title already exists');
return res.send('Oops, a countdown with that title already exists')
}

addToMap(
COUNTDOWNS,
key,
{ title, dueDate, room, userId: user.id },
brain
);
addToMap(COUNTDOWNS, key, { title, dueDate, room, userId: user.id }, brain)

return res.send('The countdown begins!');
});
return res.send('The countdown begins!')
})

robot.hear(/^countdown status$/, () => {
processCountdowns(robot);
});
processCountdowns(robot)
})

robot.hear(/^countdown remove '(.*)'$/, res => {
const { message: { user }, match } = res;
const {
message: { user },
match
} = res

const key = getKey(match[1]);
const existing = getFromMap(COUNTDOWNS, key, brain);
const key = getKey(match[1])
const existing = getFromMap(COUNTDOWNS, key, brain)

if (!existing) {
return res.send(`That countdown doesn't exist`);
return res.send(`That countdown doesn't exist`)
}

if (user.id !== existing.userId) {
return res.send(`Only user ID ${userId} can remove that countdown`);
return res.send(`Only user ID ${userId} can remove that countdown`)
}

removeFromMap(COUNTDOWNS, key, brain);
removeFromMap(COUNTDOWNS, key, brain)

return res.send('Countdown removed');
});
};
return res.send('Countdown removed')
})
}
Loading