/// <summary> /// Starts the bot asynchronously. /// </summary> public async Task Start() { Console.WriteLine("Fetching meta repositories..."); if (Directory.Exists("git")) { FileHelper.NormalizeAttributes("git"); Directory.Delete("git", true); } Meta = new MetaHandler(Settings.Meta); Commands = new Dictionary <string, Func <BotCommand, Task> >(); Directory.CreateDirectory(PluginsFolder); Console.WriteLine("Loading plugins."); Plugins = new PluginLoader <IBotPlugin>(); foreach (IBotPlugin plugin in Plugins.Load(PluginsFolder)) { plugin.Load(this); } Console.WriteLine("Reloading meta."); Meta.Reload(); if (Settings.Chat.Discord.Enabled) { DiscordConnection = new DiscordConnection(); DiscordConnection.Configure(this, Settings.Chat.Discord); Connections.Add(DiscordConnection); Console.WriteLine("Beginning asynchronous Discord connection."); _ = DiscordConnection.ConnectAsync(); } Console.WriteLine("Setting up command system."); RegisterCommands(typeof(Bot).Assembly); Console.WriteLine("Successful startup."); await Task.Delay(-1); }
public async Task StartAsync() { if (IsRunning) { return; } IsRunning = true; Source = new CancellationTokenSource(); try { await DiscordConnection.RunAsync(Source.Token, DiscordSocketConfig); } catch (OperationCanceledException ocex) { if (!Source.IsCancellationRequested || ocex.CancellationToken != Source.Token) { ExceptionCatched?.Invoke(this, ocex); } } catch (Exception ex) { ExceptionCatched?.Invoke(this, ex); } finally { IsRunning = false; } }
static public void Register(DiscordConnection connection) { var builder = new ContainerBuilder(); var loggerFactory = LoggerFactory.Create((config) => { // Configure logger config.AddConsole(); }); builder.Register(c => loggerFactory).As <ILoggerFactory>(); // DiscordConnection injection builder.RegisterInstance(connection) .OnActivated(h => { connection.handlerService = h.Context.Resolve <DispatchHandlerService>(); connection.Logger = h.Context.Resolve <ILoggerFactory>().CreateLogger <DiscordConnection>(); connection.userService = h.Context.Resolve <UserService>(); }); // DispatchHandlerService resolve builder.Register(c => { var logger = c.Resolve <ILoggerFactory>().CreateLogger <DispatchHandlerService>(); var messageCreateHandlerService = c.Resolve <MessageCreateHandlerService>(); var userService = c.Resolve <UserService>(); return(new DispatchHandlerService(logger, messageCreateHandlerService, userService)); }).InstancePerLifetimeScope(); // MessageCreateHandlerService resolve builder.Register(c => { var logger = c.Resolve <ILoggerFactory>().CreateLogger <MessageCreateHandlerService>(); var userService = c.Resolve <UserService>(); return(new MessageCreateHandlerService(logger, userService)); }).InstancePerLifetimeScope(); // UserService resolve builder.Register(c => { var logger = c.Resolve <ILoggerFactory>().CreateLogger <UserService>(); return(new UserService(logger)); }).InstancePerLifetimeScope(); // Build container var container = builder.Build(); using (var scope = container.BeginLifetimeScope()) { scope.Resolve <DiscordConnection>(); } }
static void Main(string[] args) { Task.Run(async() => { var client = new DiscordConnection(); await client.ConnectAsync("..."); }); while (true) { if (Console.ReadKey().Key == ConsoleKey.Escape) { break; } } }
public override void MapTo(User model, IServiceProvider services) { base.MapTo(model, services); model.CreatedTime = CreatedTime; model.UpdatedTime = UpdatedTime; model.Username = Username; model.Email = Email; model.Restrictions = Restrictions?.ToArray(r => r.Convert(services)) ?? Array.Empty <UserRestriction>(); model.Permissions = Permissions; model.Language = Language; model.DiscordConnection = DiscordConnection?.Convert(services); model.AllowSharedCollections = AllowSharedCollections; model.SpecialCollections = SpecialCollections; if (model.Restrictions != null) { Array.Sort(model.Restrictions, (a, b) => a.StartTime.CompareTo(b.StartTime)); } }
public async Task Run() { var savage_config = new DiscordConfig { AutoReconnect = true, DiscordBranch = Branch.Stable, LargeThreshold = 250, LogLevel = LogLevel.Info, Token = "", TokenType = TokenType.Bot, UseInternalLogHandler = false }; this.Client = new DiscordClient(savage_config); var CommandConfig = new CommandsNextConfiguration { StringPrefix = "#", EnableMentionPrefix = true, EnableDms = false }; this.Commands = this.Client.UseCommandsNext(CommandConfig); this.Commands.RegisterCommands <Commands_Base>(); this.Commands.RegisterCommands <Commands_Admin>(); var discord_conn = new DiscordConnection(); this.Client.SetWebSocketClient <WebSocket4NetClient>(); await this.Client.ConnectAsync(); await Task.Delay(-1); }
public static string GetIconUrl(this DiscordConnection discordConnection) { switch (discordConnection.Type) { case "steam": return("https://discordapp.com/assets/f09c1c70a67ceaaeb455d163f3f9cbb8.png"); case "twitch": return("https://discordapp.com/assets/edbbf6107b2cd4334d582b26e1ac786d.png"); case "twitter": return("https://discordapp.com/assets/4662875160dc4c56954003ebda995414.png"); case "reddit": return("https://discordapp.com/assets/3abe9ce5a00cc24bd8aae04bf5968f4c.png"); case "facebook": return("https://discordapp.com//assets/8d8f815f3d81a33b1e70ec7c22e1b6fe.png"); case "spotify": return("https://discordapp.com/assets/f0655521c19c08c4ea4e508044ec7d8c.png"); case "leagueoflegends": return("https://discordapp.com/assets/806953fe1cc616477175cbcdf90d5cd3.png"); case "youtube": return("https://discordapp.com/assets/ff3516ac66b71ef616b1df63e20fee65.png"); case "skype": return("https://discordapp.com/assets/5be6cc17e596c02e7506f2776cfb1622.png"); case "xbox": return("https://discordapp.com/assets/0d44ba28e39303de3832db580a252456.png"); default: return(""); } }
public async Task Run() { Log("Server", "Critical", "Savage bot is starting."); #region Misc Variables Log("Server", "Critical", "Initializing misc. variables."); Console.BackgroundColor = ConsoleColor.Blue; Console.Clear(); Console.ForegroundColor = ConsoleColor.Cyan; Console.Title = "Savage Bot"; Log("Server", "Critical", "Variables initialized."); #endregion Misc Variables #region Client Config setup Log("Server", "Critical", "Setting up client config."); DiscordConfig savage_config = new DiscordConfig { AutoReconnect = true, DiscordBranch = Branch.Stable, LargeThreshold = 250, LogLevel = LogLevel.Debug, Token = "", TokenType = TokenType.Bot, UseInternalLogHandler = false }; Log("Server", "Critical", "Loading configs."); this.Client = new DiscordClient(savage_config); Log("Server", "Critical", "Configs loaded."); #endregion Client Config setup #region Command Configs and Prefix Log("Server", "Critical", "Setting up Commands config and prefix."); var CommandConfig = new CommandsNextConfiguration { StringPrefix = "#", EnableMentionPrefix = true, EnableDms = false }; this.Commands = this.Client.UseCommandsNext(CommandConfig); Log("Server", "Critical", "Commands config loaded."); #endregion Command Configs and Prefix #region Command Loader Log("Server", "Critical", "Loading command classes."); this.Commands.RegisterCommands <Commands_Base>(); Log("Server", "Critical", "Command classes loaded."); #endregion Command Loader #region Bot Connection and Task Looping Log("Server", "Critical", "Starting bot connection."); var discord_conn = new DiscordConnection(); if (Is_Win_7 == true) { this.Client.SetWebSocketClient <WebSocket4NetClient>(); } await this.Client.ConnectAsync(); Log("Server", "Critical", "Bot connected."); Log("Server", "Info", "Enjoy your stay."); await Task.Delay(-1); #endregion Bot Connection and Task Looping }
public async Task StartAsync() { _tokenSource = new CancellationTokenSource(); await DiscordConnection.RunAsync(_tokenSource.Token); }
public void SetupNetwork() { Network = new DiscordConnection(Secrets.RetrieveBotKey(), "Golem"); }