Skip to content

Commit

Permalink
update channel and pin messages on game start
Browse files Browse the repository at this point in the history
  • Loading branch information
DiabolusGX committed Mar 9, 2021
1 parent d700b20 commit 50f131f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Setup command will let you add/edit/remove bot settings like :

To **start game** admin or bot-manager need to enter just 1 command :
`gg start <min> <max> [channel]` - Bot will start game in mentioned channel (current channel, if not mentioned) with a random number between `min` & `max` value provided.
- Bot will DM the answer to the game organizer (who runs the command)
- Bot will DM the answer to the game organizer (who runs the command).
- Bot will pin the game start and game end message, lock the channel after game ends for given role and add "🔒" emoji to channel name.
- Make sure to remove pins and "🔒" from channel before sarting next game to avoid confusion.
- Bot will un-pin all messages sent by bot except for game-end/winning message and remove "🔒" emoji (Sometimes it can hit rate limits).

To **give hint** if you feel game is too difficult you can use :
`gg hint <first | last | number> [channel]`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mod/hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
else targetChannel = message.channel;

if (!client.games.has(targetChannel.id)) return message.channel.send(`${client.myEmojis[1]} | **No game going on in** ${targetChannel}`);
const answer = client.games.get(targetChannel.id);
const answer = client.games.get(targetChannel.id).answer;

