mirror of
https://github.com/SamueleLorefice/isleBot.git
synced 2026-01-15 06:03:42 +00:00
Implemented database basic functions, added ToFormattedString to Card class, improved commands sending to the guild manager on discord, added toString to user class
This commit is contained in:
73
Program.cs
73
Program.cs
@@ -8,7 +8,8 @@ namespace IsleBot;
|
||||
public class IsleBot {
|
||||
private Config config;
|
||||
private DiscordSocketClient client;
|
||||
Random rng = new Random();
|
||||
Random rng = new();
|
||||
private DbManager db = new();
|
||||
|
||||
public static Task Main(string[] args) {
|
||||
var config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("Config.json"));
|
||||
@@ -26,10 +27,12 @@ public class IsleBot {
|
||||
);
|
||||
|
||||
client.Log += Log;
|
||||
|
||||
client.Connected += () => {
|
||||
Console.WriteLine("Connection Estabilished");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
client.Disconnected += (e) => {
|
||||
Console.WriteLine("Connection Lost");
|
||||
return Task.CompletedTask;
|
||||
@@ -48,18 +51,43 @@ public class IsleBot {
|
||||
private async Task Client_OnReady() {
|
||||
client.Guilds.ToList().ForEach(guild =>
|
||||
{
|
||||
var guildGetCard = new SlashCommandBuilder()
|
||||
.WithName("get-card")
|
||||
.WithDescription("Get/Replace your current card");
|
||||
// Guild Commands
|
||||
List<SlashCommandProperties> guildCommands = new();
|
||||
|
||||
var attackCommand = new SlashCommandBuilder()
|
||||
guildCommands.Add(new SlashCommandBuilder()
|
||||
.WithName("register-player")
|
||||
.WithDescription("Register yourself as a player")
|
||||
.Build());
|
||||
|
||||
guildCommands.Add(new SlashCommandBuilder()
|
||||
.WithName("profile")
|
||||
.WithDescription("See your profile")
|
||||
.Build());
|
||||
|
||||
guildCommands.Add(new SlashCommandBuilder()
|
||||
.WithName("get-card")
|
||||
.WithDescription("Get/Replace your current card")
|
||||
.Build());
|
||||
|
||||
guildCommands.Add(new SlashCommandBuilder()
|
||||
.WithName("attack")
|
||||
.WithDescription("Attack a player")
|
||||
.AddOption("target", ApplicationCommandOptionType.User, "The target of the attack");
|
||||
.AddOption("target", ApplicationCommandOptionType.User, "The target of the attack")
|
||||
.Build());
|
||||
|
||||
try {
|
||||
guild.CreateApplicationCommandAsync(guildGetCard.Build());
|
||||
guild.CreateApplicationCommandAsync(attackCommand.Build());
|
||||
var commands = guild.GetApplicationCommandsAsync().Result.ToList();
|
||||
/*
|
||||
guildCommands.ForEach(command => {
|
||||
//if the command already exists and is duplicated
|
||||
if(commands.FindAll(oldCommand => oldCommand.Name == (string)command.Name)?.Count > 1) {
|
||||
guild.DeleteApplicationCommandsAsync(); //delete all commands
|
||||
//else if the command doesn't already exists
|
||||
}else if(commands.Find(oldCommand => oldCommand.Name == (string)command.Name) == null) {
|
||||
guild.CreateApplicationCommandAsync(command); //create it
|
||||
}//otherise, do nothing.
|
||||
});*/
|
||||
guild.BulkOverwriteApplicationCommandAsync(guildCommands.ToArray());
|
||||
} catch (HttpException exception) {
|
||||
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
||||
Console.WriteLine(json);
|
||||
@@ -69,9 +97,32 @@ public class IsleBot {
|
||||
|
||||
private async Task SlashCommandHandler(SocketSlashCommand command) {
|
||||
switch (command.Data.Name) {
|
||||
case "register-player":
|
||||
if (db.GetPlayerById(command.User.Id) == null) {
|
||||
var user = new User(command.User.Id, command.User.GlobalName);
|
||||
db.AddPlayer(user);
|
||||
await command.RespondAsync("You are now registered. In future you will also be able to see your profile page.");
|
||||
} else {
|
||||
await command.RespondAsync("You are already registered. In future you will also be able to see your profile page.");
|
||||
}
|
||||
break;
|
||||
case "profile":
|
||||
if(CheckIfPlayerExists(command.User.Id) == false) {
|
||||
await command.RespondAsync("You are not registered. Use /register-player to register yourself.");
|
||||
} else {
|
||||
await command.RespondAsync(db.GetPlayerById(command.User.Id)!.ToString());
|
||||
}
|
||||
break;
|
||||
case "get-card":
|
||||
var card = GenerateCard();
|
||||
await command.RespondAsync("You got a new card!\n " + card);
|
||||
if (CheckIfPlayerExists(command.User.Id) == false) {
|
||||
await command.RespondAsync("You are not registered. Use /register-player to register yourself.");
|
||||
} else {
|
||||
var player = db.GetPlayerById(command.User.Id)!;
|
||||
player.Cards.Add(card);
|
||||
db.UpdatePlayer(player);
|
||||
await command.RespondAsync($"You got a new card: \n{card.ToFormattedString()}");
|
||||
}
|
||||
break;
|
||||
case "attack":
|
||||
var target = command.Data.Options.First().Value as SocketUser;
|
||||
@@ -80,6 +131,10 @@ public class IsleBot {
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckIfPlayerExists(ulong id) {
|
||||
return db.GetPlayerById(id) != null;
|
||||
}
|
||||
|
||||
private Card GenerateCard() {
|
||||
var card = new Card($"Test Card N. {rng.Next(0, 1000)}",
|
||||
rng.Next(1, 20), rng.Next(0, 10),
|
||||
|
||||
Reference in New Issue
Block a user