Skip to content

Instantly share code, notes, and snippets.

@samyok
Last active July 7, 2020 22:51
Show Gist options
  • Save samyok/18ded3c0079130ba58ab4a11ad25df64 to your computer and use it in GitHub Desktop.
Save samyok/18ded3c0079130ba58ab4a11ad25df64 to your computer and use it in GitHub Desktop.
require('dotenv').config();
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', e => {
console.log("Started bot");
})
const emojis = {
head: ['🍆'],
tails: ['🍑'],
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
client.on('message', (msg) => {
if (msg.author.id === '272115412353679363' && Object.keys(commands).includes(msg.content)) {
commands[msg.content](msg);
}
if (!msg.guild && !msg.author.bot) {
gotAnswerFor(msg);
}
})
let currentGame = {};
let channelToDelete = null;
const commands = {
'!start': msg => {
if (msg.member.voice.channel) {
let users = msg.member.voice.channel.members.array().filter(e => !e.user.bot).map(e => `<@${e.id}>`).join(", ");
let permissions = msg.member.voice.channel.members.array().map(e => ({
id: e.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
}))
permissions.push({
id: msg.guild.roles.everyone.id,
deny: ['VIEW_CHANNEL']
})
msg.guild.channels.create('paranoia-' + makeid(4), {
type: 'text',
topic: 'play paranoia here (channel deletes when game ends)',
nsfw: false,
position: 16,
permissionOverwrites: permissions
}).then(r => {
channelToDelete = r;
msg.reply(`starting game in <#${r.id}> with ${users}.`);
r.send('@everyone')
startRound(msg.member.voice.channel.members.filter(e => !e.user.bot), r);
})
} else {
msg.reply("you're not in a vc!");
}
},
'!reveal': async msg => {
let x = '';
while (x !== 'done') {
await currentGame.channel.send('.\n\n\n\n\n.')
x = await reveal();
}
currentGame = {};
}
}
function reveal() {
return new Promise(resolve => {
if (!currentGame.combined || currentGame.combined.length === 0) {
currentGame.channel.send("That's it! Send !start to start over. Channel will delete in 10 seconds :)")
setTimeout(() => {
channelToDelete.delete('We are deleting a channel');
}, 10000)
return resolve('done');
}
let currentQuestion = currentGame.combined[0];
const a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let answer = currentQuestion.answer;
answer = answer.split("").map(l => a.includes(l) ? `:regional_indicator_${l.toLowerCase()}:` : l).join(" ");
currentGame.channel.send(`<@${currentQuestion.answerProvider.id}> answered with`);
currentGame.channel.send(answer);
currentGame.channel.send('Flipping coin..')
setTimeout(() => {
if (currentQuestion.doReveal) {
currentGame.channel.send(emojis.head[emojis.head.length * Math.random() >> 0])
currentGame.channel.send('(head)')
setTimeout(() => {
currentGame.channel.send(`<@${currentQuestion.questionProvider.id}>'s question: ${currentQuestion.question} \n <@${currentQuestion.answerProvider.id}>'s answer: ${currentQuestion.answer}`)
setTimeout(resolve, 7000);
}, 6000 * Math.random() + 1000)
} else {
currentGame.channel.send(emojis.tails[emojis.tails.length * Math.random() >> 0])
currentGame.channel.send('(tails)')
setTimeout(() => {
currentGame.channel.send(`We'll never know <@${currentQuestion.questionProvider.id}>'s question 🤐`)
setTimeout(resolve, 7000);
}, 3000 * Math.random() + 500)
}
}, 10000 * Math.random() + 1000)
currentGame.combined = currentGame.combined.filter((_, i) => i > 0);
})
}
function gotAnswerFor({author, content}) {
if (currentGame.state === 'questions') {
currentGame.questions[author.id] = content;
currentGame.needAnswersFrom = currentGame.needAnswersFrom.filter(e => e !== author.id);
if (currentGame.needAnswersFrom.length > 0) {
currentGame.channel.send(`Got question from ${author.username}, need from ${currentGame.needAnswersFrom.map(e => `<@${e}>`).join(" ")}!`)
} else {
currentGame.channel.send(`Got questions! Sending questions 😎 (waiting for answers)`);
currentGame.state = "answers";
currentGame.needAnswersFrom = currentGame.randomizedArr.map(e => e.id);
for (let i = 0; i < currentGame.randomizedArr.length; i++) {
let previous = i - 1;
if (previous < 0) previous += currentGame.randomizedArr.length; // current game - 1
console.log(previous);
currentGame.randomizedArr[i].send({
"embed": {
"title": "Paranoia Bot",
"description": "**Rules: [click here](https://www.notion.so/sdsusamyok/Paranoia-Rules-af960f975bdb4d64b56aa8757c77254b)**",
"color": 7539055,
"footer": {
"icon_url": "https://unavatar.now.sh/eggsthedev",
"text": "Created by eggsthe.dev"
},
"fields": [
{
"name": "YOU ARE ANSWERING",
"value": `From: <@${currentGame.randomizedArr[previous].id}> (${currentGame.randomizedArr[previous].user.username})\n\nQuestion:`
},
{
"name": currentGame.questions[currentGame.randomizedArr[previous].id],
"value": `⁉\n\n\nThe next thing you send will be your answer. Respond with JUST the name of the person. The person MUST be in the call and not <@${currentGame.randomizedArr[previous].id}> or <@${currentGame.randomizedArr[i].id}>.`
}
]
}
}).catch(e => {
console.log(e);
currentGame.channel.send(`OH NO! We can't send dms to <@${currentGame.randomizedArr[i].id}>`)
})
}
}
} else if (currentGame.state === "answers") {
currentGame.answers[author.id] = content;
currentGame.needAnswersFrom = currentGame.needAnswersFrom.filter(e => e !== author.id);
if (currentGame.needAnswersFrom.length > 0) {
currentGame.channel.send(`Got answer from ${author.username}, need from ${currentGame.needAnswersFrom.map(e => `<@${e}>`).join(" ")}!`)
} else {
currentGame.channel.send("Got answers! Do `!reveal` to reveal.");
currentGame.combined = [];
for (let i = 0; i < currentGame.randomizedArr.length; i++) {
let questionProvider = i - 1;
if (questionProvider < 0) questionProvider += currentGame.randomizedArr.length;
let answerProvider = i;
currentGame.combined.push({
questionProvider: currentGame.randomizedArr[questionProvider],
question: currentGame.questions[currentGame.randomizedArr[questionProvider].id],
answerProvider: currentGame.randomizedArr[answerProvider],
answer: currentGame.answers[currentGame.randomizedArr[answerProvider].id],
doReveal: Math.random() > .5
})
}
// currentGame.channel.send("```json\n" + JSON.stringify(currentGame.combined.map(e => ({
// ...e,
// questionProvider: e.questionProvider.user.username,
// answerProvider: e.answerProvider.user.username
// })), null, 4) + "\n```")
}
}
}
function startRound(membersCollection, channel) {
let randomizedArr = membersCollection.array().sort((a, b) => Math.random() > 0.5 ? 1 : -1);
console.log(randomizedArr.map(e => e.user.username))
currentGame = {
channel,
membersCollection,
randomizedArr,
questions: {},
answers: {},
needAnswersFrom: randomizedArr.map(e => e.id),
state: 'questions'
}
// send a dm to each person
// wait for response
for (let i = 0; i < randomizedArr.length; i++) {
let nextPerson = i + 1;
if (i === randomizedArr.length - 1) nextPerson = 0;
randomizedArr[i].send({
"embed": {
"title": "Paranoia Bot",
"description": "**Rules: [click here](https://i.kym-cdn.com/photos/images/original/000/995/850/dae.jpg)**",
"color": 7539055,
"footer": {
"icon_url": "https://unavatar.now.sh/eggsthedev",
"text": "Created by eggsthe.dev"
},
"fields": [
{
"name": "YOU ARE ASKING",
"value": ` ${randomizedArr[nextPerson].user.username}: <@${randomizedArr[nextPerson].id}>`
},
{
"name": "The next message you send will be the question we ask.",
"value": "Have fun! 😎"
}
]
}
}).catch(e => {
console.log(e);
channel.send(`OH NO! We can't send dms to <@${randomizedArr[i].id}>`)
})
}
channel.send('Asked everyone for questions...');
}
client.login(process.env.DISCORD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment