Esempio n. 1
0
        public AuthTokenManager()
        {
            Directory.CreateDirectory(Path.GetDirectoryName(_path));

            // legacy import
            var legacyCfg = LegacyConfig.Load();

            if (legacyCfg != null)
            {
                File.WriteAllText(_path, legacyCfg.AuthToken);
            }

            // load token from filesystem
            if (File.Exists(_path))
            {
                Value = File.ReadAllText(_path);
            }

            if (string.IsNullOrWhiteSpace(Value))
            {
                Log.Info(
                    "MudaeFarm requires your user token in order to proceed.\n" +
                    "\n" +
                    "A user token is a long piece of text that is synonymous to your Discord password.\n" +
                    "How to find your token: https://github.com/chiyadev/MudaeFarm/blob/master/User%20tokens.md\n" +
                    "\n" +
                    "What happens when you enter your token:\n" +
                    "- MudaeFarm will save this token to the disk UNENCRYPTED.\n" +
                    "- MudaeFarm will authenticate to Discord using this token, ACTING AS YOU.\n" +
                    "\n" +
                    "MudaeFarm makes no guarantee regarding your account's privacy nor safety.\n" +
                    "If you are concerned, you may inspect MudaeFarm's complete source code at: https://github.com/chiyadev/MudaeFarm\n" +
                    "\n" +
                    "MudaeFarm is licensed under the MIT License. The authors of MudaeFarm shall not be held liable for any claim, damage or liability.\n" +
                    "You can read the license terms at: https://github.com/chiyadev/MudaeFarm/blob/master/LICENSE\n" +
                    "\n" +
                    "Enter your token:");

                Value = Console.ReadLine();

                File.WriteAllText(_path, Value);

                throw new DummyRestartException {
                          Delayed = false
                };
            }

            Log.Info($"User token loaded from: {_path}");
        }
Esempio n. 2
0
        public async Task InitializeAsync()
        {
            // find config guild
            var userId = _client.CurrentUser.Id;

            foreach (var guild in _client.Guilds)
            {
                if (guild.OwnerId == userId &&
                    guild.TextChannels.Any(c => c.Name == "information" && c.Topic.StartsWith($"MudaeFarm Configuration Server {userId}")))
                {
                    _guild = guild;
                    break;
                }
            }

            if (_guild == null)
            {
                try
                {
                    Log.Warning("Initializing a new configuration server. This may take a while...");

                    _guild = await _client.CreateGuildAsync("MudaeFarm", await _client.GetOptimalVoiceRegionAsync());

                    // delete default channels
                    foreach (var c in await _guild.GetChannelsAsync())
                    {
                        await c.DeleteAsync();
                    }

                    var channel = await CreateChannelAsync("information", $"MudaeFarm Configuration Server {userId} - Do not delete this channel!");

                    var message = await channel.SendMessageAsync(
                        "This is your MudaeFarm server where you can configure the bot.\n" +
                        "\n" +
                        "Check <https://github.com/chiyadev/MudaeFarm> for detailed usage guidelines!");

                    await message.PinAsync();
                }
                catch (Exception e)
                {
                    Log.Warning("Could not initialize configuration server. Try creating it manually.", e);

                    throw new DummyRestartException();
                }
            }

            // load channels
            foreach (var channel in await _guild.GetTextChannelsAsync())
            {
                void set(string name, ref ITextChannel c)
                {
                    if (channel.Name == name)
                    {
                        c = channel;
                    }
                }

                set("information", ref _generalConfigChannel);
                set("wished-characters", ref _wishedCharacterChannel);
                set("wished-anime", ref _wishedAnimeChannel);
                set("bot-channels", ref _botChannelChannel);
            }

            // create channel if not created already
            _wishedCharacterChannel = _wishedCharacterChannel ?? await CreateChannelAsync("wished-characters", "Configure your character wishlist here. Wildcards characters are supported.");

            _wishedAnimeChannel = _wishedAnimeChannel ?? await CreateChannelAsync("wished-anime", "Configure your anime wishlist here. Wildcards characters are supported.");

            _botChannelChannel = _botChannelChannel ?? await CreateChannelAsync("bot-channels", "Configure channels to enable MudaeFarm autorolling/claiming by sending the channel ID.");

            // initial load
            await ReloadChannelAsync(_generalConfigChannel);
            await ReloadChannelAsync(_wishedCharacterChannel);
            await ReloadChannelAsync(_wishedAnimeChannel);
            await ReloadChannelAsync(_botChannelChannel);

            Log.Info("Configuration loaded.");

            // import old configuration (config.json)
            var legacyCfg = LegacyConfig.Load();

            if (legacyCfg != null)
            {
                Log.Warning("Importing legacy wishlist configuration. This may take a while...");

                if (legacyCfg.WishlistCharacters != null)
                {
                    foreach (var character in legacyCfg.WishlistCharacters)
                    {
                        await _wishedCharacterChannel.SendMessageAsync(character);
                    }
                }

                if (legacyCfg.WishlistAnime != null)
                {
                    foreach (var anime in legacyCfg.WishlistAnime)
                    {
                        await _wishedAnimeChannel.SendMessageAsync(anime);
                    }
                }

                LegacyConfig.Delete();
            }

            // events
            _client.MessageReceived += message => ReloadChannelAsync(message.Channel);
            _client.MessageDeleted  += (cacheable, channel) => ReloadChannelAsync(channel);
            _client.MessageUpdated  += (cacheable, message, channel) => ReloadChannelAsync(channel);
        }