Beispiel #1
0
        public async Task AdminInfo(string botName, [Remainder] string selectedInfo)
        {
            botName      = botName.ToLower();
            selectedInfo = selectedInfo.ToLower();
            if (botName != Assembly.GetEntryAssembly().GetName().Name.ToLower())
            {
                return;
            }

            string prefix = $"**{Configurations.CommandPrefix}admininfo {Assembly.GetEntryAssembly().GetName().Name.ToLower()} ";
            string suffix = $"**";

            try
            {
                string output = _info.GetInfo(selectedInfo).Content;

                await Context.Channel.SendMessageAsync(
                    _info.GetInfo(selectedInfo).Content ?? "Nothing to display.");

                PrettyPrint.WriteLine($"Listing Info: {selectedInfo}");
            }
            catch (KeyNotFoundException e)
            {
                PrettyPrint.WriteLine(e.Message);
                await Context.Channel.SendMessageAsync("Invalid selection.");
            }
        }
Beispiel #2
0
        public async void Display(IUser user)
        {
            if (!_initialized)
            {
                Init();
                _initialized = true;
            }

            OnDisplay(user);
            if (!_displayEnabled)
            {
                return;
            }

            string response = "";

            if (oneTimeMessage != null)
            {
                if (_prependQueuedMessage)
                {
                    response      += $"{oneTimeMessage}\n\n";
                    oneTimeMessage = null;
                }
            }

            response += GetHeader();
            response += GetTitle();
            response += GetMenuList();
            response += GetFooter();

            if (oneTimeMessage != null)
            {
                if (!_prependQueuedMessage)
                {
                    response      += $"\n\n{oneTimeMessage}";
                    oneTimeMessage = null;
                }
            }

            try
            {
                if (_stream != null)
                {
                    await user.GetOrCreateDMChannelAsync().Result.SendFileAsync(_stream, "image.png", response);

                    _stream.Dispose();
                    _stream = null;
                }
                else
                {
                    Embed embed = _embed != null?_embed.Build() : null;

                    await user.SendMessageAsync(response, false, embed);
                }
            }
            catch (Exception e) //ArgumentException
            {
                PrettyPrint.WriteLine(e.ToString());
            }
        }
Beispiel #3
0
        public async Task MessageReceivedAsync(SocketMessage rawMessage)
        {
            // Ignore system messages, or messages from other bots
            if (!(rawMessage is SocketUserMessage message))
            {
                return;
            }
            if (message.Source != MessageSource.User)
            {
                return;
            }

            // This value holds the offset where the prefix ends
            var argPos = 0;

            if (!message.HasCharPrefix(Configurations.CommandPrefix, ref argPos))
            {
                return;
            }

            var context = new CommandContext(_discord, message);
            var result  = await _commands.ExecuteAsync(context, argPos, _services);

            if (result.Error.HasValue &&
                result.Error.Value != CommandError.UnknownCommand) // it's bad practice to send 'unknown command' errors
            {
                if (result.Error.Value != CommandError.UnmetPrecondition)
                {
                    await context.Channel.SendMessageAsync(result.ErrorReason);
                }

                PrettyPrint.WriteLine(result.ToString());
            }
        }
Beispiel #4
0
        public MenuService(
            DiscordSocketClient discord,
            IServiceProvider services)
        {
            _discord  = discord;
            _services = services;

            _users = new Dictionary <ulong, MenuUser>();

            _discord.MessageReceived += MessageReceivedAsync;

            PrettyPrint.WriteLine("Menu Service successfully loaded.");
        }
Beispiel #5
0
        public async Task AdminInfo(string botName)
        {
            botName = botName.ToLower();
            if (botName != Assembly.GetEntryAssembly().GetName().Name.ToLower())
            {
                return;
            }

            string prefix = $"**{Configurations.CommandPrefix}admininfo {Assembly.GetEntryAssembly().GetName().Name.ToLower()} ";
            string suffix = $"**";

            await Context.Channel.SendMessageAsync(
                _info.GetList(prefix, suffix));

            PrettyPrint.WriteLine($"Listing admin info.");
        }
Beispiel #6
0
        public async Task KillBot(string botName)
        {
            botName = botName.ToLower();
            if (botName != Assembly.GetEntryAssembly().GetName().Name.ToLower() &&
                botName != "all")
            {
                return;
            }

            await Context.Channel.SendMessageAsync("Terminating...");

            if (_discord != null)
            {
                await _discord.StopAsync();
            }

            PrettyPrint.WriteLine($"Killed the bot.{Environment.NewLine}");
            Process.GetCurrentProcess().Kill();
        }
Beispiel #7
0
        /// <summary>
        /// Executes action provided by the menu.
        /// </summary>
        /// <param name="user">User for which to apply the action to.</param>
        /// <param name="msg">Message to to pass to menu action.</param>
        /// <returns>True or false depending on whether the action successfully executed.</returns>
        public bool ExecuteMenuActionFor(MenuUser user, IMessage msg)
        {
            if (!(user.currentMenu is IMenuAction))
            {
                return(false);
            }
            var action = (IMenuAction)user.currentMenu;

            // Process input using menu's method
            // Queue message returned from action menu
            try
            {
                user.QueueMessage(action.Execute(msg));
            }
            catch (Exception e)
            {
                PrettyPrint.WriteLine("Unable to execute action:\n" + e.Message + '\n' + e.StackTrace, ConsoleColor.Red);
                return(false);
            }
            return(true);
        }
Beispiel #8
0
        public void EnterMenu(MenuBase menu, IUser user)
        {
            try
            {
                if (UserInMenu(user))
                {
                    _users.TryGetValue(user.Id, out var usermenu);
                    usermenu?.Dispose();
                }
            }
            catch (Exception e)
            {
                PrettyPrint.WriteLine(e.Message);
            }

            // Initialize Menu and Display to User
            var menuUser = new MenuUser(user, menu, _users);

            _users.Add(user.Id, menuUser);

            menuUser.DisplayCurrentMenu();
        }
Beispiel #9
0
        public async Task RestartBot(string botName)
        {
            botName = botName.ToLower();
            if (botName != Assembly.GetEntryAssembly().GetName().Name.ToLower() &&
                botName != "all")
            {
                return;
            }

            await Context.Channel.SendMessageAsync("Restarting...");

            if (_discord != null)
            {
                await _discord.StopAsync();
            }

            Process.Start(new ProcessStartInfo
            {
                FileName  = "dotnet",
                Arguments = $@"""{Assembly.GetEntryAssembly().Location}"""
            });
            PrettyPrint.WriteLine($"Restarted the bot.{Environment.NewLine}");
            Process.GetCurrentProcess().Kill();
        }