markov improvements
This commit is contained in:
parent
561c039135
commit
4d181c8ad8
|
@ -3,6 +3,7 @@ import { GuildMember, Message, User, SlashCommandBuilder, Interaction } from 'di
|
||||||
const DEFAULT_EMOJI = '🪙';
|
const DEFAULT_EMOJI = '🪙';
|
||||||
const STOP_EMOJI = '⏹️';
|
const STOP_EMOJI = '⏹️';
|
||||||
const DONE_EMOJI = '👍';
|
const DONE_EMOJI = '👍';
|
||||||
|
const BAD_EMOJI = '👎';
|
||||||
|
|
||||||
const END_TEMPLATES = [
|
const END_TEMPLATES = [
|
||||||
'Alright! Here\'s the messages you all conjured:',
|
'Alright! Here\'s the messages you all conjured:',
|
||||||
|
@ -34,23 +35,38 @@ function randomWord() {
|
||||||
return RANDOM_WORDS[Math.floor(Math.random() * RANDOM_WORDS.length)];
|
return RANDOM_WORDS[Math.floor(Math.random() * RANDOM_WORDS.length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function* chunks(arr: unknown[], n: number) {
|
||||||
|
for (let i = 0; i < arr.length; i += n) {
|
||||||
|
yield arr.slice(i, i + n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('markov')
|
.setName('markov')
|
||||||
.setDescription('Play a Markov chain game')
|
.setDescription('Play a Markov chain game')
|
||||||
.addIntegerOption((option) =>
|
.addIntegerOption(option =>
|
||||||
option
|
option
|
||||||
.setName('context')
|
.setName('context')
|
||||||
.setRequired(false)
|
.setRequired(false)
|
||||||
.setDescription('Amount of words to show as context')
|
.setDescription('Amount of words to show as context')
|
||||||
.setMinValue(1)
|
.setMinValue(1)
|
||||||
),
|
.setMaxValue(100)
|
||||||
|
)
|
||||||
|
.addIntegerOption(option =>
|
||||||
|
option
|
||||||
|
.setName('iterations')
|
||||||
|
.setRequired(false)
|
||||||
|
.setDescription('Amount of rounds to do')
|
||||||
|
.setMinValue(1)
|
||||||
|
.setMaxValue(100)),
|
||||||
|
|
||||||
execute: async (interaction: Interaction, member: GuildMember) => {
|
execute: async (interaction: Interaction, member: GuildMember) => {
|
||||||
if (!interaction.isChatInputCommand()) return;
|
if (!interaction.isChatInputCommand()) return;
|
||||||
|
|
||||||
let participants: User[] = [member.user];
|
let participants: User[] = [member.user];
|
||||||
const context = interaction.options.getInteger('context') || 3;
|
const context = interaction.options.getInteger('context') || 3;
|
||||||
|
const maxIterations = interaction.options.getInteger('iterations') || 10;
|
||||||
|
|
||||||
const duration = 25_000;
|
const duration = 25_000;
|
||||||
|
|
||||||
|
@ -78,6 +94,10 @@ module.exports = {
|
||||||
dispose: true
|
dispose: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const updateInterval = setInterval(() => {
|
||||||
|
m.edit(formatMessage(participants, duration - (Date.now() - started)));
|
||||||
|
}, 3_000);
|
||||||
|
|
||||||
collector.on('collect', (reaction, user) => {
|
collector.on('collect', (reaction, user) => {
|
||||||
if (reaction.emoji.name === STOP_EMOJI) {
|
if (reaction.emoji.name === STOP_EMOJI) {
|
||||||
collector.stop();
|
collector.stop();
|
||||||
|
@ -93,34 +113,33 @@ module.exports = {
|
||||||
});
|
});
|
||||||
|
|
||||||
collector.on('end', async () => {
|
collector.on('end', async () => {
|
||||||
|
clearInterval(updateInterval);
|
||||||
m.edit(formatMessage(participants, 0));
|
m.edit(formatMessage(participants, 0));
|
||||||
|
|
||||||
let sentences: string[][] = Array(participants.length).fill(0);
|
let sentences: string[][] = Array(participants.length).fill(0);
|
||||||
sentences = sentences.map(() => []);
|
sentences = sentences.map(() => []);
|
||||||
let iterations = 0;
|
let iterations = 0;
|
||||||
const maxIterations = 10;
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition
|
// eslint-disable-next-line no-constant-condition
|
||||||
while (true) {
|
while (true) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
participants.map(async (p, i) => {
|
participants.map(async (p, i) => {
|
||||||
const sentence = sentences[(i + iterations) % sentences.length];
|
const sentence = sentences[(i + iterations) % sentences.length];
|
||||||
const msg = await p.send(`Continue the following sentence: [${iterations}/${maxIterations}]\n\n> _${context < sentence.length ? '…' : ''}${sentence.length > 0 ? sentence.slice(-context).join(' ') : `start a sentence... (try working with: “${randomWord()}”)`}_` + (iterations === 0 ? '\n\n**Send a message to continue**' : ''));
|
const msg = await p.send((`Continue the following sentence: [${iterations}/${maxIterations}]\n\n> _${context < sentence.length ? '…' : ''}${sentence.length > 0 ? sentence.slice(-context).join(' ') : `start a sentence... (try working with: “${randomWord()}”)`}_` + (iterations === 0 ? '\n\n**Send a message to continue**' : '')).slice(0, 2000));
|
||||||
try {
|
try {
|
||||||
const collected = await msg.channel.awaitMessages({
|
const collected = await msg.channel.awaitMessages({
|
||||||
max: 1,
|
max: 1,
|
||||||
time: 30_000,
|
time: 45_000,
|
||||||
errors: ['time']
|
errors: ['time'],
|
||||||
|
filter: (msg) => {
|
||||||
|
const valid = msg.content !== '' && msg.content.length <= 2000;
|
||||||
|
if (!valid) msg.react(BAD_EMOJI);
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const message = collected.first() as Message;
|
const message = collected.first() as Message;
|
||||||
|
sentence.push(...message.content.split(' '));
|
||||||
if (sentence.join(' ').length + message.content.length >= 2000) {
|
await message.react(DONE_EMOJI);
|
||||||
message.reply('stop that');
|
|
||||||
sentence.push(...randomWord().split(' '));
|
|
||||||
} else {
|
|
||||||
sentence.push(...message.content.split(' '));
|
|
||||||
await message.react(DONE_EMOJI);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await p.send('Took too long... Surprise... Added...... :)');
|
await p.send('Took too long... Surprise... Added...... :)');
|
||||||
sentence.push(...randomWord().split(' '));
|
sentence.push(...randomWord().split(' '));
|
||||||
|
@ -136,11 +155,41 @@ module.exports = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const endTemplate = END_TEMPLATES[Math.floor(Math.random() * END_TEMPLATES.length)];
|
const endTemplate = END_TEMPLATES[Math.floor(Math.random() * END_TEMPLATES.length)];
|
||||||
await m.reply({
|
|
||||||
content: endTemplate + '\n\n' + sentences.map(sentence => '- “' + sentence.join(' ') + '”').join('\n') + '\n\nThank you for participating :)',
|
const segments = [
|
||||||
allowedMentions: {
|
endTemplate + '\n\n',
|
||||||
parse: ['users']
|
...sentences.map(sentence => '- ' + sentence.join(' ')),
|
||||||
|
'\n\nThank you for participating :)'
|
||||||
|
];
|
||||||
|
|
||||||
|
const content = [];
|
||||||
|
let contentBuffer = '';
|
||||||
|
|
||||||
|
while (segments.length > 0) {
|
||||||
|
const segment = segments.splice(0, 1)[0];
|
||||||
|
const newMsg = contentBuffer + '\n' + segment;
|
||||||
|
if (newMsg.length > 2000) {
|
||||||
|
content.push(contentBuffer);
|
||||||
|
contentBuffer = '';
|
||||||
|
|
||||||
|
if (segment.length > 2000) {
|
||||||
|
content.push(...([...(chunks(segment.split(''), 2000))].map(s => s.join(''))));
|
||||||
|
} else {
|
||||||
|
contentBuffer = segment;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
contentBuffer = newMsg;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (contentBuffer !== '') content.push(contentBuffer);
|
||||||
|
|
||||||
|
content.forEach(async content => {
|
||||||
|
await m.channel.send({
|
||||||
|
content: content,
|
||||||
|
allowedMentions: {
|
||||||
|
parse: ['users']
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue