Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
greet = (source, user) => {
if (this.lastGreeted === user.id ||
// guest users
user.username === '') {
return
}
this.lastGreeted = user.id
const greeting = random(this.options.greetings)
const message = greeting.replace(/@/g, `@${user.username}`) +
` ${random(this.options.emoji)}`
setTimeout(() => {
source.send(message)
}, 2 * 1000)
}
}
greet = (source, user) => {
if (this.lastGreeted === user.id ||
// guest users
user.username === '') {
return
}
this.lastGreeted = user.id
const greeting = random(this.options.greetings)
const message = greeting.replace(/@/g, `@${user.username}`) +
` ${random(this.options.emoji)}`
setTimeout(() => {
source.send(message)
}, 2 * 1000)
}
}
xit('should pass extra args to underlying action', () => {
const Haha = jest.fn();
const Wow = jest.fn();
const actions = [Haha, Wow];
randomItem.mockReturnValueOnce(Haha);
const action = random(actions);
const context = {
sendText: jest.fn(),
};
const extraArg = {};
action(context, extraArg);
expect(randomItem).toBeCalledWith(actions);
expect(Haha).toBeCalledWith(context, extraArg);
});
it('should create action that will call sendText', async () => {
const Haha = sendText('Haha');
const Wow = sendText('Wow');
const Cool = sendText('Cool');
const actions = [Haha, Wow, Cool];
randomItem.mockReturnValueOnce(Cool);
const Random = random([Haha, Wow, Cool]);
const context = {
sendText: jest.fn(),
};
const Action = await Random(context);
expect(randomItem).toBeCalledWith(actions);
expect(Action).toEqual(Cool);
});
async onEnd () {
this.running = false
clearTimeout(this.timer)
this.timer = null
const players = await this.getPlayers()
if (this.players.length === 0) {
this.source.send('Nobody participated in the raffle... Do I get to win now?')
} else if (players.length === 0) {
this.source.send('Nobody is eligible to win the raffle... Too bad!')
} else {
const winner = random(this.players)
debug('winner', winner.toString())
this.source.send(
this.options.winMessage.replace(/\$winner/g, winner.username)
)
const waitlist = this.source.getWaitlist()
await waitlist.move(winner, this.options.winnerPosition - 1)
}
this.players = []
this.source = null
}
}
async fistbump (message) {
const reasonList = await Karma.find({ target: message.user.id, reason: { $ne: null } })
.where('amount').gt(0)
.populate('giver')
if (reasonList.length === 0) {
message.reply('no one can explain your mysterious allure.')
}
let chosen = random(reasonList)
message.reply(`you were bumped by @${chosen.giver.username} ` +
`${chosen.reason}" ${moment(chosen.date).fromNow()}`)
}
onEnd () {
this._running = false
this._timer = null
let roulette = this._roulette
let players = this.players()
roulette.entrants = this._players.map((p) => p.id)
if (this._players.length === 0) {
this.source.send('Nobody participated in the roulette... Do I get to win now?')
} else if (players.length === 0) {
this.source.send('Nobody is eligible to win the roulette... Too bad!')
} else {
let winner = random(players)
debug('winner', winner.username)
this.source.send(`Roulette winner: @${winner.username}. Congratulations! https://i.imgur.com/TXKz7mt.gif`)
this.bot.moveDJ(winner.id, this.options.winnerPosition - 1, () => {
debug('winner moved')
})
roulette.winner = winner.id
}
RouletteHistory.create(roulette)
this._players = []
}
async fistbump (message) {
const User = this.model('User')
const Karma = this.model('Karma')
const user = await User.from(message.user)
const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
.where('amount').gt(0)
.populate('giver')
if (reasonList.length > 0) {
let chosen = random(reasonList)
message.reply(`you were bumped by @${chosen.giver.username} ` +
`"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
} else {
message.reply('no one can explain your mysterious allure.')
}
}
async fistthump (message) {
const User = this.model('User')
const Karma = this.model('Karma')
const user = await User.from(message.user)
const reasonList = await Karma.find({ target: user._id, reason: { $ne: null } })
.where('amount').lt(0)
.populate('giver')
if (reasonList.length > 0) {
let chosen = random(reasonList)
message.reply(`you were thumped by @${chosen.giver.username} ` +
`"${chosen.reason}" ${moment(chosen.createdAt).fromNow()}`)
} else {
message.reply('no reason was ever given.')
}
}
}
nextQuestion () {
if (this.isRunning()) {
let question
do {
question = random(this.questions)
} while (this._history.indexOf(question) !== -1)
return this.askQuestion(question)
}
return Promise.reject(new Error('Trivia is not running'))
}