Merge pull request #13 from cassoule/milo-250505

Milo 250505
This commit is contained in:
Milo Gourvest
2025-05-06 20:26:31 +02:00
committed by GitHub
3 changed files with 112 additions and 136 deletions

121
game.js
View File

@@ -1,84 +1,8 @@
import { capitalize } from './utils.js';
export function getResult(p1, p2) {
let gameResult;
if (RPSChoices[p1.objectName] && RPSChoices[p1.objectName][p2.objectName]) {
// o1 wins
gameResult = {
win: p1,
lose: p2,
verb: RPSChoices[p1.objectName][p2.objectName],
};
} else if (
RPSChoices[p2.objectName] &&
RPSChoices[p2.objectName][p1.objectName]
) {
// o2 wins
gameResult = {
win: p2,
lose: p1,
verb: RPSChoices[p2.objectName][p1.objectName],
};
} else {
// tie -- win/lose don't
gameResult = { win: p1, lose: p2, verb: 'tie' };
}
import { updateUserCoins, getUser } from './init_database.js'
return formatResult(gameResult);
}
function formatResult(result) {
const { win, lose, verb } = result;
return verb === 'tie'
? `<@${win.id}> and <@${lose.id}> draw with **${win.objectName}**`
: `<@${win.id}>'s **${win.objectName}** ${verb} <@${lose.id}>'s **${lose.objectName}**`;
}
// this is just to figure out winner + verb
const RPSChoices = {
rock: {
description: 'sedimentary, igneous, or perhaps even metamorphic',
virus: 'outwaits',
computer: 'smashes',
scissors: 'crushes',
},
cowboy: {
description: 'yeehaw~',
scissors: 'puts away',
wumpus: 'lassos',
rock: 'steel-toe kicks',
},
scissors: {
description: 'careful ! sharp ! edges !!',
paper: 'cuts',
computer: 'cuts cord of',
virus: 'cuts DNA of',
},
virus: {
description: 'genetic mutation, malware, or something inbetween',
cowboy: 'infects',
computer: 'corrupts',
wumpus: 'infects',
},
computer: {
description: 'beep boop beep bzzrrhggggg',
cowboy: 'overwhelms',
paper: 'uninstalls firmware for',
wumpus: 'deletes assets for',
},
wumpus: {
description: 'the purple Discord fella',
paper: 'draws picture on',
rock: 'paints cute face on',
scissors: 'admires own reflection in',
},
paper: {
description: 'versatile and iconic',
virus: 'ignores',
cowboy: 'gives papercut to',
rock: 'covers',
},
};
const messagesTimestamps = new Map();
const TimesChoices = [
{
@@ -147,28 +71,35 @@ const TimesChoices = [
},*/
];
export function getRPSChoices() {
return Object.keys(RPSChoices);
}
export function getTimesChoices() {
return TimesChoices
}
// Function to fetch shuffled options for select menu
export function getShuffledOptions() {
const allChoices = getRPSChoices();
const options = [];
export function channelPointsHandler(msg) {
const author = msg.author
const authorDB = getUser.get(author.id)
for (let c of allChoices) {
// Formatted for select menus
// https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
options.push({
label: capitalize(c),
value: c.toLowerCase(),
description: RPSChoices[c]['description'],
});
if (!authorDB) {
console.log("message from an unknown user")
return
}
return options.sort(() => Math.random() - 0.5);
const now = Date.now();
const timestamps = messagesTimestamps.get(author.id) || [];
// Remove all timestamps if first one is older than 15 minutes
const updatedTimestamps = now - timestamps[0] < 900000 ? timestamps : [];
updatedTimestamps.push(now);
messagesTimestamps.set(author.id, updatedTimestamps);
if (messagesTimestamps.get(author.id).length <= 10) {
// +10 or +50 coins
updateUserCoins.run({
id: author.id,
coins: messagesTimestamps.get(author.id).length === 10
? authorDB.coins + 50
: authorDB.coins + 10,
})
}
}

123
index.js
View File

@@ -18,29 +18,32 @@ import {
getAPOUsers,
postAPOBuy
} from './utils.js';
import { getShuffledOptions, getResult } from './game.js';
import { channelPointsHandler } from './game.js';
import { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js';
import cron from 'node-cron';
import { flopoDB,
insertUser,
insertManyUsers,
updateUser,
updateManyUsers,
getUser,
getAllUsers,
stmtUsers,
stmtSkins,
updateManySkins,
insertSkin,
updateSkin,
insertManySkins,
getAllSkins,
getSkin,
getAllAvailableSkins,
getUserInventory,
getTopSkins,
} from './init_database.js';
import Database from "better-sqlite3";
import {
flopoDB,
insertUser,
insertManyUsers,
updateUser,
updateManyUsers,
getUser,
getAllUsers,
stmtUsers,
stmtSkins,
updateManySkins,
insertSkin,
updateSkin,
insertManySkins,
getAllSkins,
getSkin,
getAllAvailableSkins,
getUserInventory,
getTopSkins, updateUserCoins,
} from './init_database.js';
import { getValorantSkins, getSkinTiers } from './valo.js';
import {sleep} from "openai/core";
// Create an express app
const app = express();
@@ -294,7 +297,18 @@ client.login(process.env.BOT_TOKEN);
client.on('messageCreate', async (message) => {
// Ignore messages from bots to avoid feedback loops
if (message.author.bot) return;
if (message.guildId !== process.env.GUILD_ID) return;
// hihihiha
if (message.author.id === process.env.PATA_ID) {
if (message.content.startsWith('feur')
|| message.content.startsWith('rati')) {
await sleep(1000)
await message.delete()
}
}
// coins mecanich
if (message.guildId === process.env.GUILD_ID) channelPointsHandler(message)
if (message.content.toLowerCase().startsWith(`<@${process.env.APP_ID}>`) || message.mentions.repliedUser?.id === process.env.APP_ID) {
let startTime = Date.now()
@@ -478,25 +492,54 @@ client.on('messageCreate', async (message) => {
.catch(console.error);
}
}
else if (message.content.toLowerCase().startsWith('membres')) {
let content = ``
const allAkhys = await getAllUsers.all()
allAkhys.forEach((akhy) => content += `> ### ${akhy.globalName} \n > **${akhy.totalRequests}** requests \n > **${akhy.warns}** warns \n > **${akhy.allTimeWarns}** all-time warns \n\n`);
message.channel.send(`${content}`)
.catch(console.error);
}
else if (message.content.toLowerCase().startsWith('?u')) {
console.log(await getAPOUsers())
}
else if (message.content.toLowerCase().startsWith('?b')) {
const amount = message.content.replace('?b ', '')
console.log(amount)
console.log(await postAPOBuy('650338922874011648', amount))
}
else if (message.content.toLowerCase().startsWith('?v')) {
console.log('active polls :')
console.log(activePolls)
else if (message.guildId === process.env.DEV_GUILD_ID) {
// ADMIN COMMANDS
if (message.content.toLowerCase().startsWith('?u')) {
console.log(await getAPOUsers())
}
else if (message.content.toLowerCase().startsWith('?b')) {
const amount = message.content.replace('?b ', '')
console.log(amount)
console.log(await postAPOBuy('650338922874011648', amount))
}
else if (message.content.toLowerCase().startsWith('?v')) {
console.log('active polls :')
console.log(activePolls)
}
else if (message.author.id === process.env.DEV_ID) {
if (message.content === 'flopo:add-coins-to-users') {
console.log(message.author.id)
try {
const stmtUpdateUsers = flopoDB.prepare(`
ALTER TABLE users
ADD coins INTEGER DEFAULT 0
`);
stmtUpdateUsers.run()
} catch (e) {
console.log(e)
}
}
else if (message.content === 'flopo:users') {
const allAkhys = getAllUsers.all()
console.log(allAkhys)
}
else if (message.content === 'flopo:cancel') {
await message.delete()
}
else if (message.content.startsWith('flopo:reset-user-coins')) {
const userId = message.content.replace('flopo:reset-user-coins ', '')
const authorDB = getUser.get(userId)
if (authorDB) {
updateUserCoins.run({
id: userId,
coins: 0,
})
console.log(`${authorDB.username}'s coins were reset to 0`)
} else {
console.log('invalid user')
}
}
}
}
});

View File

@@ -11,7 +11,8 @@ export const stmtUsers = flopoDB.prepare(`
warned BOOLEAN DEFAULT 0,
warns INTEGER DEFAULT 0,
allTimeWarns INTEGER DEFAULT 0,
totalRequests INTEGER DEFAULT 0
totalRequests INTEGER DEFAULT 0,
coins INTEGER DEFAULT 0
)
`);
stmtUsers.run();
@@ -36,6 +37,7 @@ stmtSkins.run()
export const insertUser = flopoDB.prepare('INSERT INTO users (id, username, globalName, warned, warns, allTimeWarns, totalRequests) VALUES (@id, @username, @globalName, @warned, @warns, @allTimeWarns, @totalRequests)');
export const updateUser = flopoDB.prepare('UPDATE users SET warned = @warned, warns = @warns, allTimeWarns = @allTimeWarns, totalRequests = @totalRequests WHERE id = @id');
export const updateUserCoins = flopoDB.prepare('UPDATE users SET coins = @coins WHERE id = @id');
export const getUser = flopoDB.prepare('SELECT * FROM users WHERE id = ?');
export const getAllUsers = flopoDB.prepare('SELECT * FROM users');