mirror of
https://github.com/SamueleLorefice/isleBot.git
synced 2026-01-15 06:03:42 +00:00
Converted the project in C#, added Entity Framework Core.
This commit is contained in:
102
Program.cs
Normal file
102
Program.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using Discord;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IsleBot;
|
||||
|
||||
public class IsleBot {
|
||||
private Config config;
|
||||
private DiscordSocketClient client;
|
||||
Random rng = new Random();
|
||||
|
||||
public static Task Main(string[] args) {
|
||||
var config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("Config.json"));
|
||||
var islebot = new IsleBot(config);
|
||||
return islebot.MainAsync();
|
||||
}
|
||||
|
||||
public async Task MainAsync() {
|
||||
client = new DiscordSocketClient(
|
||||
new DiscordSocketConfig {
|
||||
LogLevel = LogSeverity.Info,
|
||||
MessageCacheSize = 500,
|
||||
GatewayIntents = GatewayIntents.AllUnprivileged
|
||||
}
|
||||
);
|
||||
|
||||
client.Log += Log;
|
||||
client.Connected += () => {
|
||||
Console.WriteLine("Connection Estabilished");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
client.Disconnected += (e) => {
|
||||
Console.WriteLine("Connection Lost");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
client.Ready += Client_OnReady;
|
||||
|
||||
client.SlashCommandExecuted += SlashCommandHandler;
|
||||
|
||||
await client.LoginAsync(TokenType.Bot, config.DiscordToken);
|
||||
await client.StartAsync();
|
||||
// Block this task until the program is closed.
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
||||
private async Task Client_OnReady() {
|
||||
client.Guilds.ToList().ForEach(guild =>
|
||||
{
|
||||
var guildGetCard = new SlashCommandBuilder()
|
||||
.WithName("get-card")
|
||||
.WithDescription("Get/Replace your current card");
|
||||
|
||||
var attackCommand = new SlashCommandBuilder()
|
||||
.WithName("attack")
|
||||
.WithDescription("Attack a player")
|
||||
.AddOption("target", ApplicationCommandOptionType.User, "The target of the attack");
|
||||
|
||||
try {
|
||||
guild.CreateApplicationCommandAsync(guildGetCard.Build());
|
||||
guild.CreateApplicationCommandAsync(attackCommand.Build());
|
||||
} catch (HttpException exception) {
|
||||
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
||||
Console.WriteLine(json);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task SlashCommandHandler(SocketSlashCommand command) {
|
||||
switch (command.Data.Name) {
|
||||
case "get-card":
|
||||
var card = GenerateCard();
|
||||
await command.RespondAsync("You got a new card!\n " + card);
|
||||
break;
|
||||
case "attack":
|
||||
var target = command.Data.Options.First().Value as SocketUser;
|
||||
await command.RespondAsync($"You attacked {target.Username}!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Card GenerateCard() {
|
||||
var card = new Card($"Test Card N. {rng.Next(0, 1000)}",
|
||||
rng.Next(1, 20), rng.Next(0, 10),
|
||||
rng.Next(25, 100));
|
||||
return card;
|
||||
}
|
||||
|
||||
private void Attack(ref Card attacker, ref Card defender) {
|
||||
defender.CurrentHealth -= Math.Clamp(attacker.Attack - defender.Defense, 0, int.MaxValue);
|
||||
}
|
||||
|
||||
private Task Log(LogMessage msg) {
|
||||
Console.WriteLine(msg.ToString());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public IsleBot(Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user