示例#1
0
        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));
            }
        }
示例#2
0
        public async Task CreateNew()
        {
            await Context.Channel.SendMessageAsync(Locale.PleaseWait);

            if (Client.Languages.All(x => x.ShortName != Locale.Name))
            {
                throw new Exception($"Diswords: Language {Locale.Name} was not found.");
            }
            var channel = await Context.Guild.CreateTextChannelAsync("diswords-" +
                                                                     (Context.Guild.TextChannels.Count(c =>
                                                                                                       c.Name.Contains("diswords")) + 1));

            //Change the slow mode interval (delay between sending messages).
            await channel.ModifyAsync(c => c.SlowModeInterval = 10);

            //Create the game.
            var game = new Game.Diswords(Client, Context.Message.Author as IGuildUser,
                                         Client.Guilds.First(g => g.Id == Context.Guild.Id), channel, true, 0,
                                         Client.Languages.First(x => x.ShortName == Locale.Name));
            var word = game.Language.Words[new Random((int)DateTime.Now.Ticks).Next(game.Language.Words.Count)];

            //The bot starts the game, so it sets the word first.
            game.SetWord(word, Client.Client.CurrentUser);
            Client.Games.Add(game);
            await channel.SendMessageAsync(null, false,
                                           EmbedHelper.BuildSuccess(Locale, string.Format(Locale.GameCreated, word)));

            await Context.Channel.SendMessageAsync(null, false,
                                                   EmbedHelper.BuildSuccess(Locale, $"\n{channel.Mention}"));
        }
示例#3
0
        public async Task SetPrefix(string prefix)
        {
            //The loading gif :D
            RestUserMessage?loading = null;

            if (!string.IsNullOrEmpty(Client.Config.LoadingGif))
            {
                loading = await Context.Channel.SendFileAsync(Client.Config.LoadingGif);
            }
            //This code is kind of bad but i don't know how to improve it
            var guild = Client.Guilds.First(g => g.Id == Context.Guild.Id);

            guild.Prefix = prefix;
            guild        = DiswordsClient.StaticGuilds.First(g => g.Id == Context.Guild.Id);
            guild.Prefix = prefix;
            var path =
                $"{Client.Config.RootDirectory}{Path.DirectorySeparatorChar}{Client.Config.GuildsDirectoryName}{(Client.Config.GuildsDirectoryName == "" ? "" : Path.DirectorySeparatorChar.ToString())}{guild.Id}.json";

            //Write the new server to the file.
            guild.OverwriteTo(path);
            if (loading != null)
            {
                await loading.DeleteAsync();
            }
            await Context.Channel.SendMessageAsync(null, false, EmbedHelper.BuildSuccess(Locale, Locale.PrefixChanged));
        }
示例#4
0
        public async Task Create()
        {
            await Context.Channel.SendMessageAsync(Locale.PleaseWait);

            //I consider this a warning because I wouldn't recommend creating a game in general, for example.
            await Context.Channel.SendMessageAsync(null, false,
                                                   EmbedHelper.BuildWarning(Locale,
                                                                            string.Format(Locale.NewChannelWarning, Client.Guilds.First(g => g.Id == Context.Guild.Id).Prefix,
                                                                                          Locale.CreateNew)));

            if (Client.Languages.All(x => x.ShortName != Locale.Name))
            {
                throw new Exception($"Diswords: Language {Locale.Name} was not found.");
            }
            //Change the slow mode interval (delay between sending messages).
            var smi = ((SocketTextChannel)Context.Channel).SlowModeInterval;

            await((SocketTextChannel)Context.Channel).ModifyAsync(c =>
            {
                c.SlowModeInterval = 10;
            });
            //Create the game.
            var game = new Game.Diswords(Client, Context.Message.Author as IGuildUser,
                                         Client.Guilds.First(g => g.Id == Context.Guild.Id), Context.Channel as SocketTextChannel, false, smi,
                                         Client.Languages.First(x => x.ShortName == Locale.Name));
            var word = game.Language.Words[new Random((int)DateTime.Now.Ticks).Next(game.Language.Words.Count)];

            //The bot starts the game, so it sets the word first.
            game.SetWord(word, Client.Client.CurrentUser);
            Client.Games.Add(game);
            await Context.Channel.SendMessageAsync(null, false,
                                                   EmbedHelper.BuildSuccess(Locale, string.Format(Locale.GameCreated, word)));
        }
示例#5
0
        public async void Stop()
        {
            Guild.GamesPlayed++;
            Guild.OverwriteTo(
                $"{_client.Config.RootDirectory}{Path.DirectorySeparatorChar}{_client.Config.GuildsDirectoryName}{(_client.Config.GuildsDirectoryName == "" ? "" : Path.DirectorySeparatorChar.ToString())}{Guild.Id}.json");
            switch (_newChannelCreated)
            {
            case true:
            {
                await Channel.SendMessageAsync(null, false, EmbedHelper.BuildSuccess(Locale, Locale.GameDeleted));

                await Task.Delay(10000);

                await Channel.DeleteAsync();

                _client.Games.Remove(this);
                break;
            }

            case false:
            {
                await Channel.SendMessageAsync(null, false, EmbedHelper.BuildSuccess(Locale, Locale.GameDeleted));

                await Task.Delay(10000);

                await Channel.ModifyAsync(c => c.SlowModeInterval = _previousSlowModeInterval);

                _client.Games.Remove(this);
                break;
            }
            }
        }
示例#6
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();
            }
        }
示例#7
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
            }
        }
示例#8
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);
        }