private static async void EnsureConfigExists() { if (!Directory.Exists(Path.Combine(AppContext.BaseDirectory, "data"))) { Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "data")); } var loc = Path.Combine(AppContext.BaseDirectory, "data/configuration.json"); if (!File.Exists(loc) || Configuration.Load().Token == null) { await NeoConsole.NewLine("Configuration not found...\nCreating...", ConsoleColor.Cyan); var config = new Configuration(); await NeoConsole.NewLine("Token :", ConsoleColor.DarkCyan); config.Token = Console.ReadLine(); await NeoConsole.NewLine("Default Prefix :", ConsoleColor.Cyan); config.Prefix = Console.ReadLine(); config.Save(); } else { await NeoConsole.NewLine("Configuration found...\nLoading...", ConsoleColor.Cyan); } }
private async Task Run() { Console.OutputEncoding = Encoding.Unicode; await PrintInfoAsync(); using (var db = new NeoContext()) { db.Database.EnsureCreated(); } EnsureConfigExists(); _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Info, MessageCacheSize = 5000 }); await _client.LoginAsync(TokenType.Bot, Configuration.Load().Token); await NeoConsole.Append("Logging in...", ConsoleColor.Cyan); await _client.StartAsync(); await NeoConsole.NewLine("Success...", ConsoleColor.Cyan); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton <CommandHandler>() .AddSingleton <InteractiveService>() .BuildServiceProvider(); await NeoConsole.NewLine("Activating Services...\nDone.", ConsoleColor.Cyan); await _services.GetRequiredService <CommandHandler>().Install(_client, _services); await NeoConsole.NewLine("Command Handler starting...\nDone.\n", ConsoleColor.Cyan); _client.Log += Log; _client.Ready += _client_Ready; await Task.Delay(-1); }
private async Task HandleCommand(SocketMessage s) { var msg = s as SocketUserMessage; if (msg == null) { return; // Check if the received message is from a user. } var context = new SocketCommandContext(_client, msg); // Create a new command context. if (context.User.IsBot) { return; } var prefix = Configuration.Load().Prefix; using (var db = new NeoContext()) { if (!db.Users.Any(x => x.Id == msg.Author.Id)) //if user does net exist in database create it { var newobj = new DbUser { Id = msg.Author.Id, Cash = 500 }; db.Users.Add(newobj); db.SaveChanges(); } if (!(msg.Channel is ISocketPrivateChannel) && db.Guilds.Any(x => x.Id == (msg.Channel as IGuildChannel).GuildId)) { prefix = db.Guilds.FirstOrDefault(x => x.Id == (msg.Channel as IGuildChannel).GuildId).Prefix ?? Configuration.Load().Prefix; } if (db.Blacklist.Any(bl => bl.User == db.Users.FirstOrDefault(u => u.Id == context.User.Id))) { return; } } await CheckAfk(msg); await HandleTag(msg); await NeoConsole.Log(msg); var argPos = 0; // Check if the message has either a string or mention prefix. if (msg.HasStringPrefix(prefix, ref argPos) || msg.HasStringPrefix(Configuration.Load().Prefix, ref argPos) || msg.HasMentionPrefix(_client.CurrentUser, ref argPos)) { IResult result; // Try and execute a command with the given context. try { result = await _cmds.ExecuteAsync(context, argPos, _services); if (!result.IsSuccess && result.Error != CommandError.UnknownCommand) { var embed = NeoEmbeds.Error(result.ErrorReason, context.User, result.Error != null ? result.Error.ToString() : "Error").Build(); await context.Channel.SendMessageAsync("", false, embed); } } catch (Exception ex) { var embed = NeoEmbeds.Minimal(ex.Message).AddField("inner", ex.InnerException?.Message ?? "nope"); await NeoConsole.NewLine(ex.StackTrace); await context.Channel.SendMessageAsync("", false, embed.Build()); } } }