Exemple #1
0
        private async Task <int> PrefixResolver(DiscordMessage msg)
        {
            if (!msg.Channel.PermissionsFor(await msg.Channel.Guild.GetMemberAsync(Client.CurrentUser.Id).ConfigureAwait(false)).HasPermission(Permissions.SendMessages))
            {
                return(-1);                                                                                                                                                          //Checks if bot can't send messages, if so ignore.
            }
            if (msg.Content.StartsWith(Config.Prefix))
            {
                return(Config.Prefix.Length);                                       //Always respond with default prefix.
            }
            else
            {
                try
                {
                    if (GuildConfigs.TryGetValue(msg.Channel.GuildId, out GuildConfig gConfig))
                    {
                        // Do nothing, we just want the value.
                    }
                    else
                    {
                        gConfig = await DatabaseHandler.GetSingleGuildConfigAsync(msg.Channel.GuildId).ConfigureAwait(false);

                        if (gConfig is null)
                        {
                            gConfig = new GuildConfig
                            {
                                GuildId = msg.Channel.GuildId,
                                Prefix  = Config.Prefix
                            };

                            _ = UpdateGuildConfigAsync(gConfig).ConfigureAwait(false);
                            Client.Logger.LogInformation(Program.PrefixManager, $"Resolver generated deafult config for: {msg.Channel.Guild.Name}", DateTime.Now);
                            return(-1);
                        }
                        else
                        {
                            GuildConfigs.TryAdd(gConfig.GuildId, gConfig);
                        }
                    }

                    foreach (string cmd in CommandList)                   //Loop through all current commands.
                    {
                        if (msg.Content.StartsWith(gConfig.Prefix + cmd)) //Check if message starts with prefix AND command.
                        {
                            return(gConfig.Prefix.Length);                //Return length of server prefix.
                        }
                    }

                    return(-1); //If not, ignore.
                }
                catch (Exception err)
                {
                    Client.Logger.LogError(Program.PrefixManager, $"Resolver failed in guild {msg.Channel.Guild.Name}:", DateTime.Now, err);
                    return(-1);
                }
            }
        }