Skip to content

Commit

Permalink
Adding Give command
Browse files Browse the repository at this point in the history
  • Loading branch information
exatombe committed Jan 2, 2024
1 parent 20e59b6 commit 62df2c9
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/bot/commands/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Logout from "./ai/logout";
import Interogate from "./ai/interogate";
import { ModelGenerationInputStableSamplers } from "../../internal_libs/aihorde";
import Advanced from "./ai/advanced";
import Give from "./ai/give";


const commandData = new SlashCommandBuilder()
Expand Down Expand Up @@ -141,7 +142,27 @@ const commandData = new SlashCommandBuilder()
"en-GB": "The image to interrogate",
"en-US": "The image to interrogate"
}))
).addSubcommandGroup(subcommandGroup => subcommandGroup
)
.addSubcommand(subcommand => subcommand
.setName("give")
.setDescription("Donner des kudos à un utilisateur")
.setDescriptionLocalizations({
fr: "Donner des kudos à un utilisateur",
"en-GB": "Give kudos to an user",
"en-US": "Give kudos to an user"
})
.addUserOption(option => option.setName("user").setDescription("L'utilisateur").setRequired(true).setDescriptionLocalizations({
fr: "L'utilisateur",
"en-GB": "The user",
"en-US": "The user"
}))
.addNumberOption(option => option.setName("amount").setDescription("Le nombre de kudos à donner").setRequired(false).setDescriptionLocalizations({
fr: "Le nombre de kudos à donner",
"en-GB": "The number of kudos to give",
"en-US": "The number of kudos to give"
}))
)
.addSubcommandGroup(subcommandGroup => subcommandGroup
.setName("advanced")
.setDescription("Commandes avancées de génération").addSubcommand(subcommand => subcommand
.setName("imagine")
Expand Down Expand Up @@ -286,6 +307,8 @@ export class IaCommand extends CommandsBase {
case "ask":
await Ask(this, interaction);
break;
case "give":
await Give(this, interaction);
case "interogate":
await Interogate(this, interaction);
break;
Expand Down
79 changes: 79 additions & 0 deletions src/bot/commands/ai/give.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ActionRowBuilder, CommandInteraction, CommandInteractionOptionResolver, EmbedBuilder, MessageFlags, ModalBuilder, TextInputBuilder, TextInputStyle } from "discord.js";
import CommandsBase from "../baseCommands";

export default async function Give(command: CommandsBase, interaction: CommandInteraction) {
let i = await interaction.deferReply()
let options = interaction.options;
let userSelected = options.getUser("user", true);
let amount = 1;
if(options instanceof CommandInteractionOptionResolver) {
amount = options.getNumber("amount") || 1;
}
i.edit({
content: "Recherche en cours de l'utilisateur dans la base de donnée",
});
let giverUser = await command.client.database.users.findFirst({
where: {
id: interaction.user.id
}
});
if (!giverUser) return i.edit({
content: "Vous n'êtes pas enregistré" + `, vous devez vous enregistrer avec la commande </${interaction.commandName} login:${interaction.commandId}>`,
});
i.edit({
content: "Recherche en cours de l'utilisateur dans l'API",
});
let token = command.client.decryptString(giverUser.horde_token);
let ai = command.client.aiHorde;
try {
let user = await ai.findUser({
token
});
if(user.kudos < amount) return i.edit({
content: "Vous n'avez pas assez de kudos",
});
// find the other user
let receiverUser = await command.client.database.users.findFirst({
where: {
id: userSelected.id
}
});
if(!receiverUser) return i.edit({
content: "L'utilisateur n'est pas enregistré",
});
try{
let receiverToken = command.client.decryptString(receiverUser.horde_token);
let receiver = await ai.findUser({
token: receiverToken
});
// give kudos

ai.postKudosTransfer({
username: receiver.username,
amount
},{
token: token
}).then(async (content) => {
i.edit({
content:`Vous avez donné ${content.transferred} kudos à <@${userSelected.id}> (${receiver.username})`
});
}).catch(async (err) => {
i.edit({
content: "Une erreur est survenue lors du transfert de kudos, réessayer",
});
});
}catch(e){
return i.edit({
content: "L'utilisateur cible n'est pas enregistré",
});
}
} catch (e) {
return i.edit({
content: "Le token est invalide vous devez vous reconnecter",
});
}
}

function Capitalize(str: string) {
return `**${str.charAt(0).toUpperCase() + str.slice(1)}**`
}

0 comments on commit 62df2c9

Please sign in to comment.