public async Task Select(string shortName)
        {
            //The requested language was not found.
            if (Client.Languages.All(l => l.ShortName != shortName))
            {
                await Context.Channel.SendMessageAsync(null, false,
                                                       EmbedHelper.BuildError(Locale, string.Format(Locale.InvalidLanguage, shortName)));
            }
            else
            {
                //The loading GIF :D
                RestUserMessage?loading = null;
                if (!string.IsNullOrEmpty(Client.Config.LoadingGif))
                {
                    loading = await Context.Channel.SendFileAsync(Client.Config.LoadingGif);
                }

                var guild = Client.Guilds.First(g => g.Id == Context.Guild.Id);
                guild.Language = shortName;
                var path =
                    $"{Client.Config.RootDirectory}{Path.DirectorySeparatorChar}{Client.Config.GuildsDirectoryName}{(Client.Config.GuildsDirectoryName == "" ? "" : Path.DirectorySeparatorChar.ToString())}{guild.Id}.json";
                //Write the new guild to a file.
                guild.OverwriteTo(path);
                if (loading != null)
                {
                    await loading.DeleteAsync();
                }
                //Send an embed with the new language.
                var newLocale = ILocale.Find(shortName);
                await Context.Channel.SendMessageAsync(null, false,
                                                       EmbedHelper.BuildSuccess(newLocale, newLocale.LanguageChanged));
            }
        }
Beispiel #2
0
        public async Task Stop()
        {
            if (Client.Games.All(x => x.Creator.Id != Context.User.Id) ||
                !((IGuildUser)Context.User).GuildPermissions.Administrator)
            {
                await Context.Channel.SendMessageAsync(null, false,
                                                       EmbedHelper.BuildError(Locale, Locale.NotEnoughPermissions));

                return;
            }

            var game = Client.Games.FirstOrDefault(x => x.Creator.Id == Context.User.Id);

            game?.Stop();
        }
Beispiel #3
0
        /// <summary>
        ///     A method that is called each time the client (bot) receives a command.
        /// </summary>
        private async Task HandleCommandAsync(SocketMessage arg)
        {
            var message = arg as SocketUserMessage;
            var context = new SocketCommandContext(Client, message);

            if (message == null)
            {
                return;
            }
            if (message.Author.IsBot)
            {
                return;
            }
            var guild = GetGuild(context.Guild.Id);

            if (guild == null)
            {
                await JoinedGuild(context.Guild, _findGuildLanguage(context.Guild));

                await message.Channel.SendMessageAsync(null, false, EmbedHelper.BuildError(ILocale.Find("en"), "```unexpected error: please run this command again.```"));
            }
            var argPos = 0;

            if (message.HasStringPrefix(guild.Prefix, ref argPos))
            {
                var searchResult = _commands.Search(context, argPos);
                if (searchResult.IsSuccess)
                {
                    var result = await _commands.ExecuteAsync(context, argPos, _services);

                    if (!result.IsSuccess)
                    {
                        if (result is ExecuteResult execResult)
                        {
                            await context.Channel.SendMessageAsync(null, false,
                                                                   EmbedHelper.BuildError(ILocale.Find(guild.Language),
                                                                                          $"```{execResult.Exception.StackTrace}```"));
                        }
                    }
                }
            }
            else
            {
                var game = Games.FirstOrDefault(g => g.Channel.Id == context.Channel.Id);
                game?.HandleInput(message);
            }
        }
Beispiel #4
0
        public async Task Suggest(string language, string word)
        {
            //Send a loading GIF if the file for it exists.
            RestUserMessage?loading = null;

            if (!string.IsNullOrEmpty(Client.Config.LoadingGif))
            {
                loading = await Context.Channel.SendFileAsync(
                    Client.Config.LoadingGif);
            }
            //Find the language from the user in the language list.
            var lang = Client.Languages.FirstOrDefault(l => l.ShortName == language);

            if (lang == null)
            {
                //Failed if the language from user was not found.
                await Context.Channel.SendMessageAsync(null, false,
                                                       EmbedHelper.BuildError(Locale, string.Format(Locale.InvalidLanguage, language)));

                if (loading != null)
                {
                    await loading.DeleteAsync();
                }
                return;
            }

            //Create a suggestion.
            var suggested = await Context.Channel.SendMessageAsync(null, false,
                                                                   EmbedHelper.BuildSuccess(Locale,
                                                                                            string.Format(Locale.SuccessfullySuggested, Context.User.Mention, _suggestions.Count + 1)));

            _suggestions.Add(new Suggestion(Context.User.Id, suggested, language, word));
            if (_suggestions.Count == 1)
            {
                //If the list before was empty, start processing suggestions.
                ProcessSuggestions();
            }
            if (loading != null)
            {
                await loading.DeleteAsync();
            }
        }