let hint;
if (args[0] === "first") {
Expand Down
21 changes: 16 additions & 5 deletions src/commands/mod/start.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const guildConfigModel = require("../../database/models/guildConfig");
const guildDataModel = require("../../database/models/guildData");
const find = require("../../utils/find");

Expand All @@ -13,6 +14,7 @@ module.exports = {
async run(client, message, args) {
const dbPrefix = client.guildConfigPrefix.get(message.guild.id);
const dataDoc = await guildDataModel.findOne({ id: message.guild.id });
const guildConfig = await guildConfigModel.findOne({ id: message.guild.id });

if (!args[0] || !args[1] || isNaN(args[0]) || isNaN(args[1]) || parseInt(args[0]) > parseInt(args[1]))
return message.channel.send(`${client.myEmojis[1]} | **Minimum and Maximum value missing!**\n` +
Expand All @@ -22,21 +24,30 @@ module.exports = {

const min = parseInt(args[0]);
const max = parseInt(args[1]);
const points = Math.floor( (max-min) / 10 );
const points = Math.floor((max - min) / 10);
let targetChannel;
if (args[2] || message.mentions.channels.first()) {
targetChannel = await find.getChannel(message, args[2]);
}
else targetChannel = message.channel;
if(client.games.has(targetChannel.id)) return message.channel.send(`${client.myEmojis[1]} | There is already a game going on in ${targetChannel}`);
if (client.games.has(targetChannel.id)) return message.channel.send(`${client.myEmojis[1]} | There is already a game going on in ${targetChannel}`);

const randomAnswer = Math.floor(Math.random() * (max - min) + min);

const totalGames = dataDoc.totalGames;
const games = dataDoc.runningGames;
games.set(targetChannel.id, {answer: randomAnswer, points: points });
await guildDataModel.updateOne({ id: message.guild.id }, { $set: { runningGames: games, totalGames: totalGames+1 } }, (err) => console.error);
client.games.set(targetChannel.id, randomAnswer);
games.set(targetChannel.id, { answer: randomAnswer, points: points });
await guildDataModel.updateOne({ id: message.guild.id }, { $set: { runningGames: games, totalGames: totalGames + 1 } }, (err) => console.error);
client.games.set(targetChannel.id, { answer: randomAnswer, guesses: 0 });

if (guildConfig.lockRole) {
const role = await find.getRole(message, guildConfig.lockRole);
message.channel.updateOverwrite(role, { SEND_MESSAGES: true })
.then(channel => {
if (channel.name.endsWith("🔒")) channel.edit({ name: channel.name.slice(0, -1) });
})
.catch(console.error);
}

return message.channel.send(`${client.myEmojis[0]} | **Started Game** in ${targetChannel}`).then(msg => {
targetChannel.send(`${client.myEmojis[0]} | **Started Game**\nMin number is \`${min}\` & Max number is \`${max}\``)
Expand Down
9 changes: 7 additions & 2 deletions src/events/message/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ module.exports = async (client, message) => {

if (message.author.bot || message.channel.type !== "text") return;

if(!isNaN(message.content) && client.games.has(message.channel.id) && parseInt(message.content) === client.games.get(message.channel.id))
return answerFound(client, message);
if(!isNaN(message.content) && client.games.has(message.channel.id)) {
if(parseInt(message.content) === client.games.get(message.channel.id).answer) return answerFound(client, message);
else {
const { answer, guesses } = client.games.get(message.channel.id);
client.games.set(message.channel.id, { answer: answer, guesses: guesses+1 });
}
}

const dbPrefix = await client.guildConfigPrefix.get(message.guild.id) || process.env.PREFIX;
const botMentioned = message.content.search(/<@!818420448131285012>/i);
Expand Down
4 changes: 2 additions & 2 deletions src/events/misc/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = async (client) => {
client.guilds.cache.forEach(async guild => {
await configDoc(client, guild);
const data = await dataDoc(client, guild);
if (data.runningGames) data.runningGames.forEach((value, key) => client.games.set(key, value.answer));
if (data.runningGames) data.runningGames.forEach((value, key) => client.games.set(key, { answer: value.answer, guesses: 0 }));
});

client.user.setActivity("Your Guesses || gg help", { type: "LISTENING" });
client.user.setActivity("your guesses || gg help", { type: "LISTENING" });
console.log(client.user.tag + " is up and running in " + client.guilds.cache.size + " servers!");
}
31 changes: 24 additions & 7 deletions src/utils/answerFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = async (client, message) => {

const guildData = await guildDataModel.findOne({ id: message.guild.id }).catch(console.error);

const guesses = client.games.get(message.channel.id).guesses;
client.games.delete(message.channel.id);
const games = guildData.runningGames;
const { answer, points } = games.get(message.channel.id);
Expand All @@ -34,21 +35,37 @@ module.exports = async (client, message) => {
else users.set(message.author.id, { wins: 1, points: points });
await guildDataModel.updateOne({ id: message.guild.id }, { $set: { runningGames: games, users: users } }, (err) => console.error);

if (guildConfig.winRole) message.member.roles.add(guildConfig.winRole, "Auto Role number guess").catch(console.error);
let reward = "";
if (guildConfig.winRole) {
const awardRole = await find.getRole(message, guildConfig.winRole);
reward = `and **${awardRole.name}** role.`;
message.member.roles.add(guildConfig.winRole, "Auto Role number guess").catch(console.error);
}

if (guildConfig.dm) {
message.author.send(`${client.myEmojis[0]} | **Congratulations** 🎉\nYou guessed the correct number ||${answer}|| in ${message.channel} and won **${points} Points!**`);
}
message.channel.send(`${client.myEmojis[0]} | **Congratulations** ${message.author} 🎉\nYou guessed the correct number ||${answer}|| and won **${points} Points!**`)
.then(msg => {
message.channel.send(`${client.myEmojis[0]} | **Congratulations** ${message.author} 🎉\nYou guessed the correct number ||${answer}|| after total of **${guesses}** guesses!\n`+
`Also, you received **${points} Points!** ${reward}`).then(msg => {
if (msg.pinnable) msg.pin({ reason: "win message" }).catch(console.error);
})
.catch(console.error);
}).catch(console.error);

if (guildConfig.lockRole) {
const role = await find.getRole(message, guildConfig.lockRole);
return message.channel.updateOverwrite(role, { SEND_MESSAGES: false })
.then(channel => channel.edit({ name: channel.name + " 🔒" }))
message.channel.updateOverwrite(role, { SEND_MESSAGES: false })
.then(channel => {
if(!channel.name.endsWith("🔒")) channel.edit({ name: channel.name + " 🔒" });
})
.catch(console.error);
}

return message.channel.messages.fetchPinned()
.then(messages => {
messages.forEach(m => {
if(m.author.id === "818420448131285012" && !m.content.startsWith(`${client.myEmojis[0]} | **Congratulations**`))
m.unpin({ reason: "game ended" }).catch(console.error);
})
})
.catch(console.error);

}

0 comments on commit 50f131f

Please sign in to comment.