-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request new match #74
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb150ed
registered match command
2a9d1bb
moved stuff to other files
2eb94f3
fixed some issue
ae59198
more cleanup
d97e961
small changes
7e1e35c
lol
e7dd0fe
check if thest was finished
a1bcb3c
xtra line
7dd6e80
removed comments
da3729e
started changes
johan-t d882553
WIP breaking changes
greg6775 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { SlashCommandBuilder, Guild } from 'discord.js'; | ||
import { client, db } from '../../common'; | ||
import 'dotenv/config'; | ||
import questions from '../../questions'; | ||
import findMatchingUser from '../../functions/findMatchingUser'; | ||
import conversationStarter from '../../functions/conversationStarter'; | ||
|
||
export const sendQuestion = async (interaction: any) => { | ||
|
||
const userContext = await db.db('contrabot').collection("users").findOne({ userId: interaction.user.id }); | ||
|
||
let userResponses = userContext?.userVector || []; | ||
|
||
const guildId = process.env.GUILD_ID; | ||
if (!guildId) throw new Error('GUILD_ID not found'); | ||
|
||
const guild: Guild | undefined = client.guilds.cache.get(guildId); | ||
if (!guild) throw new Error('Guild not found'); | ||
|
||
const bestMatch = await findMatchingUser(interaction.user.id, userResponses, guild); | ||
if (bestMatch) { | ||
const interactionGuildMember = guild.members.cache.get(interaction.user.id); | ||
if (!interactionGuildMember) throw new Error('interactionGuildMember was nog found'); | ||
|
||
bestMatch.GuildMember = await guild.members.fetch(bestMatch.userId); | ||
if (!guild) throw new Error('bestMatch.GuildMember'); | ||
|
||
const matchesCategory = guild.channels.cache.find((category: any) => category.name === 'matches' && category.type === 4); | ||
|
||
const channelName = `match-${interaction.user.username}-${bestMatch.username}`; | ||
|
||
const textChannel = await guild.channels.create({ | ||
parent: matchesCategory?.id, | ||
name: channelName.toLowerCase(), | ||
type: 0, | ||
}); | ||
|
||
await textChannel.permissionOverwrites.edit(interactionGuildMember, { | ||
ViewChannel: true, | ||
SendMessages: true, | ||
}); | ||
await textChannel.permissionOverwrites.edit(bestMatch.GuildMember, { | ||
ViewChannel: true, | ||
SendMessages: true, | ||
}); | ||
|
||
const everyone = await guild.roles.everyone; | ||
|
||
await textChannel.permissionOverwrites.edit(everyone, { | ||
ViewChannel: false, | ||
}); | ||
|
||
await textChannel.send(`Hallo ${interactionGuildMember} 👋, hallo ${bestMatch.GuildMember} 👋, basierend auf unserem Algorithmus wurdet ihr als Gesprächspartner ausgewählt. Bitte vergesst nicht respektvoll zu bleiben. Viel Spaß bei eurem Match!`); | ||
await textChannel.send(`Bei beispielsweise diesen drei Fragen seid ihr nicht einer Meinung:`); | ||
conversationStarter(textChannel, interaction, bestMatch.userVector, userResponses); | ||
|
||
interaction.user.send(`Du wurdest erfolgreich mit **@${bestMatch.username}** gematcht. Schau auf den Discord-Server um mit dem Chatten zu beginnen! 😊`); | ||
} else { | ||
console.warn('No best match found'); | ||
interaction.user.send("Leider konnte zur Zeit kein geeigneter Gesprächspartner gefunden werden. Bitte versuchen Sie es später erneut."); | ||
} | ||
|
||
// Reset context for this user in the database | ||
await db.db('contrabot').collection("users").updateOne( | ||
{ userId: interaction.user.id }, | ||
{ | ||
$set: { | ||
currentQuestionIndex: 0, // Reset to first question | ||
completionTime: new Date().toISOString(), // Set completion time | ||
} | ||
} | ||
); | ||
} | ||
|
||
export const data = new SlashCommandBuilder().setName('match').setDescription('Requests new match without retaking the test.'); | ||
export const execute = async (interaction: any) => { | ||
|
||
const userContext = await db.db('contrabot').collection("users").findOne({ userId: interaction.user.id }); | ||
|
||
let userResponses = userContext?.userVector || []; | ||
|
||
// checks if the user has answered the test | ||
// if not, an error hint is displayed | ||
if (userResponses.length === questions.length) { | ||
await interaction.reply({ | ||
content: 'Neues Match wird ermittelt. Bitte schaue in deinen Direktnachrichten nach :)', | ||
ephemeral: true, | ||
}); | ||
sendQuestion(interaction); | ||
} else { | ||
await interaction.reply({ | ||
content: 'Bitte beantworte den Meinungstest vollständig, bevor du mit Anderen gematcht werden kannst! Bitte nutze dazu den Befehl `/test`.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we maybe automatically trigger the test command if the user has not finished the test? |
||
ephemeral: true, | ||
}); | ||
console.log('Invalid userVector: test was not completed!'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { EmbedBuilder } from 'discord.js'; | ||
import questions from '../questions'; | ||
|
||
async function conversationStarter(channelOfDestination: any, interaction: any, bestMatch: number[], user: number[]) { | ||
|
||
// get all contrasting and similar answers | ||
let addedToDisagree = false; // Track if any numbers were added to disagree | ||
const disagree: number[] = []; | ||
|
||
user.forEach((value, i) => { | ||
const total = value + bestMatch[i]; | ||
if (value !== 0 && total === 0) { | ||
disagree.push(i); | ||
addedToDisagree = true; | ||
} | ||
}); | ||
// Only add to disagree if the flag is still false | ||
if (!addedToDisagree || disagree.length < 6) { | ||
user.forEach((value, i) => { | ||
const total = value + bestMatch[i]; | ||
if (Math.abs(total) === 1) { | ||
disagree.push(i); | ||
} | ||
}); | ||
} | ||
|
||
const selectedIndexes = getRandomDisagreement(disagree, 6); | ||
sendDisagreedQuestions(channelOfDestination, selectedIndexes.slice(0, 3)); | ||
} | ||
|
||
function getRandomDisagreement(arr: number[], num: number) { | ||
return Array.from({ length: Math.min(num, arr.length) }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]); | ||
} | ||
|
||
function sendDisagreedQuestions(channelOfDestination: any, disagree: number[]) { | ||
disagree.forEach((value) => { | ||
channelOfDestination.send({ | ||
embeds: [ | ||
new EmbedBuilder() | ||
.setTitle(`Frage: ${value + 1}/38`) | ||
.setDescription(questions[value].question) | ||
.setColor('#fb2364') | ||
] | ||
}); | ||
}); | ||
|
||
// Make it so that the tags of the questions are printed properly | ||
const selectedTags = disagree | ||
.map(index => questions[index].tag) | ||
.filter(tag => tag) | ||
.slice(0, 3); | ||
|
||
const topicsMessage = `Als Gesprächsthemen können z.B. ${selectedTags.map(tag => `**${tag}**`).join(", ")} besprochen werden.`; | ||
channelOfDestination.send(topicsMessage); | ||
} | ||
|
||
export default conversationStarter; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we declaring the sendQuestion function and not just importing the old one?