Beispiel #5
0
        public async void HandleInput(SocketUserMessage originalMessage)
        {
            IUserMessage?errorMessage = null;

            switch (_isValidInput(originalMessage.Content, originalMessage.Author))
            {
            case InputCheckResult.Success:
                SetWord(originalMessage.Content, originalMessage.Author);
                var message =
                    await Channel.SendMessageAsync(null, false,
                                                   EmbedHelper.BuildSuccess(Locale,
                                                                            string.Format(Locale.Continuing, _getValidWord(originalMessage.Content).Last())));

                await Task.Delay(5000);

                await message.DeleteAsync();

                break;

            case InputCheckResult.IsBot:
                return;

            case InputCheckResult.IsComment:
                return;

            case InputCheckResult.IsQuote:
                return;

            case InputCheckResult.IsEmoji:
                return;

            case InputCheckResult.SameUser:
                errorMessage = await Channel.SendMessageAsync(null, false, EmbedHelper.BuildError(Locale, Locale.InvalidUser));

                break;

            case InputCheckResult.RepeatedWord:
                errorMessage = await Channel.SendMessageAsync(null, false, EmbedHelper.BuildError(Locale, Locale.AlreadyUsedWord));

                break;

            case InputCheckResult.TooManyWords:
                errorMessage = await Channel.SendMessageAsync(null, false,
                                                              EmbedHelper.BuildError(Locale, string.Format(Locale.TooManyWords, originalMessage.Content.Split(" ").Length)));

                break;

            case InputCheckResult.WrongLetter:
                errorMessage = await Channel.SendMessageAsync(null, false,
                                                              EmbedHelper.BuildError(Locale, string.Format(Locale.WrongWord, _lastLetter)));

                break;

            case InputCheckResult.Gibberish:
                errorMessage = await Channel.SendMessageAsync(null, false,
                                                              EmbedHelper.BuildError(Locale, Locale.NotAWord + "\n" +
                                                                                     string.Format(Locale.HowToSuggest, Guild.Prefix,
                                                                                                   Locale.SuggestCommand, Language.ShortName, originalMessage.Content)));

                break;

            case InputCheckResult.NotFound:
                errorMessage = await Channel.SendMessageAsync(null, false, EmbedHelper.BuildError(Locale,
                                                                                                  string.Format(Locale.WordNotFound, Guild.Prefix, Locale.SuggestCommand, Language.ShortName,
                                                                                                                originalMessage.Content)));

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (errorMessage == null)
            {
                return;
            }
            await Task.Delay(10000);

            try
            {
                await errorMessage.DeleteAsync();

                await originalMessage.DeleteAsync();
            }
            catch
            {
                // ignored
            }
        }
Beispiel #6
0
        private async void ProcessSuggestions()
        {
            //Words that are going to be added to the database.
            var addedWords = new List <Suggestion>();

            //While there are suggestions
            while (_suggestions.Count != 0)
            {
                var suggestion = _suggestions.First();
                var language   = Client.Languages.First(l => l.ShortName == suggestion.Language);
                //Word doesn't end with a letter.
                if (!char.IsLetter(suggestion.Word.Last()))
                {
                    //Decline the suggestion.
                    await suggestion.Message.ModifyAsync(m => m.Embed =
                                                         EmbedHelper.BuildError(Locale,
                                                                                Locale.WrongEndLetter));

                    _suggestions.Remove(_suggestions.First(s => s.Author == suggestion.Author));
                    continue;
                }

                //Word already exists in the database.
                if (language.Words.Contains(suggestion.Word.ToLower()))
                {
                    //Decline the suggestion.
                    await suggestion.Message.ModifyAsync(m => m.Embed =
                                                         EmbedHelper.BuildError(Locale,
                                                                                Locale.WordAlreadyExists));

                    _suggestions.Remove(_suggestions.First(s => s.Author == suggestion.Author));
                    continue;
                }

                //Check if the word is gibberish.
                var results = language.Words.Where(w => w.ToLower().StartsWith(char.ToLower(suggestion.Word.First())))
                              .ToList().Select(w => StringComparison.LevenshteinDistance(suggestion.Word, w)).ToList();
                //This is going to be really close-checking, since
                //this system is automated and 0 people are checking it.
                if (!results.Any() || results.Max() < 0.3f)
                {
                    //Word looks like gibberish, decline it.
                    await suggestion.Message.ModifyAsync(m => m.Embed =
                                                         EmbedHelper.BuildError(Locale,
                                                                                Locale.NotAWord));

                    _suggestions.Remove(_suggestions.First(s => s.Author == suggestion.Author));
                    continue;
                }

                //Accept the word. Yay :D
                addedWords.Add(suggestion);
                await suggestion.Message.ModifyAsync(m => m.Embed = EmbedHelper.BuildSuccess(Locale,
                                                                                             string.Format(Locale.DoneProcessing, Client.Client.GetUser(suggestion.Author).Mention)));

                _suggestions.Remove(_suggestions.First(s => s.Author == suggestion.Author));
            }

            //Update the database.
            UpdateDatabase(addedWords);
        }